将数据从sql asp.net页导出到excel将截断< character

gywdnpxw  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(253)

我们已经成功地将数据从asp.net系统导出到excel文件中。然而,我们刚刚遇到一个问题。如果要导出的数据的文本中有一个“<”(小于)运算符,excel将截断该符号处的文本。
例如,如果小于号周围没有单引号“”(必须将它们放在此处以显示符号):
如果我们导出的文本是“项目成本在5000美元以下,收益远远大于成本”
唯一可以导入excel的部分是“项目的成本是”
然而,当我们查看在asp.net系统中导出的完全相同的字段时,我们会看到整行文字“项目的成本是‘<’5000美元,而且收益远远超过成本”
有没有人知道如何在不让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;

所有的想法都非常感谢。

tyky79it

tyky79it1#

如果文本包含单引号/双引号,则需要将每个引号括在双引号中。它也会保留双引号。
尝试将文本从

"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"

同样,也不需要单引号。你能不能试着用你的代码替换-

"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"

相关问题