从SQL asp.net页面导出数据以excel截断

问题描述 投票:0回答:1

我们已经成功地将数据从asp.net系统导出到excel文件中。但是,我们只遇到了一个问题。如果要导出的数据在其文本中包含“

例如,在小于号周围没有单引号“(必须将其放在此处以显示符号):

如果我们要导出的文字是“ 该项目的成本为'“

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

但是,当我们查看要在asp.net系统本身中导出的完全相同的字段时,会看到整行文本“ 该项目的成本为'“

有人对如何导出整个文本行(例如:能够显示特殊字符)有任何想法,而不必擅长截断文本?

设置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;

非常感谢所有想法。

sql asp.net excel special-characters truncate
1个回答
0
投票
  1. 如果文本包含单引号/双引号,则需要将每个引号括在双引号中。然后,它也会保留双引号。

尝试从以下位置更改文本

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

to

"The cost of the project was "'<"'$5,000 and the benefit far outweighed the cost"
  1. 而且,也不需要单引号。您还可以尝试通过代码替换-
  2. 中的文本吗?
"The cost of the project was '<'$5,000 and the benefit far outweighed the cost"

to

"The cost of the project was &lt;$5,000 and the benefit far outweighed the cost"
© www.soinside.com 2019 - 2024. All rights reserved.