本文介绍了如何将gridview中的图表控件添加到PDF文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在尝试将gridview1的内容下载到Excel和PDF文件中。使用iTextSharp尝试了很多方法。我尝试渲染,也逐个复制创建pdfTable。没有用。请帮助 请帮助我,因为我是ASP.NET中图表控件的新手 我尝试过: private void PDF_Export() { Response.ContentType =application / pdf; Response.AddHeader(content-disposition,attachment; filename = GridViewExport.pdf); Response.Cache.SetCacheability(HttpCacheability.NoCache); StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); GridView1.AllowPaging = false; GridView1.DataBind(); GridView1.RenderControl(hw); StringReader sr = new StringReader(sw.ToString()); 文档pdfDoc =新文档(PageSize.A4,10f,10f,10f,0f); HTMLWorker htmlparser = new HTMLWorker( pdfDoc); PdfWriter.GetInstance(pdfDoc,Response.OutputStream); pdfDoc.Open(); htmlparser.Parse(sr); pdfDoc.Close(); Response.Write(pdfDoc); Response.End(); } 我也尝试使用Button Click Event mwod中的以下代码。仍然失败。 int columnsCount = GridView1.HeaderRow.Cells.Count; PdfPTable pdfTable = new PdfPTable(columnsCount); foreach(GridView1.HeaderRow.Cells中的TableCell gridViewHeaderCell) { 字体字体=新字体(); font.Color = new BaseColor(GridView1.HeaderStyle.ForeColor); PdfPCell pdfCell = new PdfPCell(new Phrase(gridViewHeaderCell.Text,font) ); pdfCell.BackgroundColor = new BaseColor(GridView1.HeaderStyle.BackColor); //将单元格添加到PDF表格 pdfTable.AddCell(pdfCell); } foreach(GridView1.Rows中的GridViewRow gridViewRow) { if(gridViewRow.RowType == DataControlRowTyp e.DataRow) { foreach(GridViewRow.Cells中的TableCell gridViewCell) { Font font = new Font(); font.Color = new BaseColor(GridView1.RowStyle.ForeColor); PdfPCell pdfCell = new PdfPCell(new Phrase(gridViewCell.Text,font)); pdfCell.BackgroundColor = new BaseColor(GridView1.RowStyle.BackColor); pdfTable.AddCell(pdfCell); } } } 文件pdfDocument =新文件(PageSize.A4,10f,10f,10f,10f); PdfWriter.GetInstance(pdfDocument,Response.OutputStream); pdfDocument.Open(); pdfDocument。添加(pdfTable); pdfDocument.Close(); Response.ContentType =app lication / pdf; Response.AppendHeader(content-disposition, attachment; filename = Employees.pdf); Response.Write(pdfDocument); Response.Flush(); Response.End(); 解决方案 很高兴看到你的问题。 我刚刚为你的需求创建了一个示例代码。但是我还没有在gridview中创建图表。我只是用于将图表控件导出为pdf文件的快速编写代码。 我用iTextSharp库创建pdf文件。 步骤1:将以下图表控件放在网页上 < asp:Chart runat = server ID = Chart1 > < 系列 > < asp:Series 名称 = Series1 XValueMember = col1 YValueMembers = col2 > < / asp:Series > < / Series > < ChartAreas > < asp:ChartArea 名称 = ChartArea1 > < / asp:ChartArea > < / ChartAreas > < / asp:Chart > 第2步:点击按钮将图表控件导出为pdf protected void btnExportChart_Click( object sender,EventArgs e) { DataTable dtData = new DataTable(); dtData.Columns.Add( col1, typeof ( decimal )); dtData.Columns.Add( col2, typeof ( decimal )); dtData.Rows.Add( 2 , 10 ); dtData.Rows.Add( 4 , 6 ); dtData.Rows.Add( 6 , 9 ); dtData.Rows.Add( 8 , 12 ); dtData.Rows.Add( 10 , 15 ); dtData.AcceptChanges(); Chart1.DataSource = dtData; Chart1.DataBind(); string img_path = Server.MapPath( 图片/ chart1.jpg); Chart1.SaveImage(img_path,ChartImageFormat.Jpeg); 凭证doc = new 凭证(PageSize.LETTER, 50 , 50 , 50 , 50 ); 使用(MemoryStream输出= new MemoryStream()) { PdfWriter wri = PdfWriter.GetInstance(doc,output); doc.Open(); iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(img_path); doc.Add(img); doc.Close(); Response.ClearContent(); Response.ClearHeaders(); Response.ContentType = application / pdf; Response.AddHeader( Content-Disposition, attachment; filename = testpdf.pdf); Response.BinaryWrite(output.ToArray()); Response.End(); Response.Flush(); Response.Clear(); } } 希望这可以帮到你根据您对gridview的要求使用此代码。 如果您需要任何说明,请恢复。 这是exporitng gridview的更新代码pdf以及图表 第1步:将此gridview和按钮放在您的aspx页面上: < asp:按钮 ID = btnExportGridView runat = server 文字 = ExportChart OnClick = btnExportGridView_Click / > < asp:GridView ID = gvData runat = 服务器 AutoGenerateColumns = false onrowcreated = gvData_RowCreated onrowdatabound = gvData_RowDataBound > < 列 > < asp:BoundField HeaderText = 用户名 DataField = 用户名 ItemStyle-Width = 120px / > < asp:TemplateField HeaderText = 标记报告 > < ItemTemplate > < asp:Chart ID = Chart2 runat = server 宽度 = 250px 高度 = 200px > < 系列 > < asp:Series 名称 = Series1 LegendText = LEVEL 1 IsValueShownAsLabel = false ChartArea = ChartArea1 MarkerBorderColor = #DBDBDB XValueMember = col1 YValueMembers = col2 > < / asp:Series > < / Series > < ChartAreas > < asp :ChartArea 名称 = ChartArea1 > < / asp:ChartArea > < / ChartAreas > < / asp:Chart > < / ItemTemplate > < / asp:TemplateField > < / Columns > < / asp:GridView > 第2步:将示例数据绑定到GridView: DataTable dtData; 受保护 void Page_Load( object sender,EventArgs e) { BindData(); } private void BindData() { dtData = GetData(); gvData.DataSource = dtData; gvData.DataBind(); } private DataTable GetData() { DataTable dtDataTmp = new DataTable(); dtDataTmp.Columns.Add( 用户名, typeof ( string )); dtDataTmp.Columns.Add( 标记, typeof运算(数据表)); DataTable dt1 = new DataTable(); dt1.Columns.Add( col1, typeof ( string )); dt1.Columns.Add( col2, typeof ( decimal )); DataTable dt2 = new DataTable(); dt2.Columns.Add( col1, typeof ( string )); dt2.Columns.Add( col2, typeof ( decimal )); dt1.Rows.Add( Math, 40 ); dt1.Rows.Add( Science, 65 ); dt1.Rows.Add( English, 74 ); dt1.AcceptChanges(); dt2.Rows.Add( Math, 45 ); dt2.Rows.Add( Science, 60 ); dt2.Rows.Add( English, 30 ); dt2.AcceptChanges(); dtDataTmp.Rows.Add( Rohit,dt1); dtDataTmp.Rows.Add( Mohit,dt2); dtDataTmp.AcceptChanges(); return dtDataTmp; } protected void gvData_RowDataBound( object sender,GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { DataTable dtTmp =(DataTable)dtData.Rows [e.Row.RowIndex] [ 标记]; 图表图表=(图表)e.Row.FindControl( Chart2); chart.DataSource = dtTmp; chart.DataBind(); } } 第3步:使用iTextSharp将Gridview和图表导出为pdf格式的代码: protected void btnExportGridView_Click( object sender,EventArgs e) {文件doc = 新文件(PageSize.LETTER, 50 , 50 , 50 , 50 ); 使用(MemoryStream输出= new MemoryStream()) { PdfWriter wri = PdfWriter.GetInstance(doc,output); doc.Open(); GridViewRow gvr = null ; PdfPTable tab = new PdfPTable( 2 ); PdfPCell [] cells = null ; 图表chrt = null ; Random rnd = new Random(); string img_path = ; List< string> lstImgs = new List< string>(); for ( int i = 0 ; i < gvData.Rows.Count; i ++) { gvr = gvData.Rows [i]; PdfPCell c1 = new PdfPCell(); PdfPCell c2 = new PdfPCell(); for ( int j = 0 ; j < gvData.Rows [i] .Cells.Count; j ++) { c1 = new PdfPCell(); c2 = new PdfPCell(); c1.AddElement( new 段落(gvr.Cells [ 0 ]。Text)); chrt =(图表)gvr.FindControl( Chart2); img_path = Server.MapPath( Images / img _ + rnd.NextDouble()+ 。jpg); chrt.SaveImage(img_path); c2.AddElement(iTextSharp.text.Image.GetInstance(img_path)); lstImgs.Add(img_path); } cells = new PdfPCell [] {c1,c2}; tab.Rows.Add( new PdfPRow(cells)); } doc.Add(tab); doc.Close(); foreach ( string img in lstImgs) { if (File.Exists(img)) File.Delete(img); } Response.ClearContent(); Response.ClearHeaders(); Response.ContentType = application / pdf; Response.AddHeader( Content-Disposition, attachment; filename = testpdf.pdf); Response.BinaryWrite(output.ToArray()); Response.End(); Response.Flush(); Response.Clear(); } } 享受!! I am trying to download the content of gridview1 to Excel and PDF file. Tried Lots of ways using iTextSharp. I tried rendering , also copying cell by cell creating pdfTable. None worked. Please HelpPlease help me as I am new to chart control in ASP.NETWhat I have tried:private void PDF_Export() { Response.ContentType = "application/pdf"; Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf"); Response.Cache.SetCacheability(HttpCacheability.NoCache); StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); GridView1.AllowPaging = false; GridView1.DataBind(); GridView1.RenderControl(hw); StringReader sr = new StringReader(sw.ToString()); Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f); HTMLWorker htmlparser = new HTMLWorker(pdfDoc); PdfWriter.GetInstance(pdfDoc, Response.OutputStream); pdfDoc.Open(); htmlparser.Parse(sr); pdfDoc.Close(); Response.Write(pdfDoc); Response.End(); }also I tried with following code in Button Click Event mwthod. Still Failed. int columnsCount = GridView1.HeaderRow.Cells.Count; PdfPTable pdfTable = new PdfPTable(columnsCount); foreach(TableCell gridViewHeaderCell in GridView1.HeaderRow.Cells) { Font font = new Font(); font.Color = new BaseColor(GridView1.HeaderStyle.ForeColor); PdfPCell pdfCell = new PdfPCell(new Phrase(gridViewHeaderCell.Text, font)); pdfCell.BackgroundColor = new BaseColor(GridView1.HeaderStyle.BackColor); // Add the cell to PDF table pdfTable.AddCell(pdfCell); } foreach (GridViewRow gridViewRow in GridView1.Rows) { if (gridViewRow.RowType == DataControlRowType.DataRow) { foreach (TableCell gridViewCell in gridViewRow.Cells) { Font font = new Font(); font.Color = new BaseColor(GridView1.RowStyle.ForeColor); PdfPCell pdfCell = new PdfPCell(new Phrase(gridViewCell.Text, font)); pdfCell.BackgroundColor = new BaseColor(GridView1.RowStyle.BackColor); pdfTable.AddCell(pdfCell); } } } Document pdfDocument = new Document(PageSize.A4, 10f, 10f, 10f, 10f); PdfWriter.GetInstance(pdfDocument, Response.OutputStream); pdfDocument.Open(); pdfDocument.Add(pdfTable); pdfDocument.Close(); Response.ContentType = "application/pdf"; Response.AppendHeader("content-disposition", "attachment;filename=Employees.pdf"); Response.Write(pdfDocument); Response.Flush(); Response.End(); 解决方案 Hi, nice to see your question.I just created a sample code for your requirement. However I have not created chart inside a gridview. I have just quicky written code for exporting a chart control to pdf file.I have used iTextSharp library for creating pdf file.Step 1 : Place the following Chart Control on the web page<asp:Chart runat="server" ID="Chart1"> <Series> <asp:Series Name="Series1" XValueMember="col1" YValueMembers="col2"> </asp:Series> </Series> <ChartAreas> <asp:ChartArea Name="ChartArea1"> </asp:ChartArea> </ChartAreas></asp:Chart>Step 2: Exporting the chart control to pdf on click of a buttonprotected void btnExportChart_Click(object sender, EventArgs e) { DataTable dtData = new DataTable(); dtData.Columns.Add("col1", typeof(decimal)); dtData.Columns.Add("col2", typeof(decimal)); dtData.Rows.Add(2, 10); dtData.Rows.Add(4, 6); dtData.Rows.Add(6, 9); dtData.Rows.Add(8, 12); dtData.Rows.Add(10, 15); dtData.AcceptChanges(); Chart1.DataSource = dtData; Chart1.DataBind(); string img_path = Server.MapPath("Images/chart1.jpg"); Chart1.SaveImage(img_path, ChartImageFormat.Jpeg); Document doc = new Document(PageSize.LETTER, 50, 50, 50, 50); using (MemoryStream output = new MemoryStream()) { PdfWriter wri = PdfWriter.GetInstance(doc, output); doc.Open(); iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(img_path); doc.Add(img); doc.Close(); Response.ClearContent(); Response.ClearHeaders(); Response.ContentType = "application/pdf"; Response.AddHeader("Content-Disposition"," attachment;filename=testpdf.pdf"); Response.BinaryWrite(output.ToArray()); Response.End(); Response.Flush(); Response.Clear(); } }Hope this might help you to use this code according to your requirement for gridview too.Please revert if you need any clarifications.Here is the updated code for exporitng gridview to pdf along with the chartsStep 1: Place this gridview and button on your aspx page :<asp:Button ID="btnExportGridView" runat="server" Text="ExportChart" OnClick="btnExportGridView_Click" /><asp:GridView ID="gvData" runat="server" AutoGenerateColumns="false"onrowcreated="gvData_RowCreated" onrowdatabound="gvData_RowDataBound"><Columns><asp:BoundField HeaderText="Username" DataField="Username" ItemStyle-Width="120px" /><asp:TemplateField HeaderText="marks Report"><ItemTemplate><asp:Chart ID="Chart2" runat="server" Width="250px" Height="200px"><Series> <asp:Series Name="Series1" LegendText="LEVEL 1" IsValueShownAsLabel="false" ChartArea="ChartArea1" MarkerBorderColor="#DBDBDB" XValueMember="col1" YValueMembers="col2"> </asp:Series> </Series><ChartAreas><asp:ChartArea Name="ChartArea1"></asp:ChartArea></ChartAreas></asp:Chart></ItemTemplate></asp:TemplateField></Columns></asp:GridView>Step 2: Binding Sample Data to GridView :DataTable dtData;protected void Page_Load(object sender, EventArgs e) { BindData(); }private void BindData() { dtData = GetData(); gvData.DataSource = dtData; gvData.DataBind(); }private DataTable GetData() { DataTable dtDataTmp = new DataTable(); dtDataTmp.Columns.Add("Username", typeof(string)); dtDataTmp.Columns.Add("Marks", typeof(DataTable)); DataTable dt1 = new DataTable(); dt1.Columns.Add("col1", typeof(string)); dt1.Columns.Add("col2", typeof(decimal)); DataTable dt2 = new DataTable(); dt2.Columns.Add("col1", typeof(string)); dt2.Columns.Add("col2", typeof(decimal)); dt1.Rows.Add("Math", 40); dt1.Rows.Add("Science", 65); dt1.Rows.Add("English", 74); dt1.AcceptChanges(); dt2.Rows.Add("Math", 45); dt2.Rows.Add("Science", 60); dt2.Rows.Add("English", 30); dt2.AcceptChanges(); dtDataTmp.Rows.Add("Rohit", dt1); dtDataTmp.Rows.Add("Mohit", dt2); dtDataTmp.AcceptChanges(); return dtDataTmp; }protected void gvData_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { DataTable dtTmp = (DataTable)dtData.Rows[e.Row.RowIndex]["Marks"]; Chart chart = (Chart)e.Row.FindControl("Chart2"); chart.DataSource = dtTmp; chart.DataBind(); } }Step 3: Code to Export the Gridview alongwith chart to pdf format using iTextSharp :protected void btnExportGridView_Click(object sender, EventArgs e) { Document doc = new Document(PageSize.LETTER, 50, 50, 50, 50); using (MemoryStream output = new MemoryStream()) { PdfWriter wri = PdfWriter.GetInstance(doc, output); doc.Open(); GridViewRow gvr = null; PdfPTable tab = new PdfPTable(2); PdfPCell[] cells = null; Chart chrt = null; Random rnd = new Random(); string img_path = ""; List<string> lstImgs = new List<string>(); for (int i = 0; i < gvData.Rows.Count; i++) { gvr = gvData.Rows[i]; PdfPCell c1 = new PdfPCell(); PdfPCell c2 = new PdfPCell(); for (int j = 0; j < gvData.Rows[i].Cells.Count; j++) { c1 = new PdfPCell(); c2 = new PdfPCell(); c1.AddElement(new Paragraph(gvr.Cells[0].Text)); chrt = (Chart)gvr.FindControl("Chart2"); img_path = Server.MapPath("Images/img_" + rnd.NextDouble() + ".jpg"); chrt.SaveImage(img_path); c2.AddElement(iTextSharp.text.Image.GetInstance(img_path)); lstImgs.Add(img_path); } cells = new PdfPCell[] { c1, c2 }; tab.Rows.Add(new PdfPRow(cells)); } doc.Add(tab); doc.Close(); foreach (string img in lstImgs) { if (File.Exists(img)) File.Delete(img); } Response.ClearContent(); Response.ClearHeaders(); Response.ContentType = "application/pdf"; Response.AddHeader("Content-Disposition", "attachment; filename=testpdf.pdf"); Response.BinaryWrite(output.ToArray()); Response.End(); Response.Flush(); Response.Clear(); } }Enjoy !! 这篇关于如何将gridview中的图表控件添加到PDF文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-23 03:47