首页 > 解决方案 > 从 SQL asp.net 页面导出数据到 excel 会截断 < 字符上的数据

问题描述

我们已经成功地将数据从 asp.net 系统导出到 excel 文件中。然而,我们刚刚遇到了一个问题。如果要导出的数据在其文本中包含“<”(小于)运算符,则 Excel 会截断该符号处的文本。

例如,在小于号周围没有单引号 ''(必须将它们放在这里以显示符号):

如果我们导出的文本是“该项目的成本是 '<'$5,000 并且收益远远超过成本

唯一会导入到 excel 中的部分是“项目的成本是

但是,当我们查看我们在 asp.net 系统本身中导出的完全相同的字段时,我们会看到整行文本“项目的成本是 '<'$5,000 并且收益远远超过成本

有没有人对如何导出整行文本(即:能够显示特殊字符)而不用 excel 截断文本有任何想法?

设置excel文件的代码后面

                Response.Buffer = true;
                Response.ContentType = "application/vnd.ms-excel ";
                Response.AddHeader("content-Disposition", "attachment;filename=" +
                    SessionInfo.CurrentComplete + RepType + ".xls");
                Response.Write(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">");
                Response.Charset = "UTF-8";

设置excel字段的代码

                        switch (defrow["DataType"].ToString())
                        {
                            case "image":
                                System.Web.UI.WebControls.Image imgPS = new System.Web.UI.WebControls.Image();
                                imgPS.ImageUrl = datarow[defrow["ColumnName"].ToString()].ToString();
                                imgPS.Height = 15;
                                imgPS.Width = 15;
                                tCell.Controls.Add(imgPS);
                                tCell.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
                                break;
                            case "date":
                                convertedDate = Convert.ToDateTime(datarow[defrow["ColumnName"].ToString()].ToString());
                                tCell.Text = convertedDate.ToString("d MMM yyyy");
                                break;
                            case "comment":
                                tCell.Text = datarow[defrow["ColumnName"].ToString()].ToString();
                                tCell.Width = 300;
                                break;
                            case "longtext":
                                tCell.Text = RemoveHtmlTag(datarow[defrow["ColumnName"].ToString()].ToString()); 
                                tCell.Width = 500;
                                break;
                            case "shorttext":
                                tCell.Text = datarow[defrow["ColumnName"].ToString()].ToString();
                                tCell.Width = 250;
                                break;
                            default:
                                tCell.Text = datarow[defrow["ColumnName"].ToString()].ToString();
                                break;
                        }

SQL 查询用于获取数据以显示在 asp.net 系统本身中使用的主表中的信息以及此处显示的主导出。

        string sql = "SELECT * " + rights +
                     "from table_information " + condition + sortorder;

非常感谢所有想法。

标签: sqlasp.netexcelspecial-characterstruncate

解决方案


  1. 如果您的文本包含单引号/双引号,则需要将每个引号括在双引号中。然后,它也会保留双引号。

尝试将文本从

"The cost of the project was '<'$5,000 and the benefit far outweighed the cost"

"The cost of the project was "'<"'$5,000 and the benefit far outweighed the cost"
  1. 此外,不需要单引号。您是否还可以尝试通过您的代码替换以下文本 -
"The cost of the project was '<'$5,000 and the benefit far outweighed the cost"

"The cost of the project was &lt;$5,000 and the benefit far outweighed the cost"

推荐阅读