通过ItextSharp建立复杂报表1

 前两天做的一个项目,客户要求报表直接写入pdf后下载,因为报表格式是规定好,并且尺寸要求严格,所以费了几天的时间才达到标准.现在发出来供大家分享,希望能从这里得到一些启示,由于赶项目进度,所以在性能上和代码整洁上的考虑不多。

 

using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using iTextSharp.text.pdf;
using iTextSharp.text;
using System.IO;

namespace Web
{
    struct ImageCellNull
    {
        public Cell img2, img3, img4, img5, img6, img7;
    }

    public partial class Share_OutPDF : System.Web.UI.Page
    {
        const float _PageWidth = 595;
        const float _PX2MM = 2.83f;//4.3F;
        const float _TableWidth = 95 * _PX2MM;
        const float _TableHeight = 75 * _PX2MM;
        const float _Col1Width = 20 * _PX2MM;
        const float _Col2Width = 30 * _PX2MM;
        const float _Col3Width = 20 * _PX2MM;
        const float _Col4Width = 25 * _PX2MM;
        const float _Row1Height = 11 * _PX2MM;
        const float _Row2Height = 10 * _PX2MM;
        const float _Row3Height = 10 * _PX2MM;
        const float _Row4Height = 12 * _PX2MM;
        const float _Row5Height = 10 * _PX2MM;
        const float _Row6Height = 6 * _PX2MM;
        const float _Row7Height = 6 * _PX2MM;
        const float _Row8Height = 14 * _PX2MM;
        const float _CellNullWidth = 5f * _PX2MM;
        const float _RowNullHeight = 13f * _PX2MM;
        const float _cellLeading18 = 18f;
        const float _cellLeading12 = 12f;
        const float _cellLeading20 = 20f;

        ImageCellNull imagenull = new ImageCellNull();
        enum Alignment { Left, Center, Right,Default }

        private bool isChVersion = true;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (1 == 0)//在此处判断是否为中文版
                isChVersion = false;
            if (!Page.IsPostBack)
            {
                string strID=Request.QueryString["ID"];
                CreateTag(strID);
            }
        }

         #region 建立主框架表
        /// <summary>
        /// 建立标签主表
        /// </summary>
        /// <param name="OrderID">运单号序列,以逗号(,)分开</param>
        public void CreateTag(string OrderID)
        {
            System.IO.MemoryStream fs = new MemoryStream();
            Document doc = new Document(PageSize.A4, 15, 15, 30.5f, 30);//定义文件纸张类型,边距
            PdfWriter pdf = PdfWriter.getInstance(doc, fs);
            DataTable dtData = getTagData(OrderID);//获取数据
            if (dtData == null)//如果不存在数据跳过
                return;
            doc.Open();//打开文档
            Table tabMain = new Table(3);//建立三列的主表
            tabMain.DefaultHorizontalAlignment = Element.ALIGN_CENTER;//表默认水平中对齐
            tabMain.DefaultVerticalAlignment = Element.ALIGN_MIDDLE;//表默认垂直中对齐,不可用
            float[] tabWidth = { _TableWidth, _CellNullWidth, _TableWidth };//设置列宽
            tabMain.Widths = tabWidth;
            tabMain.BorderWidth = 0;//设置边框为0
            tabMain.WidthPercentage = 100.0F;
            int CellIndex = 1;
            imagenull.img2 = CreateNullImage((int)_Row2Height);//空图片,用于支持单元格高度
            imagenull.img3 = CreateNullImage((int)_Row3Height);
            imagenull.img4 = CreateNullImage((int)_Row4Height);
            imagenull.img5 = CreateNullImage((int)_Row5Height);
            imagenull.img6 = CreateNullImage((int)_Row6Height);
            imagenull.img7 = CreateNullImage((int)_Row7Height);
            Cell cellMainNull = CreateNullImage((int)_RowNullHeight);
            string BatchCode = "";
            int BatchID = 1;
            foreach (DataRow dr in dtData.Rows)
            {
                if (dr["vchBatchNumber"].ToString() != BatchCode)
                {
                    BatchCode = dr["vchBatchNumber"].ToString();
                    BatchID = 1;
                }
                tabMain.insertTable(CreateTagTable(dr, BatchID));
                tabMain.addCell(cellNull(0));//加中间空列
                if (CellIndex % 2 == 0)//每两个表之后加一空行
                {
                    tabMain.addCell(cellMainNull);
                    tabMain.addCell(cellNull(0));
                }
                BatchID++;
                CellIndex++;
            }
            doc.Add(tabMain);//添加主表到文档

            doc.Close();
            pdf.Close();
            fs.Close();
            DownLoadFile(fs);
        }
        #endregion

        #region 建立标签表
        /// <summary>
        /// 建立标签主表
        /// </summary>
        /// <param name="dr"></param>
        /// <param name="imgLogo"></param>
        /// <returns></returns>
        private Table CreateTagTable(DataRow dr, int BatchID)
        {
            BaseFont bfHei = CreateBaseFont();
            //建立表中需要的几种字体
            Font font1 = new Font(bfHei, 9, Font.NORMAL);
            Font font_12_B = new Font(bfHei, 12, Font.BOLD);
            Font font_12 = new Font(bfHei, 12, Font.NORMAL);
            Font font_14_B = new Font(bfHei, 14, Font.BOLD);
            Font font_20_B = new Font(bfHei, 18, Font.BOLD);
            Font font_1_N = new Font(bfHei, 1, Font.BOLD, new Color(255, 255, 255));//白色一号字,用于防图片压边
            Table tab = new Table(6, 9);//建立5列9行表
            tab.AbsWidth = _TableWidth.ToString();//表宽度
            tab.Cellpadding = 5;
            tab.Cellspacing = 2;
            float[] tabWidth = { _Col1Width, _Col2Width, _Col3Width, _Col4Width, 1, 1 };
            tab.Widths = tabWidth; //设置各列宽
            tab.BorderWidth = 1;//设置边框宽度
            tab.DefaultHorizontalAlignment = Element.ALIGN_CENTER;//表默认水平中对齐
            tab.DefaultVerticalAlignment = Element.ALIGN_MIDDLE;//表默认算直中对齐

            Cell cellHeard = new Cell();//定义第一行
            cellHeard.Colspan = 4;
            int intUsefullife = 0;
            int.TryParse(dr["intpartusefullife"].ToString(), out intUsefullife);
            string SheetDate = DateTime.Now.AddDays(intUsefullife).ToString("yyyy-MM-dd");//建立表头时间,当前时间+有效期
            System.Drawing.Bitmap imgLogo = ImageHeader(SheetDate);//建立表头图片
            Image img = Image.getInstance(imgLogo, null, false);
            img.Alignment = Image.MIDDLE;//图片中对齐
            img.Alignment = Image.MIDDLE;//图片中对齐
            img.scaleToFit(_TableWidth, _TableHeight);//将图片缩小到260*30
            cellHeard.Add(new Paragraph(".", font_1_N)); //白色一号字,用于防图片压边
            cellHeard.Add(img);
            cellHeard.Leading = 1;//设置行间距
            tab.addCell(cellHeard, 0, 0);
            //第二行
            tab.addCell(CreateCell(Resources.PrintfSet.PartsName, font1,_cellLeading18), 1, 0);
            //根据版本分别提取中英文名
            if (isChVersion)
                tab.addCell(CreateCell(dr["vchDescriptionCH"].ToString(), font_12_B,_cellLeading18), 1, 1);
            else
                tab.addCell(CreateCell(dr["vchDescriptionEN"].ToString(), font_12_B, _cellLeading18), 1, 1);
            tab.addCell(CreateCell(Resources.PrintfSet.PartNo, font1, _cellLeading18), 1, 2);
            tab.addCell(CreateCell(dr["vchPartCode"].ToString(), font_12_B, _cellLeading18), 1, 3);
            //第三行
            tab.addCell(CreateCell(Resources.PrintfSet.OrderNO, font1, _cellLeading18), 2, 0);//订单号
            tab.addCell(CreateCell(dr["vchOrderNum"].ToString(), font_12_B, _cellLeading18), 2, 1);//订单号
            tab.addCell(CreateCell(Resources.PrintfSet.DeliveryOrderNO, font1, _cellLeading18), 2, 2);//交货单号
            tab.addCell(CreateCell(dr["vchOrderCode"].ToString(), font_12_B, _cellLeading18), 2, 3);//交货单号
            //第四行
            tab.addCell(CreateCell(Resources.PrintfSet.Suppliers, font1, _cellLeading20), 3, 0);//供应商
            //当文字较多换行时需要改变行间距
            int ProCodeLen = 0;
            string providercode="(" + dr["vchprovidercode"].ToString() + ")" + dr["vchName"].ToString();
            foreach (char charPro in providercode.ToCharArray())
                if ((int)charPro > 999)
                    ProCodeLen += 2;
                else
                    ProCodeLen += 1;
            if(ProCodeLen>30)//当内容超过15个汉字或30个非汉字时换行
                tab.addCell(CreateCell(providercode, font_14_B, _cellLeading12+2, 3, 1), 3, 1);//1.2
            else
                tab.addCell(CreateCell(providercode, font_14_B, _cellLeading20, 3, 1), 3, 1);//1.2
            //第五行
            tab.addCell(CreateCell(Resources.PrintfSet.QtyInfo, font1, _cellLeading18), 4, 0);
            tab.addCell(CreateCell(dr["intPackNumber"].ToString(), font_20_B, _cellLeading20), 4, 1);
            //当为英文版时需要换行间距
            if (isChVersion)
                tab.addCell(CreateCell(Resources.PrintfSet.Passs, font1, _cellLeading12), 4, 2);
            else
                tab.addCell(CreateCell(Resources.PrintfSet.Passs, font1, _cellLeading18), 4, 2);
            tab.addCell(CreateCell(" ", font_12_B), 4, 3);//空值
            //第六行
            tab.addCell(CreateCell(" " + Resources.PrintfSet.SupplierBatchNo, font1, 11f, Alignment.Left), 5, 0);//供应商批号
            tab.addCell(CreateCell(dr["vchBatchNumber"].ToString(), font_12_B, _cellLeading12), 5, 1);//供应商批号
            //当为英文版时需要换行间距
            if(isChVersion)
                tab.addCell(CreateCell(Resources.PrintfSet.IQCPass, font1, 0, 1, 2), 5, 2);
            else
                tab.addCell(CreateCell(Resources.PrintfSet.IQCPass, font1, _cellLeading18, 1, 2), 5, 2);//章(English)

            tab.addCell(CreateCell("", font_12_B, 13f ,1, 2), 5, 3);//空值
            //第七行
            tab.addCell(CreateCell(" " + Resources.PrintfSet.Shippingdate, font1, _cellLeading12, Alignment.Left), 6, 0);//出厂日期
            tab.addCell(CreateCell(DateTime.Now.ToString("yyMMdd"), font_12_B, _cellLeading12), 6, 1);//出厂日期
            //第八行
            System.Drawing.Image image = TF.Webasto.BLL.Code128.CreateImage(dr["vchTagDetailCode"].ToString(), (int)(_Col1Width + _Col2Width + _Col3Width) * 2, (int)_Row8Height * 2, TF.Webasto.BLL.Code128.CodePosition.Below, "");
            Image imgCode = Image.getInstance(image, null, false);
            imgCode.Alignment = Image.MIDDLE;
            imgCode.scaleToFit(_Col1Width + _Col2Width + _Col3Width, _Row8Height);
            Cell cellCode = new Cell();
            cellCode.Colspan = 3;
            cellCode.Add(new Paragraph(".", font_1_N)); //白色一号字,用于防图片压边
            cellCode.Add(imgCode);
            cellCode.Leading = 1;
            tab.addCell(cellCode, 7, 0);//供应商
            tab.addCell(CreateCell(BatchID.ToString() + "/" + dr["intBoxNumber"].ToString(), font_12, _cellLeading20 + 3), 7, 3);//编号

            //tab.addCell(cellNull(_Row1Height), 0, 4);
            tab.addCell(imagenull.img2, 1, 5);
            tab.addCell(imagenull.img3, 2, 5);
            tab.addCell(imagenull.img4, 3, 5);
            tab.addCell(imagenull.img5, 4, 5);
            tab.addCell(imagenull.img6, 5, 5);
            tab.addCell(imagenull.img7, 6, 5);
            //tab.addCell(cellNull(_Row8Height), 7, 4);
            return tab;
        }
        #endregion

        #region 下载文件
        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="MyFileStream"></param>
        private void DownLoadFile(System.IO.MemoryStream fs)
        {
            Response.AddHeader("Content-Disposition", "attachment;filename=file.pdf");
            Response.ContentType = "application/octet-stream";

            Response.BinaryWrite(fs.ToArray());
            Response.Flush();
            Response.Close();

        }
        #endregion

        #region 添加表头图片
        /// <summary>
        /// 添加表头部分图片,由于不能再套表格,所以将logo和时间画在同一图上
        /// </summary>
        /// <returns></returns>
        private System.Drawing.Bitmap ImageHeader(string SheetDate)
        {
            System.Drawing.Bitmap img = new System.Drawing.Bitmap(((int)_TableWidth-10)*2,(int)_Row1Height*2);//建立原图为PDF中大小的两倍
            System.Drawing.Bitmap img1 = new System.Drawing.Bitmap(MapPath("../images/logo4.png"));
            System.Drawing.Graphics g=System.Drawing.Graphics.FromImage(img);
            g.Clear(System.Drawing.Color.White);
            int Prent = img.Height / img1.Height;
            g.DrawImage(img1, 0, 0, img1.Width*Prent, img.Height);
            g.DrawString(SheetDate, new System.Drawing.Font("", 12), new System.Drawing.SolidBrush(System.Drawing.Color.Black), 430, 20);
            g.Save();
            return img;
        }
        #endregion

        #region 公共信息

        #region 建立空图片表
        private Cell CreateNullImage(int Height)
        {
            System.Drawing.Bitmap img = new System.Drawing.Bitmap(1, Height);//建立原图为PDF中大小的两倍
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(img);
            g.Clear(System.Drawing.Color.White);
            System.Drawing.Pen pen=new System.Drawing.Pen(new System.Drawing.SolidBrush(System.Drawing.Color.White),1);
            g.DrawLine(pen, 0, 0, 0, Height);
            g.Save();
            Image img1 = Image.getInstance(img, null, false);
            //img1.scaleToFit(1, Height);
            Cell cell = new Cell();//定义第一行

            cell.BorderWidth = 0;
            cell.Add(img1);
            cell.Leading = 0;//设置行间距
            return cell;
        }
        #endregion

        /// <summary>
        /// 建立基础字体
        /// </summary>
        /// <returns></returns>
        private BaseFont CreateBaseFont()
        {
            string FontPath = MapPath("../simsun.ttc,1");//必须把字体复制到相应地方
            BaseFont bfHei = BaseFont.createFont(FontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            return bfHei;
        }

        /// <summary>
        /// 添加空表
        /// </summary>
        /// <param name="Heidht"></param>
        /// <returns></returns>
        private Cell cellNull(float Heidht)
        {
            Cell cell = new Cell("");
            cell.BorderWidth = 0;
            cell.Leading = Heidht;
            return cell;
        }

        #region 建立单元格
        private Cell CreateCell(string content, Font font)
        {
            return CreateCell(content, font, 0, 1, 1, Alignment.Default);
        }

        private Cell CreateCell(string content, Font font, float RowHeight)
        {
            return CreateCell(content, font, RowHeight, 1, 1, Alignment.Default);
        }

        private Cell CreateCell(string content, Font font, Alignment Align)
        {
            return CreateCell(content, font, 0, 1, 1, Align);
        }

        private Cell CreateCell(string content, Font font, float RowHeight, Alignment Align)
        {
            return CreateCell(content, font, RowHeight, 1, 1, Align);
        }

        private Cell CreateCell(string content, Font font, float RowHeight, int ColSpan, int RowSpan)
        {
            return CreateCell(content, font, RowHeight, ColSpan, RowSpan, Alignment.Default);
        }

        private Cell CreateCell(string content, Font font, float RowHeight, int ColSpan, int RowSpan, Alignment Align)
        {
            Cell cell = new Cell(new Paragraph(content.Replace("<br />", "/r/n"), font));
            switch (Align)
            {
                case Alignment.Left:
                    cell.HorizontalAlignment = Element.ALIGN_LEFT;//水平对齐
                    break;
                case Alignment.Center:
                    cell.HorizontalAlignment = Element.ALIGN_CENTER;//水平对齐
                    break;
                case Alignment.Right:
                    cell.HorizontalAlignment = Element.ALIGN_RIGHT;//水平对齐
                    break;
                case Alignment.Default:
                    cell.HorizontalAlignment = Element.ALIGN_CENTER;//水平对齐
                    break;
            }
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE.ToString());
            //cell.VerticalAlignment = Element.ALIGN_MIDDLE;//垂直对齐

            if (RowHeight > 0)
                cell.Leading = RowHeight;
            if (RowHeight < 0)
                cell.Leading = cell.Leading + RowHeight;
           
            cell.Colspan = ColSpan;//跨列
            cell.Rowspan = RowSpan;//跨行
            return cell;
        }
        #endregion

        #endregion

        #region 获取数据
        private DataTable getTagData(string OrderID)
        {
            if (!string.IsNullOrEmpty(OrderID))
            {
                string Where = "";
                foreach (string strWhere in OrderID.Split(','))
                {
                    Where += " or FK_intDeliveryOrderID=" + OrderID;
                }
                if (Where != "")
                    Where = Where.Substring(3);
                BLL.tabTagDetail bll = BLL.tabTagDetail();
                DataTable dtData = bll.GetList(Where+ " order by vchBatchNumber ").Tables[0];
                return dtData;
            }
            else
                return null;
        }
        #endregion

        #region 导出到excel
        /// <summary>
        /// 导出到excel,例new ExcelExport().Export(dgrList, Response, "仓库应收报表");
        /// </summary>
        /// <param name="ctl">导出控件</param>
        /// <param name="hrs">Response</param>
        /// <param name="Titel">导出数据标题</param>
        public void Export1(Control ctl, HttpResponse hrs, string Titel)
        {
            hrs.Clear();
            hrs.Buffer = true;
            hrs.Charset = "gb2312";
            hrs.AppendHeader("Content-Disposition", "attachment;filename=Report.xls");
            hrs.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");//设置输出流为简体中文
            hrs.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
            EnableViewState = false;
            System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
            oStringWriter.Write("<style>td {font-size: 9pt;}.title {font-size: 11pt;font-weight: bolder;}</style>");//添加样式
            oStringWriter.Write("<p align='center'><font Class='title'><br>" + Titel + "</font></p>");//定义表头
            ctl.RenderControl(oHtmlTextWriter); //dgrLineConyent为datagrid控件名称
            hrs.Write(oStringWriter.ToString());
            hrs.End();
        }
        #endregion
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值