导出pdf(表格形式)

来源:互联网 发布:淘宝客拉人广告词 编辑:程序博客网 时间:2024/06/10 02:31
由于一个项目有大量的表格需要导出成pdf,所以研究了一下,现在整理了一下发表出来,有需要的朋友可以看一下。

本文主要介绍了一个简单的pdf导出,我封装了三个方法,实现了表格,单选,多选的功能,这样节约了大量的代码;

如果想看到效果,可以把从数据库里面的查询dt.Rows[0]["LegalPeople"].ToString()替换为"",这样就实现了一个空表了


1.首先添加itextsharp.dll组件的引用;
2.然后创建一个web项目;

3.简单的pdf表格导出代码如下:

 public void DataDayi(string _TabName, int _pid)
        {
            Document document = null;
            //为该Document创建一个Writer实例
            PdfWriter writer = null;
            Paragraph p = null;

            try
            {
        //从数据库查询数据  dt.Rows[0]["OrganizationCode"].ToString()取出具体的数据,可以将其替换为“”可以看到效果,不然没有数据库就会报错
                SL.Data D = new SL.Data();
                DataTable dt = D.select(_TabName, "PId ='" + _pid + "'");

                BaseFont bfchinese = BaseFont.CreateFont(@"c:\windows\fonts\SIMHEI.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
                Font ChFont = new Font(bfchinese, 10, Font.NORMAL);
                Font ChFont1 = new Font(bfchinese, 10, Font.BOLD);
                Font ChFont_blue = new Font(bfchinese, 20, Font.NORMAL, new BaseColor(51, 0, 153));
                Font ChFont_line = new Font(bfchinese, 10, Font.NORMAL, new BaseColor(51, 0, 153));
                Font ChFont_msg = new Font(bfchinese, 10, Font.BOLD, BaseColor.BLACK);
                Font ChFont_msg2 = new Font(bfchinese, 10, Font.NORMAL, BaseColor.BLACK);
                Font ChFont_msg1 = new Font(bfchinese, 10, Font.NORMAL, BaseColor.BLACK);
                MemoryStream memoryStream = new MemoryStream();
                document = new Document(PageSize.A4, -30, -30, 10, 10);

                writer = PdfWriter.GetInstance(document, memoryStream);
                writer.CloseStream = false;
                //打开
                document.Open();

                p = new Paragraph("研发机构建设资助(2015年)\n", ChFont_msg2);
                p.Alignment = Element.ALIGN_CENTER;
                document.Add(p);

                //标题与表格换行
                p = new Paragraph("\n\n", ChFont_msg2);
                p.Alignment = Element.ALIGN_CENTER;
                document.Add(p);
                
                PdfPTable table = new PdfPTable(14);
                //table.SetTotalWidth(new float[] { 2F, 2F, 2F, 2F, 2F });
                CreateCell(14, 1, "单位基本情况", ChFont_msg2, 0, 1, 0, false, table);

                CreateCell(2, 1, "名称:", ChFont_msg2, 1, 1, 0, false, table);
                CreateCell(12, 1, dt.Rows[0]["CompanyName"].ToString(), ChFont_msg2, 1, 1, 0, false, table);

                CreateCell(2, 1, "组织机构代码:", ChFont_msg2, 1, 1, 0, false, table);
                CreateCell(12, 1, dt.Rows[0]["OrganizationCode"].ToString(), ChFont_msg2, 1, 1, 0, false, table);

                CreateCell(2, 1, "通讯地址:", ChFont_msg2, 1, 1, 0, false, table);
                CreateCell(5, 1, dt.Rows[0]["Address"].ToString(), ChFont_msg2, 1, 1, 0, false, table);
                CreateCell(2, 1, "邮编:", ChFont_msg2, 1, 1, 0, false, table);
                CreateCell(5, 1, dt.Rows[0]["ZipCode"].ToString(), ChFont_msg2, 1, 1, 0, false, table);

                CreateCell(2, 1, "企业注册类型:", ChFont_msg2, 1, 1, 0, false, table);


                CreateCell(12, 1, select("□国有 □集体 □私营 □联营 □股份 □中外合资 □外资独资 □中外合作 □其它", dt.Rows[0]["RegisteredType"].ToString()), ChFont_msg2, 0, 1, 0, false, table);

                CreateCell(14, 1, "研发机构基本情况", ChFont_msg2, 0, 1, 0, false, table);

                CreateCell(2, 1, "机构名称:", ChFont_msg2, 1, 1, 0, false, table);
                CreateCell(5, 1, dt.Rows[0]["OrganizationName"].ToString(), ChFont_msg2, 1, 1, 0, false, table);
                CreateCell(2, 1, "组建时间:", ChFont_msg2, 1, 1, 0, false, table);
                CreateCell(5, 1, dt.Rows[0]["SetTime"].ToString(), ChFont_msg2, 1, 1, 0, false, table);

                CreateCell(2, 1, "机构类型:", ChFont_msg2, 1, 1, 0, false, table);
                CreateCell(12, 1, select("□省级工程技术研究中心 □省级重点实验室 □省级企业技术中心 □省级科技研发(开发)中心 □市级工程技术研究中心 □市级企业技术中心 □市级科技研发(开发)中心 □市级产学研联合实验室 □院士(专家)创新工作站", dt.Rows[0]["OrganizationType"].ToString()), ChFont_msg2, 0, 1, 0, false, table);

                CreateCell(2, 1, "技术领域:", ChFont_msg2, 1, 1, 0, false, table);
                CreateCell(12, 1, select("□电子信息 □高端装备制造 □生物医药 □新材料 □新能源 □节能环保 □都市现代农业 □现代服务业 □轻工食品 □冶金石化 □建筑市政交通 □其它", dt.Rows[0]["TechnicalField"].ToString()), ChFont_msg2, 0, 1, 0, false, table);

                CreateCell(2, 1, "联系人:", ChFont_msg2, 1, 1, 0, false, table);
                CreateCell(5, 1, dt.Rows[0]["Contacts"].ToString(), ChFont_msg2, 1, 1, 0, false, table);
                CreateCell(2, 1, "联系电话:", ChFont_msg2, 1, 1, 0, false, table);
                CreateCell(5, 1, dt.Rows[0]["ContactNumber"].ToString(), ChFont_msg2, 1, 1, 0, false, table);

                CreateCell(2, 1, "Email:", ChFont_msg2, 1, 1, 0, false, table);
                CreateCell(5, 1, dt.Rows[0]["Email"].ToString(), ChFont_msg2, 1, 1, 0, false, table);
                CreateCell(2, 1, "传 真:", ChFont_msg2, 1, 1, 0, false, table);
                CreateCell(5, 1, dt.Rows[0]["Fax"].ToString(), ChFont_msg2, 1, 1, 0, false, table);

                CreateCell(2, 1, "人员总数:", ChFont_msg2, 1, 1, 0, false, table);
                CreateCell(2, 1, dt.Rows[0]["PeopleNumber"].ToString(), ChFont_msg2, 1, 1, 0, false, table);
                CreateCell(1, 1, "人", ChFont_msg2, 1, 1, 0, false, table);
                CreateCell(2, 1, "其中:高级:", ChFont_msg2, 1, 1, 0, false, table);
                CreateCell(2, 1, dt.Rows[0]["Advanced"].ToString(), ChFont_msg2, 1, 1, 0, false, table);
                CreateCell(1, 1, "人", ChFont_msg2, 1, 1, 0, false, table);
                CreateCell(1, 1, "中级:", ChFont_msg2, 1, 1, 0, false, table);
                CreateCell(2, 1, dt.Rows[0]["Intermediate"].ToString(), ChFont_msg2, 1, 1, 0, false, table);
                CreateCell(1, 1, "人", ChFont_msg2, 1, 1, 0, false, table);

                CreateCell(14, 1, "研发机构负责人", ChFont_msg2, 0, 1, 0, false, table);
                CreateCell(1, 1, "选择", ChFont_msg2, 1, 1, 0, false, table);
                CreateCell(1, 1, "序号", ChFont_msg2, 1, 1, 0, false, table);
                CreateCell(2, 1, "姓名 ", ChFont_msg2, 1, 1, 0, false, table);
                CreateCell(2, 1, "年龄 ", ChFont_msg2, 1, 1, 0, false, table);
                CreateCell(2, 1, "职称", ChFont_msg2, 1, 1, 0, false, table);
                CreateCell(2, 1, "从事专业 ", ChFont_msg2, 1, 1, 0, false, table);
                CreateCell(2, 1, "所在单位", ChFont_msg2, 1, 1, 0, false, table);
                CreateCell(2, 1, "电话 ", ChFont_msg2, 1, 1, 0, false, table);

                JsonData jd = JsonMapper.ToObject(dt.Rows[0]["Department"].ToString());
                for (int i = 0; i < jd.Count; i++)
                {
                    CreateCell(1, 1, "○", ChFont_msg2, 1, 1, 0, false, table);
                    CreateCell(1, 1,i+1+"", ChFont_msg2, 1, 1, 0, false, table);
                    CreateCell(2, 1, jd[i]["Departmenta"].ToString(), ChFont_msg2, 1, 1, 0, false, table);
                    CreateCell(2, 1, jd[i]["Departmentb"].ToString(), ChFont_msg2, 1, 1, 0, false, table);
                    CreateCell(2, 1, jd[i]["Departmentc"].ToString(), ChFont_msg2, 1, 1, 0, false, table);
                    CreateCell(2, 1, jd[i]["Departmentd"].ToString(), ChFont_msg2, 1, 1, 0, false, table);
                    CreateCell(2, 1, jd[i]["Departmente"].ToString(), ChFont_msg2, 1, 1, 0, false, table);
                    CreateCell(2, 1, jd[i]["Departmentf"].ToString(), ChFont_msg2, 1, 1, 0, false, table);
                }

                CreateCell(14, 1, "技术带头人", ChFont_msg2, 0, 1, 0, false, table);
                CreateCell(1, 1, "选择", ChFont_msg2, 1, 1, 0, false, table);
                CreateCell(1, 1, "序号", ChFont_msg2, 1, 1, 0, false, table);
                CreateCell(2, 1, "姓名 ", ChFont_msg2, 1, 1, 0, false, table);
                CreateCell(2, 1, "年龄 ", ChFont_msg2, 1, 1, 0, false, table);
                CreateCell(2, 1, "职称", ChFont_msg2, 1, 1, 0, false, table);
                CreateCell(2, 1, "从事专业 ", ChFont_msg2, 1, 1, 0, false, table);
                CreateCell(2, 1, "所在单位及院系 ", ChFont_msg2, 1, 1, 0, false, table);
                CreateCell(2, 1, "电话 ", ChFont_msg2, 1, 1, 0, false, table);

                JsonData jd2 = JsonMapper.ToObject(dt.Rows[0]["TechnicalLeader"].ToString());
                for (int i = 0; i < jd2.Count; i++)
                {
                    CreateCell(1, 1,"○", ChFont_msg2, 1, 1, 0, false, table);
                    CreateCell(1, 1, i + 1 + "", ChFont_msg2, 1, 1, 0, false, table);
                    CreateCell(2, 1, jd2[i]["TechnicalLeadera"].ToString(), ChFont_msg2, 1, 1, 0, false, table);
                    CreateCell(2, 1, jd2[i]["TechnicalLeaderb"].ToString(), ChFont_msg2, 1, 1, 0, false, table);
                    CreateCell(2, 1, jd2[i]["TechnicalLeaderc"].ToString(), ChFont_msg2, 1, 1, 0, false, table);
                    CreateCell(2, 1, jd2[i]["TechnicalLeaderd"].ToString(), ChFont_msg2, 1, 1, 0, false, table);
                    CreateCell(2, 1, jd2[i]["TechnicalLeadere"].ToString(), ChFont_msg2, 1, 1, 0, false, table);
                    CreateCell(2, 1, jd2[i]["TechnicalLeaderf"].ToString(), ChFont_msg2, 1, 1, 0, false, table);
                }

                CreateCell(14, 1, "研发机构简介", ChFont_msg2, 0, 1, 0, false, table);
                CreateCell(14, 1, "研发机构简介(限1000字):", ChFont_msg2, 0, 1, 0, false, table);
                CreateCell(14, 10, dt.Rows[0]["Profile"].ToString(), ChFont_msg2, 0, 1, 0, false, table);

                CreateCell(1, 1, "序号", ChFont, 1, 0, 0, false, table);
                CreateCell(5, 1, "附件文件 ", ChFont, 1, 0, 0, false, table);
                CreateCell(2, 1, "是否必备材料", ChFont, 1, 0, 0, false, table);
                CreateCell(4, 1, "查看", ChFont, 1, 0, 0, false, table);
                CreateCell(2, 1, "操作", ChFont, 1, 0, 0, false, table);

                CreateCell(1, 1, "1", ChFont, 1, 1, 0, false, table);
                CreateCell(5, 1, "法人营业执照 ", ChFont, 0, 1, 0, false, table);
                CreateCell(2, 1, "是 ", ChFont, 1, 1, 0, false, table);
                CreateCell(4, 1, dt.Rows[0]["BusinessLicense"].ToString(), ChFont, 1, 1, 0, false, table);
                CreateCell(2, 1, "上传", ChFont, 1, 1, 0, false, table);

                CreateCell(1, 1, "2", ChFont, 1, 1, 0, false, table);
                CreateCell(5, 1, "组织机构代码证", ChFont, 0, 1, 0, false, table);
                CreateCell(2, 1, "是 ", ChFont, 1, 1, 0, false, table);
                CreateCell(4, 1, dt.Rows[0]["CodeCertificate"].ToString(), ChFont, 1, 1, 0, false, table);
                CreateCell(2, 1, "上传", ChFont, 1, 1, 0, false, table);

                CreateCell(1, 1, "3", ChFont, 1, 1, 0, false, table);
                CreateCell(5, 1, "税务登记证", ChFont, 0, 1, 0, false, table);
                CreateCell(2, 1, "是 ", ChFont, 1, 1, 0, false, table);
                CreateCell(4, 1, dt.Rows[0]["TaxCertificate"].ToString(), ChFont, 1, 1, 0, false, table);
                CreateCell(2, 1, "上传", ChFont, 1, 1, 0, false, table);

                CreateCell(1, 1, "4", ChFont, 1, 1, 0, false, table);
                CreateCell(5, 1, "上一年度审计报告及财务报表(资产负载表、现金流量表、损益表)", ChFont, 0, 1, 0, false, table);
                CreateCell(2, 1, "是 ", ChFont, 1, 1, 0, false, table);
                CreateCell(4, 1, dt.Rows[0]["ReportStatements"].ToString(), ChFont, 1, 1, 0, false, table);
                CreateCell(2, 1, "上传", ChFont, 1, 1, 0, false, table);

                CreateCell(1, 1, "5", ChFont, 1, 1, 0, false, table);
                CreateCell(5, 1, "省、市企业研发机构批文(原件和复印件)", ChFont, 0, 1, 0, false, table);
                CreateCell(2, 1, "是 ", ChFont, 1, 1, 0, false, table);
                CreateCell(4, 1, dt.Rows[0]["Approval"].ToString(), ChFont, 1, 1, 0, false, table);
                CreateCell(2, 1, "上传", ChFont, 1, 1, 0, false, table);

                CreateCell(1, 1, "6", ChFont, 1, 1, 0, false, table);
                CreateCell(5, 1, "大邑县产学研联合实验室资助资金到位证明", ChFont, 0, 1, 0, false, table);
                CreateCell(2, 1, "否 ", ChFont, 1, 1, 0, false, table);
                CreateCell(4, 1, dt.Rows[0]["FundedProve"].ToString(), ChFont, 1, 1, 0, false, table);
                CreateCell(2, 1, "上传", ChFont, 1, 1, 0, false, table);

                CreateCell(1, 1, "7", ChFont, 1, 1, 0, false, table);
                CreateCell(5, 1, "经税务部门审核的研究开发费用税前加计扣除《企业所得税优惠备案表》", ChFont, 0, 1, 0, false, table);
                CreateCell(2, 1, "否 ", ChFont, 1, 1, 0, false, table);
                CreateCell(4, 1, dt.Rows[0]["Income"].ToString(), ChFont, 1, 1, 0, false, table);
                CreateCell(2, 1, "上传", ChFont, 1, 1, 0, false, table);

                CreateCell(1, 1, "8", ChFont, 1, 1, 0, false, table);
                CreateCell(5, 1, "其它资质材料(如专利证书、项目立项文件等)", ChFont, 0, 1, 0, false, table);
                CreateCell(2, 1, "否 ", ChFont, 1, 1, 0, false, table);
                CreateCell(4, 1, dt.Rows[0]["OtherMaterials"].ToString(), ChFont, 1, 1, 0, false, table);
                CreateCell(2, 1, "上传", ChFont, 1, 1, 0, false, table);

                document.Add(table);
                //关闭目标文件
                document.Close();
                //关闭写入流
                writer.Close();
                //输出
                System.Web.HttpContext.Current.Response.ContentType = "application/pdf";
                System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
                System.Web.HttpContext.Current.Response.AddHeader("Content-disposition", string.Format("attachment; filename={0}-{1}-{2}.pdf", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day));
                System.Web.HttpContext.Current.Response.OutputStream.Write(memoryStream.GetBuffer(), 0, memoryStream.GetBuffer().Length);
                System.Web.HttpContext.Current.Response.OutputStream.Flush();
                System.Web.HttpContext.Current.Response.OutputStream.Close();
                System.Web.HttpContext.Current.Response.End();
                //context.Response.OutputStream.Flush();
                //context.Response.OutputStream.Close();
            }
            catch (Exception ex)
            {
                string ee = ex.Message;
                //关闭目标文件
                document.Close();
                //关闭写入流
                writer.Close();
                document = null;
                writer = null;
                p = null;
            }
        }
4.实现的效果如下:



5.下面的方法实现了表格,
/// 1.合并列数  2.合并行数   3. 内容 4.字体 5.水平对齐方式 6. 垂直对齐方式  7.间距 8.边框线 9.此单元格填充的表
        private void CreateCell(int Colspan, int Rowspan, string Content, Font font, int HorizontalAlignment, int VerticalAlignment, int Padding, bool Border,  PdfPTable table)
        {
            PdfPCell cell = new PdfPCell(new Phrase(Content, font));
            cell.Colspan = Colspan;
            cell.Rowspan = Rowspan;
            
            if (HorizontalAlignment > 0)
                cell.HorizontalAlignment = 1;
            if (VerticalAlignment > 0)
                cell.VerticalAlignment = Element.ALIGN_MIDDLE;
            if (Border)
                cell.Border = 1;
            if (Padding > 0)
                cell.Padding = Padding;
            table.AddCell(cell);
        }
6.单选,多选的实现都差不多,首先是在表格里面显示原内容,然后再从数据库里面查出选择的项,然后将其替换

        public string select(string selectstr, string selectedstr)
        {
            //页面原始的数据   selectstr = "□国有 □集体 □私营 □联营 □股份 □中外合资 □外资独资 □中外合作 □其它";
            //数据库中存的数据  selectedstr="股份,中外合资";
            if (selectedstr == "")
            {
                return selectstr;
            }
            for (int i = 0; i < selectedstr.Split(',').Length; i++)
            {
                selectstr = selectstr.Replace("□" + selectedstr.Split(',')[i], "■" + selectedstr.Split(',')[i]);
            }
            return selectstr;
        }

        public string danxuan(string selectstr, string selectedstr)
        {
            //页面原始的数据 selectstr = "○是 ○否";
            //数据库中存的数据 selectedstr="是";
            if (selectedstr == "")
            {
                return selectstr;
            }
            for (int i = 0; i < selectedstr.Split(',').Length; i++)
            {
                selectstr = selectstr.Replace("○" + selectedstr.Split(',')[i], "●" + selectedstr.Split(',')[i]);

            }
            return selectstr;
        }
0 0
原创粉丝点击