Hi smile,
Refer below sample.
Namespaces
C#
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
using System.Linq;
VB.Net
Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.html.simpleparser
Imports iTextSharp.text.pdf
Imports System.Text
Code
C#
DataTable dt = new DataTable();
private void Form1_Load(object sender, EventArgs e)
{
dt.Columns.AddRange(new DataColumn[] {
new DataColumn("AdmissionNo", typeof(string)),
new DataColumn("HeadName", typeof(string)),
new DataColumn("Amount", typeof(int))
});
dt.Rows.Add("R-01", "AdmissionNo", 550);
dt.Rows.Add("R-01", "Tution Fees", 1100);
dt.Rows.Add("R-01", "Library Fees", 300);
dt.Rows.Add("R-02", "AdmissionNo", 123);
dt.Rows.Add("R-02", "Tution Fees", 400);
dt.Rows.Add("R-02", "Library Fees", 250);
dataGridView1.DataSource = dt;
}
private void ExportToPdf(object sender, EventArgs e)
{
string folderPath = @"C:\Users\anand\Desktop\Test\\";
FileStream stream = new FileStream(folderPath + "DataGridViewExport.pdf", FileMode.Create);
Document pdfDoc = new Document();
StringReader sr = null;
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, stream);
pdfDoc.Open();
var grouped = from x in dt.AsEnumerable()
group x by new { a = x["AdmissionNo"] } into g
select new
{
Value = g.Key,
ColumnValues = g
};
foreach (var key in grouped)
{
DataTable dtfinal = dt.Clone();
foreach (var columnValue in key.ColumnValues)
{
dtfinal.ImportRow(columnValue);
}
using (StringWriter sw = new StringWriter())
{
StringBuilder sb = new StringBuilder();
sb.Append("<table width='100%' cellspacing='0' cellpadding='2'>");
sb.Append("<tr><td align='center' colspan = '2'><b>Generate Report</b></td></tr>");
sb.Append("</td></tr>");
sb.Append("</table>");
sb.Append("<table border = '1'>");
sb.Append("<tr>");
foreach (DataColumn column in dtfinal.Columns)
{
sb.Append("<th>");
sb.Append(column.ColumnName);
sb.Append("</th>");
}
sb.Append("</tr>");
foreach (DataRow row in dtfinal.Rows)
{
sb.Append("<tr>");
foreach (DataColumn column in dtfinal.Columns)
{
sb.Append("<td>");
sb.Append(row[column]);
sb.Append("</td>");
}
sb.Append("</tr>");
}
sb.Append("<tr><td align = 'right' colspan = '");
sb.Append(dtfinal.Columns.Count - 1);
sb.Append("'>Total</td>");
sb.Append("<td>");
int sum = Convert.ToInt32(dtfinal.Compute("sum(Amount)", ""));
sb.Append(sum);
sb.Append("</td>");
sb.Append("</tr></table>");
sr = new StringReader(sb.ToString());
}
pdfDoc.NewPage();
htmlparser.Parse(sr);
}
pdfDoc.Close();
stream.Close();
}
VB.Net
Private dt As DataTable = New DataTable()
Private Sub ExportToPdf(sender As System.Object, e As System.EventArgs) Handles button1.Click
Dim folderPath As String = "C:\Users\anand\Desktop\Test\\"
Dim stream As FileStream = New FileStream(folderPath & "DataGridViewExport.pdf", FileMode.Create)
Dim pdfDoc As Document = New Document()
Dim sr As StringReader = Nothing
Dim htmlparser As HTMLWorker = New HTMLWorker(pdfDoc)
PdfWriter.GetInstance(pdfDoc, stream)
pdfDoc.Open()
Dim grouped = From x In dt.AsEnumerable()
Group x By Value = x.Field(Of String)("AdmissionNo") Into g = Group
Select New With {
.Value = Value,
.ColumnValues = g}
For Each key In grouped
Dim dtfinal As DataTable = dt.Clone()
For Each columnValue In key.ColumnValues
dtfinal.ImportRow(columnValue)
Next
Using sw As StringWriter = New StringWriter()
Dim sb As StringBuilder = New StringBuilder()
sb.Append("<table width='100%' cellspacing='0' cellpadding='2'>")
sb.Append("<tr><td align='center' colspan = '2'><b>Generate Report</b></td></tr>")
sb.Append("</td></tr>")
sb.Append("</table>")
sb.Append("<table border = '1'>")
sb.Append("<tr>")
For Each column As DataColumn In dtfinal.Columns
sb.Append("<th>")
sb.Append(column.ColumnName)
sb.Append("</th>")
Next
sb.Append("</tr>")
For Each row As DataRow In dtfinal.Rows
sb.Append("<tr>")
For Each column As DataColumn In dtfinal.Columns
sb.Append("<td>")
sb.Append(row(column))
sb.Append("</td>")
Next
sb.Append("</tr>")
Next
sb.Append("<tr><td align = 'right' colspan = '")
sb.Append(dtfinal.Columns.Count - 1)
sb.Append("'>Total</td>")
sb.Append("<td>")
Dim sum As Integer = Convert.ToInt32(dtfinal.Compute("sum(Amount)", ""))
sb.Append(sum)
sb.Append("</td>")
sb.Append("</tr></table>")
sr = New StringReader(sb.ToString())
End Using
pdfDoc.NewPage()
htmlparser.Parse(sr)
Next
pdfDoc.Close()
stream.Close()
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
dt.Columns.AddRange(New DataColumn() {New DataColumn("AdmissionNo", GetType(String)), New DataColumn("HeadName", GetType(String)), New DataColumn("Amount", GetType(Integer))})
dt.Rows.Add("R-01", "AdmissionNo", 550)
dt.Rows.Add("R-01", "Tution Fees", 1100)
dt.Rows.Add("R-01", "Library Fees", 300)
dt.Rows.Add("R-02", "AdmissionNo", 123)
dt.Rows.Add("R-02", "Tution Fees", 400)
dt.Rows.Add("R-02", "Library Fees", 250)
dataGridView1.DataSource = dt
End Sub
Screenshot