C#用iTextSharp导出PDF到页面

来源:互联网 发布:python数据分析工具 编辑:程序博客网 时间:2024/06/06 01:32
 using iTextSharp.text;using iTextSharp.text.pdf;  //导出PDF public 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);                }                #endregion                document.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>");            }        }


0 0