ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Export Each Excel row in separate PDF using C# and VB.Net in ASP.Net<p>Hi <a class="username" rel="BugHunter"> BugHunter</a>,</p> <p>Refer below sample.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;asp:FileUpload ID=&#34;FileUpload1&#34; runat=&#34;server&#34; /&gt; &lt;asp:Button ID=&#34;btnImport&#34; runat=&#34;server&#34; Text=&#34;Import&#34; OnClick=&#34;ImportExcel&#34; /&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; using System.Text;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports ClosedXML.Excel Imports System.Data Imports System.IO Imports iTextSharp.text Imports iTextSharp.text.html.simpleparser Imports iTextSharp.text.pdf</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 fileName = Path.GetFileName(FileUpload1.PostedFile.FileName); FileUpload1.SaveAs(Server.MapPath(&#34;~/Files/&#34;) + fileName); using (XLWorkbook workBook = new XLWorkbook(Server.MapPath(&#34;~/Files/&#34;) + fileName)) { 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++; } } } int loop = 0; foreach (DataRow row in dt.Rows) { using (StringWriter sw = new StringWriter()) { using (HtmlTextWriter hw = new HtmlTextWriter(sw)) { StringBuilder sb = new StringBuilder(); sb.Append(&#34;&lt;table border = &#39;1&#39;&gt;&#34;); sb.Append(&#34;&lt;tr&gt;&#34;); foreach (DataColumn column in dt.Columns) { sb.Append(&#34;&lt;th align=&#39;center&#39; style=&#39;font-weight: bold; color: Red;&#39;&gt;&#34;); sb.Append(column.ColumnName); sb.Append(&#34;&lt;/th&gt;&#34;); } sb.Append(&#34;&lt;/tr&gt;&#34;); sb.Append(&#34;&lt;tr&gt;&#34;); foreach (DataColumn column in dt.Columns) { sb.Append(&#34;&lt;td&gt;&#34;); sb.Append(row[column]); sb.Append(&#34;&lt;/td&gt;&#34;); } sb.Append(&#34;&lt;/tr&gt;&#34;); sb.Append(&#34;&lt;/table&gt;&#34;); StringReader sr = new StringReader(sb.ToString()); Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f); HTMLWorker htmlparser = new HTMLWorker(pdfDoc); PdfWriter writer = PdfWriter.GetInstance(pdfDoc, new FileStream(Server.MapPath(&#34;~/Files/&#34; + &#34;Customer&#34; + (loop + 1) + &#34;.pdf&#34;), FileMode.Create)); pdfDoc.Open(); htmlparser.Parse(sr); pdfDoc.Close(); } loop++; } } } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub ImportExcel(ByVal sender As Object, ByVal e As EventArgs) Dim fileName As String = Path.GetFileName(FileUpload1.PostedFile.FileName) FileUpload1.SaveAs(Server.MapPath(&#34;~/Files/&#34;) &amp; fileName) Using workBook As XLWorkbook = New XLWorkbook(Server.MapPath(&#34;~/Files/&#34;) &amp; fileName) 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 Dim [loop] As Integer = 0 For Each row As DataRow In dt.Rows Using sw As StringWriter = New StringWriter() Using hw As HtmlTextWriter = New HtmlTextWriter(sw) Dim sb As StringBuilder = New StringBuilder() sb.Append(&#34;&lt;table border = &#39;1&#39;&gt;&#34;) sb.Append(&#34;&lt;tr&gt;&#34;) For Each column As DataColumn In dt.Columns sb.Append(&#34;&lt;th align=&#39;center&#39; style=&#39;font-weight: bold; color: Red;&#39;&gt;&#34;) sb.Append(column.ColumnName) sb.Append(&#34;&lt;/th&gt;&#34;) Next sb.Append(&#34;&lt;/tr&gt;&#34;) sb.Append(&#34;&lt;tr&gt;&#34;) For Each column As DataColumn In dt.Columns sb.Append(&#34;&lt;td&gt;&#34;) sb.Append(row(column)) sb.Append(&#34;&lt;/td&gt;&#34;) Next sb.Append(&#34;&lt;/tr&gt;&#34;) sb.Append(&#34;&lt;/table&gt;&#34;) Dim sr As StringReader = New StringReader(sb.ToString()) Dim pdfDoc As Document = New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 0.0F) Dim htmlparser As HTMLWorker = New HTMLWorker(pdfDoc) Dim writer As PdfWriter = PdfWriter.GetInstance(pdfDoc, New FileStream(Server.MapPath(&#34;~/Files/&#34; &amp; &#34;Customer&#34; &amp; ([loop] + 1) &amp; &#34;.pdf&#34;), FileMode.Create)) pdfDoc.Open() htmlparser.Parse(sr) pdfDoc.Close() End Using [loop] += 1 End Using Next End Using End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><strong>Excel</strong></p> <p><img src="https://imgur.com/qPrfcXt.jpg" alt="" width="291" height="222" /></p> <p><strong>PDF</strong></p> <p><strong><img src="https://imgur.com/7OT9DHP.jpg" alt="" width="524" height="577" /></strong></p>https://www.aspforums.net:443/Threads/116394/Export-Each-Excel-row-in-separate-PDF-using-C-and-VBNet-in-ASPNet/https://www.aspforums.net:443/Threads/116394/Export-Each-Excel-row-in-separate-PDF-using-C-and-VBNet-in-ASPNet/Tue, 19 Mar 2019 03:50:23 GMT