本文主要是介绍C#用iTextSharp导出PDF到页面,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
using iTextSharp.text;
using iTextSharp.text.pdf;//导出PDFpublic ActionResult ImportFile(string date){try{List<IncomingHeatVal> list = null;string typeData = "日";string typeData2 = "月";DateTime dateTT;try{dateTT = Convert.ToDateTime(date);}catch (Exception){return Content("<script>alert('日期格式不正确');</script>");}//导出日报 string date1 = dateTT.ToString("yyyyMM") + "01";string date2 = Convert.ToDateTime(date).ToString("yyyyMMdd");list = new IncomingHeatDao().GetReportData(date1, date2); //字体读取的是windows系统宋体BaseFont basefont = BaseFont.CreateFont("C:/Windows/Fonts/simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);iTextSharp.text.Font font = new Font(basefont, 10);//设置字体为宋体和大小iTextSharp.text.Font font2 = new Font(basefont, 9);//设置字体为宋体和大小//font.Color = BaseColor.BLUE;//字体颜色iTextSharp.text.Document document = new iTextSharp.text.Document(PageSize.A4.Rotate(), 15, 15, 15, 15);System.IO.MemoryStream stream = new System.IO.MemoryStream();PdfWriter.GetInstance(document, stream);document.Open();document.AddTitle("入厂入炉热值差" + typeData + "报表");Paragraph element = new Paragraph("入厂入炉热值差" + typeData + "报表", new Font(basefont, 16));element.SpacingAfter = 10; //设置离后面内容的间距element.Alignment = Element.ALIGN_CENTER;document.Add(element);//打印时间Paragraph element2 = new Paragraph("打印时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm"), new Font(basefont, 6));element2.SpacingAfter = 5; //设置离后面内容的间距 document.Add(element2);PdfPTable table = new PdfPTable(15);table.WidthPercentage = 100;//设置表格宽度占用百分比 //table.SetTotalWidth(new float[] { 90f, 90f, 150f, 90f, 150f, 90f, 150f, 90f, 150f, 90f, 150f, 90f, 150f, 150f, 150f });#region 表头//第一行表头PdfPCell cell = new PdfPCell(new Paragraph("时间", font));cell.Rowspan = 3;cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE; //垂直居中cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;//水平居中table.AddCell(cell);cell = new PdfPCell(new Paragraph("入厂煤", font));cell.Colspan = 4; //定义一个表格单元的跨度cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE; //垂直居中cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;//水平居中table.AddCell(cell);cell = new PdfPCell(new Paragraph("入炉煤", font));cell.Colspan = 8;//定义一个表格单元的跨度cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE; //垂直居中cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;//水平居中table.AddCell(cell);cell = new PdfPCell(new Paragraph("入厂入炉热值差", font));cell.Colspan = 2;//定义一个表格单元的跨度cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE; //垂直居中cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;//水平居中//cell.FixedHeight = 130f;//设置高度 table.AddCell(cell);//第二行cell = new PdfPCell(new Paragraph("全厂当" + typeData, font));cell.Colspan = 2;//定义一个表格单元的跨度cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE; //垂直居中cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;//水平居中table.AddCell(cell);cell = new PdfPCell(new Paragraph("全厂" + typeData2 + "累计", font));cell.Colspan = 2;//定义一个表格单元的跨度cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE; //垂直居中cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;//水平居中table.AddCell(cell);cell = new PdfPCell(new Paragraph("一二期入炉", font));cell.Colspan = 2;//定义一个表格单元的跨度cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE; //垂直居中cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;//水平居中table.AddCell(cell);cell = new PdfPCell(new Paragraph("三期入炉", font));cell.Colspan = 2;//定义一个表格单元的跨度cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE; //垂直居中cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;//水平居中table.AddCell(cell);cell = new PdfPCell(new Paragraph("全厂当" + typeData, font));cell.Colspan = 2;//定义一个表格单元的跨度cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE; //垂直居中cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;//水平居中table.AddCell(cell);cell = new PdfPCell(new Paragraph("全厂" + typeData2 + "累计", font));cell.Colspan = 2;//定义一个表格单元的跨度cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE; //垂直居中cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;//水平居中table.AddCell(cell);cell = new PdfPCell(new Paragraph("全厂当" + typeData, font));cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE; //垂直居中cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;//水平居中table.AddCell(cell);cell = new PdfPCell(new Paragraph("全厂" + typeData2 + "累计", font));cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE; //垂直居中cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;//水平居中table.AddCell(cell);string[] titleArr = { "吨量(t)", "热值(kj/kg)", "吨量(t)", "热值(kj/kg)", "吨量(t)", "热值(kj/kg)", "吨量(t)", "热值(kj/kg)", "吨量(t)", "热值(kj/kg)", "吨量(t)", "热值(kj/kg)", "热值(kj/kg)", "热值(kj/kg)" };foreach (var item in titleArr){cell = new PdfPCell(new Paragraph(item, font2));cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE; //垂直居中cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;//水平居中table.AddCell(cell);}#endregion#region 数据载入foreach (var item in list){PdfPCell cell_data = new PdfPCell(new Paragraph(item.date, font));//时间cell_data.VerticalAlignment = PdfPCell.ALIGN_MIDDLE; //垂直居中cell_data.HorizontalAlignment = PdfPCell.ALIGN_CENTER;//水平居中table.AddCell(cell_data);cell_data = new PdfPCell(new Paragraph(item.qcdd_wt.ToString(), font));//全厂当日吨量cell_data.VerticalAlignment = PdfPCell.ALIGN_MIDDLE; //垂直居中cell_data.HorizontalAlignment = PdfPCell.ALIGN_CENTER;//水平居中table.AddCell(cell_data);cell_data = new PdfPCell(new Paragraph(item.qcdd_qr.ToString(), font));//全厂当日热值cell_data.VerticalAlignment = PdfPCell.ALIGN_MIDDLE; //垂直居中cell_data.HorizontalAlignment = PdfPCell.ALIGN_CENTER;//水平居中table.AddCell(cell_data);cell_data = new PdfPCell(new Paragraph(item.qcmmsum_wt.ToString(), font));//全厂当月累计吨量cell_data.VerticalAlignment = PdfPCell.ALIGN_MIDDLE; //垂直居中cell_data.HorizontalAlignment = PdfPCell.ALIGN_CENTER;//水平居中table.AddCell(cell_data);cell_data = new PdfPCell(new Paragraph(item.qcmmsum_qr.ToString(), font));//全厂当月累计热值cell_data.VerticalAlignment = PdfPCell.ALIGN_MIDDLE; //垂直居中cell_data.HorizontalAlignment = PdfPCell.ALIGN_CENTER;//水平居中table.AddCell(cell_data);cell_data = new PdfPCell(new Paragraph(item.rulu12_wt.ToString(), font));//一二期入炉吨量cell_data.VerticalAlignment = PdfPCell.ALIGN_MIDDLE; //垂直居中cell_data.HorizontalAlignment = PdfPCell.ALIGN_CENTER;//水平居中table.AddCell(cell_data);cell_data = new PdfPCell(new Paragraph(item.rulu12_qr.ToString(), font));//一二期入炉热值cell_data.VerticalAlignment = PdfPCell.ALIGN_MIDDLE; //垂直居中cell_data.HorizontalAlignment = PdfPCell.ALIGN_CENTER;//水平居中table.AddCell(cell_data);cell_data = new PdfPCell(new Paragraph(item.rulu3_wt.ToString(), font));//三期入炉吨量cell_data.VerticalAlignment = PdfPCell.ALIGN_MIDDLE; //垂直居中cell_data.HorizontalAlignment = PdfPCell.ALIGN_CENTER;//水平居中table.AddCell(cell_data);cell_data = new PdfPCell(new Paragraph(item.rulu3_qr.ToString(), font));//三期入炉热值cell_data.VerticalAlignment = PdfPCell.ALIGN_MIDDLE; //垂直居中cell_data.HorizontalAlignment = PdfPCell.ALIGN_CENTER;//水平居中table.AddCell(cell_data);cell_data = new PdfPCell(new Paragraph(item.ruluqcddsum_wt.ToString(), font));//全厂当日入炉吨量cell_data.VerticalAlignment = PdfPCell.ALIGN_MIDDLE; //垂直居中cell_data.HorizontalAlignment = PdfPCell.ALIGN_CENTER;//水平居中table.AddCell(cell_data);cell_data = new PdfPCell(new Paragraph(item.ruluqcddsum_qr.ToString(), font));//全厂当日入炉热值cell_data.VerticalAlignment = PdfPCell.ALIGN_MIDDLE; //垂直居中cell_data.HorizontalAlignment = PdfPCell.ALIGN_CENTER;//水平居中table.AddCell(cell_data);cell_data = new PdfPCell(new Paragraph(item.ruluqcmmsum_wt.ToString(), font));//全厂月累计入炉吨量cell_data.VerticalAlignment = PdfPCell.ALIGN_MIDDLE; //垂直居中cell_data.HorizontalAlignment = PdfPCell.ALIGN_CENTER;//水平居中table.AddCell(cell_data);cell_data = new PdfPCell(new Paragraph(item.ruluqcmmsum_qr.ToString(), font));//全厂月累计入炉热值cell_data.VerticalAlignment = PdfPCell.ALIGN_MIDDLE; //垂直居中cell_data.HorizontalAlignment = PdfPCell.ALIGN_CENTER;//水平居中table.AddCell(cell_data);cell_data = new PdfPCell(new Paragraph(item.qccha_qr1.ToString(), font));//入厂入炉热值差,全厂当日cell_data.VerticalAlignment = PdfPCell.ALIGN_MIDDLE; //垂直居中cell_data.HorizontalAlignment = PdfPCell.ALIGN_CENTER;//水平居中table.AddCell(cell_data);cell_data = new PdfPCell(new Paragraph(item.qccha_qr2.ToString(), font));//入厂入炉热值差,全厂月累计cell_data.VerticalAlignment = PdfPCell.ALIGN_MIDDLE; //垂直居中cell_data.HorizontalAlignment = PdfPCell.ALIGN_CENTER;//水平居中table.AddCell(cell_data);}#endregiondocument.Add(table);document.Close();byte[] fileBytes = stream.GetBuffer();stream.Close();stream.Dispose();return File(fileBytes, "application/pdf", DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf");}catch (Exception ex){string msg = string.Empty;if (ex.InnerException != null){msg = ex.InnerException.Message;}else{msg = ex.Message;}return Content("<script>alert(\"异常:" + msg + "\");</script>");}}
这篇关于C#用iTextSharp导出PDF到页面的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!