Моих Mockup страницы с Grid View:Экспорт Gridview в Excel и PDF не работает.
<div style="overflow:scroll; height: 447px;">
<asp:GridView ID="GridView1" AutoGenerateColumns="true" GridLines="Both" RowStyle-BorderWidth="0" runat="server" CellPadding="4" ForeColor="#333333">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
</div>
Моим VB код для экспорта в опцию кнопки Excel с помощью itextSharp DLL:
Protected Sub ExportAsExcel_Click(sender As Object, e As EventArgs) Handles ExportAsExcel.Click
'Exporting the results to an excel spreadsheet
Response.Clear()
Response.Buffer = True
Response.AddHeader("content-disposition",
"attachment;filename=GridViewExport.xls")
Response.Charset = ""
Response.ContentType = "application/vnd.ms-excel"
Dim sw As New StringWriter()
Dim hw As New HtmlTextWriter(sw)
GridView1.AllowPaging = False
GridView1.DataBind()
'Change the Header Row back to white color
GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF")
'Apply style to Individual Cells
For i As Integer = 0 To GridView1.HeaderRow.Cells.Count - 1
GridView1.HeaderRow.Cells(i).Style.Add("background-color", "green")
Next
For i As Integer = 0 To GridView1.Rows.Count - 1
Dim row As GridViewRow = GridView1.Rows(i)
'Change Color back to white
row.BackColor = System.Drawing.Color.White
'Apply text style to each Row
row.Attributes.Add("class", "textmode")
'Apply style to Individual Cells of Alternating Row
If i Mod 2 <> 0 Then
row.Cells(0).Style.Add("background-color", "#C2D69B")
row.Cells(1).Style.Add("background-color", "#C2D69B")
row.Cells(2).Style.Add("background-color", "#C2D69B")
row.Cells(3).Style.Add("background-color", "#C2D69B")
End If
Next
GridView1.RenderControl(hw)
'style to format numbers to string
Dim style As String = "<style>.textmode{mso-number-format:\@;}</style>"
Response.Write(style)
Response.Output.Write(sw.ToString())
Response.Flush()
Response.End()
End Sub
Моего код для экспорта страницы в PDF с помощью itextsharp dll ниже:
Protected Sub ExportAsPDF_Click(sender As Object, e As EventArgs) Handles ExportAsPDF.Click
Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition",
"attachment;filename=GridViewExport.pdf")
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Dim sw As New StringWriter()
Dim hw As New HtmlTextWriter(sw)
GridView1.AllowPaging = False
GridView1.DataBind()
GridView1.RenderControl(hw)
Dim sr As New StringReader(sw.ToString())
Dim pdfDoc As New Document(PageSize.A0, 10.0F, 10.0F, 10.0F, 0.0F)
Dim htmlparser As New HTMLWorker(pdfDoc)
PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
pdfDoc.Open()
htmlparser.Parse(sr)
pdfDoc.Close()
Response.Write(pdfDoc)
Response.End()
End Sub
Не уверен, что нужно делать здесь, у меня также есть следующие импорты.
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.text.html
Imports iTextSharp.text.html.simpleparser
Imports System.IO
Однако iTextSharp.text.html импортирован не используется, я не знаю почему. всего несколько дней назад этот код работал отлично. а не нет. Я получил этот код со следующего URL: Нажмите here!
Вы не сказали, какую версию iTextSharp вы используете. Пожалуйста, измените свой код, чтобы работать с 'XMLWorker' вместо' HTMLWorker' (устаревший) и посмотреть, сохраняется ли ваша проблема, когда вы используете версию iTextSharp '5.5.9'. –
Где бы я нашел эту версию! – JT4U
Почему, на официальном сайте, конечно! http://itextpdf.com –