为什么当我在pdf文件中下载gridview时,它显示错误
本文关键字:gridview 错误 显示 下载 pdf 文件 为什么 | 更新日期: 2023-09-27 18:06:23
我实际上想下载包含pid的pdf文件,以及从一月到十二月的月份。
月份包含浮点数,其中逗号后有9个数字,因此它使gridview比它应该的更宽。当我下载pdf文件时,它显示错误' system . web . ui . htmltextwwriter '
这里到底发生了什么?
这是我的代码
connection.Open();
SqlCommand dm = new SqlCommand("select id,sum (case when [Month] = 1 then demand else 0.0 end) January, sum(case when [Month] = 2 then demand else 0.0 end) February,sum(case when [Month] = 3 then demand else 0.0 end) March ,sum(case when [Month] = 4 then demand else 0.0 end) April ,sum(case when [Month] = 5 then demand else 0.0 end) May ,sum(case when [Month] = 6 then demand else 0.0 end) June ,sum(case when [Month] = 7 then demand else 0.0 end) July ,sum(case when [Month] = 8 then demand else 0.0 end) August ,sum(case when [Month] = 9 then demand else 0.0 end) September ,sum(case when [Month] = 10 then demand else 0.0 end) October ,sum(case when [Month] = 11 then demand else 0.0 end) November ,sum(case when [Month] = 12 then demand else 0.0 end) December from reorder group by id", connection);
SqlDataReader fdm = dm.ExecuteReader();
if (fdm.HasRows)
{
fdm.Read();
GridView3.DataSource = fdm;
}
connection.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Demand.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter wsfdm = new StringWriter();
HtmlTextWriter fdmws = new HtmlTextWriter(wsfdm);
GridView3.AllowPaging = false;
GridView3.RenderControl(fdmws);
GridView3.HeaderRow.Style.Add("width", "20%");
GridView3.HeaderRow.Style.Add("font-size", "8x");
GridView3.Style.Add("text-decoration", "none");
GridView3.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
GridView3.Style.Add("font-size", "8px");
StringReader fgdm = new StringReader(fdmws.ToString());
Document documentpdf = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(documentpdf);
PdfWriter.GetInstance(documentpdf, Response.OutputStream);
documentpdf.Open();
htmlparser.Parse(fgdm);
documentpdf.Close();
Response.Write(documentpdf);
Response.End();
}
您必须在StringReader
类中使用StringWriter
对象而不是HtmlTextWriter
试试这个
connection.Open();
SqlCommand dm = new SqlCommand("select id,sum (case when [Month] = 1 then demand else 0.0 end) January, sum(case when [Month] = 2 then demand else 0.0 end) February,sum(case when [Month] = 3 then demand else 0.0 end) March ,sum(case when [Month] = 4 then demand else 0.0 end) April ,sum(case when [Month] = 5 then demand else 0.0 end) May ,sum(case when [Month] = 6 then demand else 0.0 end) June ,sum(case when [Month] = 7 then demand else 0.0 end) July ,sum(case when [Month] = 8 then demand else 0.0 end) August ,sum(case when [Month] = 9 then demand else 0.0 end) September ,sum(case when [Month] = 10 then demand else 0.0 end) October ,sum(case when [Month] = 11 then demand else 0.0 end) November ,sum(case when [Month] = 12 then demand else 0.0 end) December from reorder group by id", connection);
SqlDataReader fdm = dm.ExecuteReader();
if (fdm.HasRows)
{
fdm.Read();
GridView3.DataSource = fdm;
}
connection.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Demand.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter wsfdm = new StringWriter();
HtmlTextWriter fdmws = new HtmlTextWriter(wsfdm);
GridView3.AllowPaging = false;
GridView3.RenderControl(fdmws);
GridView3.HeaderRow.Style.Add("width", "20%");
GridView3.HeaderRow.Style.Add("font-size", "8x");
GridView3.Style.Add("text-decoration", "none");
GridView3.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
GridView3.Style.Add("font-size", "8px");
StringReader fgdm = new StringReader(wsfdm.ToString());
Document documentpdf = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(documentpdf);
PdfWriter.GetInstance(documentpdf, Response.OutputStream);
documentpdf.Open();
htmlparser.Parse(fgdm);
documentpdf.Close();
Response.Write(documentpdf);
Response.End();
}
来源:
看看这里export-gridview-to-pdf
在连接关闭后粘贴此代码:
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
//To Export all pages
GridView1.AllowPaging = false;
GridView3.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A2, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Demand.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
}
}
不要忘记包含iTextSharp DLL