c# 使用 itextsharp 实现生成Pdf报表
由于項目需要,所以學(xué)習(xí)Itextsharp? ?此項目需求是? ?某一角色提交申請,然后從后臺查出數(shù)據(jù)生成pdf報表
打印出來用于查看 以下是代碼:
?
string sql = "select * from table " // sql 查詢數(shù)據(jù)?
DataTable dt = Medalsoft.ServiceMe.ClassLibrary.SQLHelper.ExecuteDt(sql);
if (dt.Rows.Count == 0) //對查詢結(jié)果做判斷 沒有數(shù)據(jù)就不生成PDF
{
return "dataNull";
}
Stream myStream;//文件流
string PdfFilePath = "/costPdf";? 這是存放的文件夾名稱? 服務(wù)器中的
string temppath = HttpContext.Current.Server.MapPath(PdfFilePath);
if (System.IO.Directory.Exists(temppath) == false)//如果不存在就創(chuàng)建文件夾
{
System.IO.Directory.CreateDirectory(HttpContext.Current.Server.MapPath(PdfFilePath));
}
string finalPath = PdfFilePath + "/" + "expense.pdf";//獲取保存后的路徑? (服務(wù)器中)
string pdfPath = HttpContext.Current.Server.MapPath(PdfFilePath) + "/" + "expense.pdf"; //物理文件路徑?
if(System.IO.Directory.Exists(pdfPath) == true)? //文件已存在就刪除? 以此達到覆蓋文件的效果
{
File.Delete(pdfPath);
}
myStream = new FileStream(pdfPath, FileMode.OpenOrCreate); //打開文件并賦給IO流myStream
//string imgPath = HttpContext.Current.Server.MapPath(PdfFilePath) + "/" + "shade.png";//圖片路徑 陰影圖片 填充不需要的單元格
//iTextSharp.text.Image img1 = iTextSharp.text.Image.GetInstance(imgPath);
Document document = new Document(PageSize.A4.Rotate()); //創(chuàng)建A4紙、橫向PDF文檔 默認(rèn)縱向
PdfWriter writer = PdfWriter.GetInstance(document, myStream); //將PDF文檔寫入創(chuàng)建的文件中
document.Open();
//要在PDF文檔中寫入中文必須指定中文字體,否則無法寫入中文
BaseFont bftitle = BaseFont.CreateFont("C:\\Windows\\Fonts\\SIMHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); //用系統(tǒng)中的字體文件SimHei.ttf創(chuàng)建文件字體
iTextSharp.text.Font fonttitle = new iTextSharp.text.Font(bftitle, 20); //標(biāo)題字體,大小30
BaseFont bf1 = BaseFont.CreateFont("C:\\Windows\\Fonts\\SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); //用系統(tǒng)中的字體文件SimSun.ttc創(chuàng)建文件字體
iTextSharp.text.Font CellFont = new iTextSharp.text.Font(bf1, 12); //單元格中的字體,大小12
iTextSharp.text.Font fonttitle2 = new iTextSharp.text.Font(bf1, 15); //副標(biāo)題字體,大小15
//添加標(biāo)題
Paragraph Title = new Paragraph("上海長瀨貿(mào)易有限公司費用報銷憑證", fonttitle); //添加段落,第二個參數(shù)指定使用fonttitle格式的字體,寫入中文必須指定字體否則無法顯示中文
Title.Alignment = iTextSharp.text.Rectangle.ALIGN_CENTER; //設(shè)置居中
document.Add(Title); //將標(biāo)題段加入PDF文檔中
//空一行
Paragraph null1 = new Paragraph(" ", fonttitle2);
null1.Leading = 20;? //此數(shù)值用于調(diào)整空白大小
document.Add(null1);
List<string> list = new List<string>();
double num1, num2, num3, num4, num5;
double sum = 0;
for (int i = 0; i < dt.Rows.Count; i++)
{
string date = dt.Rows[i]["字段名"].ToString().Substring(0, 10);
list.Add(date);
string dept = dt.Rows[i]["字段名"].ToString();
list.Add(dept);
?
?
}
Chunk underline = new Chunk(" ");
underline.SetUnderline(0.1f, -1f);
Chunk underline1 = new Chunk(" ");
underline1.SetUnderline(0.1f, -1f);
?Paragraph apply = new Paragraph("申請日:", fonttitle2);
apply.Leading = 10;
apply.FirstLineIndent = 75;
apply.Add(underline);
apply.Add(new Chunk("年"));
apply.Add(underline);
apply.Add(new Chunk("月"));
apply.Add(underline);
apply.Add(new Chunk("日 申請人:"));
apply.Add(underline1);
document.Add(apply);
//上面這一段主要是添加下劃線 達到“申請日_______年______月________日”的效果? 由于使用輸入法的話打印出來會有間隔,所以使用這種方法
//空一行
Paragraph nulll = new Paragraph(" ", fonttitle2);
nulll.Leading = 20;
document.Add(nulll);
PdfPTable table = new PdfPTable(xxx); // 括號中 為用戶設(shè)置的列數(shù),創(chuàng)建xxx列的表格,行會根據(jù)寫入數(shù)據(jù)自動擴展
table.SetWidths(new int []) ;//表格每列寬度? 參數(shù)為數(shù)組
table.WidthPercentage = 100;//表格總寬度100%
string[] tableHeader = { "字段名", "字段名", };//此處以兩個為例??
PdfPCell cell1;
foreach (string th in tableHeader)
{
cell1 = new PdfPCell(new Phrase(th, CellFont));
cell1.HorizontalAlignment = Element.ALIGN_CENTER;//單元格水平居中
cell1.VerticalAlignment = Element.ALIGN_MIDDLE;//單元格垂直居中
?cel.MinimumHeight = 44; //設(shè)置單元格高度
table.AddCell(cell1);
}
foreach (string td in list)
{
cell1 = new PdfPCell(new Phrase(td, CellFont));
cell1.HorizontalAlignment = Element.ALIGN_CENTER;//單元格水平居中
cell1.VerticalAlignment = Element.ALIGN_MIDDLE;//單元格垂直居中
?cel.MinimumHeight = 44;
table.AddCell(cell1);
}
//表格最后一行
PdfPCell cell;
cell = new PdfPCell(new Phrase("合計", CellFont));
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.Colspan = 2;//單元格合并列
table.AddCell(cell);
PdfPCell cel2 = new PdfPCell();
cel2.BackgroundColor = new BaseColor(128, 128, 128); //這里是設(shè)置單元格背景色? 表示一些cell 不需要填數(shù)據(jù)
for (int i = 0; i < 5; i++)
{
table.AddCell(cel2);
}
PdfPCell cel;
cel = new PdfPCell(new Phrase(sum.ToString("f2"), CellFont));
cel.VerticalAlignment = Element.ALIGN_MIDDLE;
cel.HorizontalAlignment = Element.ALIGN_CENTER;
table.AddCell(cel);
PdfPCell cel3 = new PdfPCell(new Phrase(" "));
cel3.BackgroundColor = new BaseColor(128,128,128);
table.AddCell(cel3);
document.Add(table);
PdfPTable table1 = new PdfPTable(4);
//空一行
Paragraph null2 = new Paragraph(" ", fonttitle2);
null2.Leading = 10;
document.Add(null2);
PdfPCell cell2;
string[] tableHeader1 = { "1 (COO)", "2 (TRE)", "3 (財務(wù))", "4" };
string[] result = { "審批:", "審批:", "審核:", "領(lǐng)款人:" };//此數(shù)組需要拼接數(shù)據(jù)
foreach (string th1 in tableHeader1)
{
cell2 = new PdfPCell(new Phrase(th1, CellFont));
cell2.HorizontalAlignment = Element.ALIGN_CENTER;
cell2.VerticalAlignment = Element.ALIGN_MIDDLE;
table1.AddCell(cell2);
}
PdfPCell cell3;
foreach (string th1 in result)
{
cell3 = new PdfPCell(new Phrase(th1, CellFont));
cell3.VerticalAlignment = Element.ALIGN_MIDDLE;
cell3.MinimumHeight = 40;
table1.AddCell(cell3);
}
document.Add(table1);
//空一行
Paragraph null3 = new Paragraph(" ", fonttitle2);
null3.Leading = 10;
document.Add(null3);
Paragraph remark = new Paragraph("備注:3000元以上必須COO審批。", CellFont);
remark.Leading = 10;
remark.FirstLineIndent = 75;
document.Add(remark);
?
document.Close(); myStream.Close();
轉(zhuǎn)載于:https://www.cnblogs.com/yuan-J/p/7510626.html
總結(jié)
以上是生活随笔為你收集整理的c# 使用 itextsharp 实现生成Pdf报表的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据结构之堆的插入、取值、排序(细致讲解
- 下一篇: 【笔记】shellcode相关整理