webapi 接口返回生成中国式 复杂表格,字节数据

大家好:我是你们的好朋友铭文。好久没有分享博文了,今天来分享一个博客博文哈。是一个简单的数据导出给前端字节数据,以前都是在服务器生成的,这次需求不太一样希望可以给大家有点帮助把。

一.这个表格给前端返回的是中国式复杂表格有合并等需求

    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;
            }
        }

大家有不明白的可以留言哈,或者私信呀。 看到会回复的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一个武术猴子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值