大家好:我是你们的好朋友铭文。好久没有分享博文了,今天来分享一个博客博文哈。是一个简单的数据导出给前端字节数据,以前都是在服务器生成的,这次需求不太一样希望可以给大家有点帮助把。
一.这个表格给前端返回的是中国式复杂表格有合并等需求
ps:这个表格我们生成用到了 iTextSharp 组件
官方知识库搜索: iText Knowledge Base https://kb.itextpdf.com/home
合并的效果图:
直接上代码了:
private static byte[] CreatePDF(List<StudentDetailsexport> dtSource, List<StudentDetailListsexport> dtSourceDetai)
{
try
{
Document doc = new Document(PageSize.A4);//定义一个Document,并设置页面大小为A4,竖向
MemoryStream pdfMS = new MemoryStream();
PdfWriter.GetInstance(doc, pdfMS);//PDF内容放入到流中
doc.Open();
float[] colSet = { 40, 100, 100, 90, 90, 90, 90 };//设定每列宽度
PdfPTable pdfTable = new PdfPTable(colSet.Length); //初始化列数
pdfTable.SetWidthPercentage(colSet, PageSize.A4);
pdfTable.NormalizeHeadersFooters();
pdfTable.SplitLate = false;
PdfPCell pdfPCell;//创建单元格
BaseFont baseFont = BaseFont.CreateFont("C://WINDOWS//Fonts//simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//宋体
BaseFont titleBaseFont = BaseFont.CreateFont("C://WINDOWS//Fonts//simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//黑体
iTextSharp.text.Font titleFont = new iTextSharp.text.Font(titleBaseFont, 7);//标题字体
iTextSharp.text.Font contentFont = new iTextSharp.text.Font(baseFont, 6);//正文字体
int horizontalAlignment = Element.ALIGN_CENTER; //水平居中(单元格文字位置)
int verticalAlignment = Element.ALIGN_MIDDLE; //垂直居中(单元格文字位置)
int leftHAlignment = Element.ALIGN_LEFT;//水平居左
#region 创建Table
//标题
pdfPCell = new PdfPCell(new Phrase("表名", titleFont));
pdfPCell.FixedHeight = 20F;
pdfPCell.Colspan = 7;//合并单元格,默认为1
pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
//pdfPCell.BackgroundColor = new BaseColor(0, 102, 204); //单元格背景颜色
pdfPCell.HorizontalAlignment = horizontalAlignment;
pdfPCell.VerticalAlignment = verticalAlignment;
pdfTable.AddCell(pdfPCell);
//Table列标题
pdfPCell = new PdfPCell(new Phrase("基本信息", titleFont));
pdfPCell.Rowspan = 3;
pdfPCell.FixedHeight = 20F;
pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
//pdfPCell.BackgroundColor = new BaseColor(0, 102, 204); //单元格背景颜色
pdfPCell.HorizontalAlignment = horizontalAlignment;
pdfPCell.VerticalAlignment = verticalAlignment;
pdfTable.AddCell(pdfPCell);
//Table列标题
pdfPCell = new PdfPCell(new Phrase("姓名", titleFont));
pdfPCell.FixedHeight = 20F;
pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
//pdfPCell.BackgroundColor = new BaseColor(0, 102, 204); //单元格背景颜色
pdfPCell.HorizontalAlignment = horizontalAlignment;
pdfPCell.VerticalAlignment = verticalAlignment;
pdfTable.AddCell(pdfPCell);
//对应名字
pdfPCell = new PdfPCell(new Phrase(dtSource?.Count > 0 ? dtSource?.FirstOrDefault().Name : "", contentFont));
pdfPCell.FixedHeight = 20F;
pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
pdfPCell.VerticalAlignment = verticalAlignment;
pdfPCell.HorizontalAlignment = leftHAlignment;
pdfTable.AddCell(pdfPCell);
pdfPCell = new PdfPCell(new Phrase("性别", titleFont));
pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
pdfPCell.FixedHeight = 20F;
//pdfPCell.BackgroundColor = new BaseColor(0, 102, 204); //单元格背景颜色
pdfPCell.HorizontalAlignment = horizontalAlignment;
pdfPCell.VerticalAlignment = verticalAlignment;
pdfTable.AddCell(pdfPCell);
//性别
pdfPCell = new PdfPCell(new Phrase(dtSource?.Count > 0 ? dtSource?.FirstOrDefault().Sex : "", contentFont));
pdfPCell.FixedHeight = 20F;
pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
pdfPCell.HorizontalAlignment = leftHAlignment;
pdfPCell.VerticalAlignment = verticalAlignment;
pdfTable.AddCell(pdfPCell);
pdfPCell = new PdfPCell(new Phrase("电话", titleFont));
pdfPCell.FixedHeight = 20F;
pdfPCell.Colspan = 1;
pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
pdfPCell.HorizontalAlignment = horizontalAlignment;
pdfPCell.FixedHeight = 20F;
pdfPCell.VerticalAlignment = verticalAlignment;
pdfTable.AddCell(pdfPCell);
pdfPCell = new PdfPCell(new Phrase(dtSource?.Count > 0 ? dtSource?.FirstOrDefault().PhoneNumber : "", contentFont));
pdfPCell.VerticalAlignment = verticalAlignment;
pdfPCell.HorizontalAlignment = horizontalAlignment;
pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
pdfPCell.HorizontalAlignment = leftHAlignment;
pdfTable.AddCell(pdfPCell);
pdfPCell = new PdfPCell(new Phrase("身份证号码", titleFont));
pdfPCell.FixedHeight = 20F;
pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
//pdfPCell.BackgroundColor = new BaseColor(0, 102, 204); //单元格背景颜色
pdfPCell.HorizontalAlignment = horizontalAlignment;
pdfPCell.FixedHeight = 20F;
pdfPCell.VerticalAlignment = verticalAlignment;
pdfTable.AddCell(pdfPCell);
pdfPCell = new PdfPCell(new Phrase(dtSource?.Count > 0 ? dtSource?.FirstOrDefault().IdNumber : "", contentFont));
pdfPCell.VerticalAlignment = verticalAlignment;
pdfPCell.FixedHeight = 20F;
pdfPCell.Colspan = 2;
pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
pdfPCell.HorizontalAlignment = leftHAlignment;
pdfTable.AddCell(pdfPCell);
pdfPCell = new PdfPCell(new Phrase("列名", titleFont));
pdfPCell.FixedHeight = 20F;
pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
//pdfPCell.BackgroundColor = new BaseColor(0, 102, 204); //单元格背景颜色
pdfPCell.HorizontalAlignment = horizontalAlignment;
pdfPCell.VerticalAlignment = verticalAlignment;
pdfTable.AddCell(pdfPCell);
pdfPCell = new PdfPCell(new Phrase(dtSource?.Count > 0 ? dtSource?.FirstOrDefault().NametrainingPlan : "", contentFont));
pdfPCell.VerticalAlignment = verticalAlignment;
pdfPCell.FixedHeight = 20F;
pdfPCell.Colspan = 2;
pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
pdfPCell.HorizontalAlignment = leftHAlignment;
pdfTable.AddCell(pdfPCell);
pdfPCell = new PdfPCell(new Phrase("列名2", titleFont));
pdfPCell.FixedHeight = 20F;
pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
//pdfPCell.BackgroundColor = new BaseColor(0, 102, 204); //单元格背景颜色
pdfPCell.HorizontalAlignment = horizontalAlignment;
pdfPCell.VerticalAlignment = verticalAlignment;
pdfTable.AddCell(pdfPCell);
pdfPCell = new PdfPCell(new Phrase(dtSource?.Count > 0 ? dtSource?.FirstOrDefault().Company : "", contentFont));
pdfPCell.VerticalAlignment = verticalAlignment;
pdfPCell.FixedHeight = 20F;
pdfPCell.Colspan = 2;
pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
pdfPCell.HorizontalAlignment = leftHAlignment;
pdfTable.AddCell(pdfPCell);
pdfPCell = new PdfPCell(new Phrase("列名3", titleFont));
pdfPCell.FixedHeight = 20F;
pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
//pdfPCell.BackgroundColor = new BaseColor(0, 102, 204); //单元格背景颜色
pdfPCell.HorizontalAlignment = horizontalAlignment;
pdfPCell.VerticalAlignment = verticalAlignment;
pdfTable.AddCell(pdfPCell);
pdfPCell = new PdfPCell(new Phrase(dtSource?.Count > 0 ? dtSource?.FirstOrDefault().TrainingMethods : "", contentFont));
pdfPCell.VerticalAlignment = verticalAlignment;
pdfPCell.FixedHeight = 20F;
pdfPCell.Colspan = 2;
pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
pdfPCell.HorizontalAlignment = leftHAlignment;
pdfTable.AddCell(pdfPCell);
pdfPCell = new PdfPCell(new Phrase("学时记录", titleFont));
pdfPCell.FixedHeight = 20F;
pdfPCell.Rowspan = dtSourceDetai.Count + 2;
pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
//pdfPCell.BackgroundColor = new BaseColor(0, 102, 204); //单元格背景颜色
pdfPCell.HorizontalAlignment = horizontalAlignment;
pdfPCell.VerticalAlignment = verticalAlignment;
pdfTable.AddCell(pdfPCell);
pdfPCell = new PdfPCell(new Phrase("序号", titleFont));
pdfPCell.FixedHeight = 20F;
pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
//pdfPCell.BackgroundColor = new BaseColor(0, 102, 204); //单元格背景颜色
pdfPCell.HorizontalAlignment = horizontalAlignment;
pdfPCell.VerticalAlignment = verticalAlignment;
pdfTable.AddCell(pdfPCell);
pdfPCell = new PdfPCell(new Phrase("列名4", titleFont));
pdfPCell.FixedHeight = 20F;
pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
//pdfPCell.BackgroundColor = new BaseColor(0, 102, 204); //单元格背景颜色
pdfPCell.HorizontalAlignment = horizontalAlignment;
pdfPCell.VerticalAlignment = verticalAlignment;
pdfTable.AddCell(pdfPCell);
pdfPCell = new PdfPCell(new Phrase("列名5", titleFont));
pdfPCell.FixedHeight = 20F;
pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
//pdfPCell.BackgroundColor = new BaseColor(0, 102, 204); //单元格背景颜色
pdfPCell.HorizontalAlignment = horizontalAlignment;
pdfPCell.VerticalAlignment = verticalAlignment;
pdfTable.AddCell(pdfPCell);
pdfPCell = new PdfPCell(new Phrase("列名6", titleFont));
pdfPCell.FixedHeight = 20F;
pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
//pdfPCell.BackgroundColor = new BaseColor(0, 102, 204); //单元格背景颜色
pdfPCell.HorizontalAlignment = horizontalAlignment;
pdfPCell.VerticalAlignment = verticalAlignment;
pdfTable.AddCell(pdfPCell);
pdfPCell = new PdfPCell(new Phrase("列名7", titleFont));
pdfPCell.FixedHeight = 20F;
pdfPCell.Colspan = 2;
pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
//pdfPCell.BackgroundColor = new BaseColor(0, 102, 204); //单元格背景颜色
pdfPCell.HorizontalAlignment = horizontalAlignment;
pdfPCell.VerticalAlignment = verticalAlignment;
pdfTable.AddCell(pdfPCell);
int sortNo = 0;//序号
//表格行数据
foreach (StudentDetailListsexport dr in dtSourceDetai)
{
//序号
sortNo++;
pdfPCell = new PdfPCell(new Phrase(sortNo.ToString(), contentFont));
pdfPCell.VerticalAlignment = verticalAlignment;
pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
pdfPCell.FixedHeight = 20F;
pdfPCell.HorizontalAlignment = horizontalAlignment;
pdfTable.AddCell(pdfPCell);
pdfPCell = new PdfPCell(new Phrase(dr.CreationTime?.ToString("yyyy/MM/dd") + "-" + dr.LastUpdateTime?.ToString("yyyy/MM/dd"), contentFont));
pdfPCell.VerticalAlignment = verticalAlignment;
pdfPCell.HorizontalAlignment = horizontalAlignment;
pdfPCell.Colspan = 1;
pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
pdfPCell.FixedHeight = 20F;
pdfTable.AddCell(pdfPCell);
pdfPCell = new PdfPCell(new Phrase(dr.TrainingContent.ToString(), contentFont));
pdfPCell.VerticalAlignment = verticalAlignment;
pdfPCell.HorizontalAlignment = horizontalAlignment;
pdfPCell.FixedHeight = 20F;
pdfPCell.Colspan = 1;
pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
pdfTable.AddCell(pdfPCell);
pdfPCell = new PdfPCell(new Phrase(dr.ClassHours.ToString(), contentFont));
pdfPCell.VerticalAlignment = verticalAlignment;
pdfPCell.HorizontalAlignment = horizontalAlignment;
pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
pdfPCell.FixedHeight = 20F;
pdfTable.AddCell(pdfPCell);
//列名7
pdfPCell = new PdfPCell(new Phrase(dr.Remarks.ToString(), contentFont));
pdfPCell.VerticalAlignment = verticalAlignment;
pdfPCell.Colspan = 2;
pdfPCell.HorizontalAlignment = horizontalAlignment;
pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
pdfTable.AddCell(pdfPCell);
}
#endregion
pdfPCell = new PdfPCell(new Phrase("列名8", contentFont));
pdfPCell.FixedHeight = 20F;
pdfPCell.Colspan = 3;
pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
pdfPCell.HorizontalAlignment = horizontalAlignment;
pdfPCell.VerticalAlignment = verticalAlignment;
pdfTable.AddCell(pdfPCell);
pdfPCell = new PdfPCell(new Phrase(dtSourceDetai.Sum(P => P.ClassHours).ToString(), contentFont));
pdfPCell.FixedHeight = 20F;
pdfPCell.Colspan = 1;
pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
pdfPCell.HorizontalAlignment = horizontalAlignment;
pdfPCell.VerticalAlignment = verticalAlignment;
pdfTable.AddCell(pdfPCell);
pdfPCell = new PdfPCell(new Phrase("", contentFont));
pdfPCell.FixedHeight = 20F;
pdfPCell.Colspan = 2;
pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
pdfPCell.HorizontalAlignment = horizontalAlignment;
pdfPCell.VerticalAlignment = verticalAlignment;
pdfTable.AddCell(pdfPCell);
#region 插入图片
//pdfPCell = new PdfPCell();
//pdfPCell.Border = 0;
//pdfPCell.Colspan = 10;
//pdfPCell.HorizontalAlignment = leftHAlignment;
//iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance("D:\\test.jpg");
//pdfPCell.Image = img; //图片
//pdfTable.AddCell(pdfPCell);
#endregion
doc.Add(pdfTable);
doc.NewPage();//分页
doc.Close();
//保存pdf文件
byte[] fileByte = pdfMS.GetBuffer();
//string fileName = "D:\\" + Guid.NewGuid().ToString() + ".pdf";
//FileStream fs = new FileStream(fileName, FileMode.Create);
//BinaryWriter bw = new BinaryWriter(fs);
//bw.Write(fileByte);
//bw.Close();
//fs.Close();
return fileByte;
}
catch
{
return null;
}
}
大家有不明白的可以留言哈,或者私信呀。 看到会回复的。