ASPForums.Net RSS Feed additions to the content that appears on ASPForums.Net(c) 2019 All rights reserved.Convert Excel file to Pdf in ASP.Net<p>Hi <a class="username" rel="ritesh18a"> ritesh18a</a>,</p> <p>Please refer below sample.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;div&gt; &nbsp;&nbsp;&nbsp; &lt;asp:FileUpload ID=&#34;FileUpload1&#34; runat=&#34;server&#34; /&gt; &nbsp;&nbsp;&nbsp; &lt;asp:Button ID=&#34;btnImport&#34; runat=&#34;server&#34; Text=&#34;Import&#34; OnClick=&#34;ImportExcel&#34; /&gt; &lt;/div&gt;</pre> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.IO; using System.Data; using ClosedXML.Excel; using iTextSharp.text; using iTextSharp.text.html.simpleparser; using iTextSharp.text.pdf;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.IO Imports iTextSharp.text Imports iTextSharp.text.html.simpleparser Imports iTextSharp.text.pdf Imports ClosedXML.Excel Imports System.Data</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void ImportExcel(object sender, EventArgs e) { string filePath = Server.MapPath(&#34;~/Files/&#34;) + Path.GetFileName(FileUpload1.PostedFile.FileName); FileUpload1.SaveAs(filePath); using (XLWorkbook workBook = new XLWorkbook(filePath)) { IXLWorksheet workSheet = workBook.Worksheet(1); DataTable dt = new DataTable(); bool firstRow = true; foreach (IXLRow row in workSheet.Rows()) { if (firstRow) { foreach (IXLCell cell in row.Cells()) { dt.Columns.Add(cell.Value.ToString()); } firstRow = false; } else { dt.Rows.Add(); int i = 0; foreach (IXLCell cell in row.Cells()) { dt.Rows[dt.Rows.Count - 1][i] = cell.Value.ToString(); i++; } } } CreatePdf(dt); } } private void CreatePdf(DataTable dt) { Response.ContentType = &#34;application/pdf&#34;; Response.AddHeader(&#34;content-disposition&#34;, &#34;attachment;filename=DataTable.pdf&#34;); Response.Cache.SetCacheability(HttpCacheability.NoCache); StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); GridView gvCustomers = new GridView(); gvCustomers.DataSource = dt; gvCustomers.DataBind(); gvCustomers.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(); } public override void VerifyRenderingInServerForm(Control control) { /* Verifies that the control is rendered */ }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub ImportExcel(ByVal sender As Object, ByVal e As EventArgs) Dim filePath As String = Server.MapPath(&#34;~/Files/&#34;) + Path.GetFileName(FileUpload1.PostedFile.FileName) FileUpload1.SaveAs(filePath) Using workBook As XLWorkbook = New XLWorkbook(filePath) Dim workSheet As IXLWorksheet = workBook.Worksheet(1) Dim dt As DataTable = New DataTable() Dim firstRow As Boolean = True For Each row As IXLRow In workSheet.Rows() If firstRow Then For Each cell As IXLCell In row.Cells() dt.Columns.Add(cell.Value.ToString()) Next firstRow = False Else dt.Rows.Add() Dim i As Integer = 0 For Each cell As IXLCell In row.Cells() dt.Rows(dt.Rows.Count - 1)(i) = cell.Value.ToString() i += 1 Next End If Next CreatePdf(dt) End Using End Sub Private Sub CreatePdf(ByVal dt As DataTable) Response.ContentType = &#34;application/pdf&#34; Response.AddHeader(&#34;content-disposition&#34;, &#34;attachment;filename=DataTable.pdf&#34;) Response.Cache.SetCacheability(HttpCacheability.NoCache) Dim sw As StringWriter = New StringWriter() Dim hw As HtmlTextWriter = New HtmlTextWriter(sw) Dim gvCustomers As GridView = New GridView() gvCustomers.DataSource = dt gvCustomers.DataBind() gvCustomers.RenderControl(hw) Dim sr As StringReader = New StringReader(sw.ToString()) Dim pdfDoc As Document = New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 0.0F) Dim htmlparser As HTMLWorker = New HTMLWorker(pdfDoc) PdfWriter.GetInstance(pdfDoc, Response.OutputStream) pdfDoc.Open() htmlparser.Parse(sr) pdfDoc.Close() Response.Write(pdfDoc) Response.[End]() End Sub Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control) End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="" alt="" width="407" height="206" /></p>, 10 Jul 2018 00:37:08 GMT