Npoi导出Word

混江龙づ霸主 提交于 2019-12-16 23:57:27

参考网上大神们笔记,完成Word导出。

//创建文档
            XWPFDocument doc = new XWPFDocument();            
            
            //标题
            XWPFParagraph p1 = doc.CreateParagraph();
          
            XWPFRun r1 = p1.CreateRun();
            r1.SetBold(true);
            r1.FontSize = 23;
            r1.SetText("先进个人基本信息");
            r1.SetTextPosition(30);

            CT_P doc_p1 = doc.Document.body.GetPArray(0);//标题居中
            doc_p1.AddNewPPr().AddNewJc().val = ST_Jc.center;
            
            //创建表格
            XWPFTable table = doc.CreateTable();//行,列

            table.RemoveRow(0);
            

            #region

            //for (int i = 0; i < table.Rows.Count; i++)//水平和垂直居中
            //{
            //    for (int j = 0; j < table.Rows.Count; j++)
            //    {
            //        CT_Tc cttc = table.GetRow(i).GetCell(j).GetCTTc();

            //        CT_TcPr ctpr = cttc.AddNewTcPr();

            //        cttc.GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.center;//水平居中

            //        ctpr.AddNewVAlign().val = ST_VerticalJc.center;//垂直居中
            //    }
            //}

            //SetColSpan(table.GetRow(2).GetCell(1), 2);
            //SetColSpan(table.GetRow(2).GetCell(4), 2);

            //SetColSpan(table.GetRow(3).GetCell(1), 2);
            //SetColSpan(table.GetRow(3).GetCell(4), 2);

            //SetColSpan(table.GetRow(5).GetCell(1), 2);
            //SetColSpan(table.GetRow(5).GetCell(4), 2);

            //SetColSpan(table.GetRow(6).GetCell(1), 7);

            //SetColSpan(table.GetRow(7).GetCell(1), 7);

            #endregion


            //2.表格内容
            XWPFTableRow row_0 = CreateRow(table);//第一行

            XWPFTableCell cell_0_0 = CreateCell(row_0);
            cell_0_0.SetText("姓名");
            XWPFTableCell cell_0_1 = CreateCell(row_0);
            cell_0_1.SetText("");
            XWPFTableCell cell_0_2 = CreateCell(row_0);
            cell_0_2.SetText("性别");
            XWPFTableCell cell_0_3 = CreateCell(row_0);
            cell_0_3.SetText("");
            XWPFTableCell cell_0_4 = CreateCell(row_0);
            cell_0_4.SetText("民族");
            XWPFTableCell cell_0_5 = CreateCell(row_0);
            cell_0_5.SetText("");
            XWPFTableCell cell_0_6 = CreateCell(row_0);
            cell_0_6.SetText("出生年月");
            XWPFTableCell cell_0_7 = CreateCell(row_0);
            cell_0_7.SetText("");

            XWPFTableRow row_1 = CreateRow(table);//第二行
            XWPFTableCell cell_1_0 = CreateCell(row_1);
            cell_1_0.SetText("政治面貌");
            XWPFTableCell cell_1_1 = CreateCell(row_1);
            cell_1_1.SetText("");
            XWPFTableCell cell_1_2 = CreateCell(row_1);
            cell_1_2.SetText("籍贯");
            XWPFTableCell cell_1_3 = CreateCell(row_1);
            cell_1_3.SetText("");
            XWPFTableCell cell_1_4 = CreateCell(row_1);
            cell_1_4.SetText("学历");
            XWPFTableCell cell_1_5 = CreateCell(row_1);
            cell_1_5.SetText("");
            XWPFTableCell cell_1_6 = CreateCell(row_1);
            cell_1_6.SetText("参加工作时间");
            XWPFTableCell cell_1_7 = CreateCell(row_1);
            cell_1_7.SetText("");

            XWPFTableRow row_2 = CreateRow(table);//第三行
            XWPFTableCell cell_2_0 = CreateCell(row_2);
            cell_2_0.SetText("所在单位");
            XWPFTableCell cell_2_1 = CreateCell(row_2);
            SetColSpan(cell_2_1, 2);//合并单元格
            cell_2_1.SetText("");
            XWPFTableCell cell_2_2 = CreateCell(row_2);
            cell_2_2.SetText("所属部门");
            XWPFTableCell cell_2_3 = CreateCell(row_2);
            SetColSpan(cell_2_3, 2);
            cell_2_3.SetText("");
            XWPFTableCell cell_2_4 = CreateCell(row_2);
            cell_2_4.SetText("隶属系统");
            XWPFTableCell cell_2_5 = CreateCell(row_2);
            cell_2_5.SetText("");

            XWPFTableRow row_3 = CreateRow(table);//第四行
            XWPFTableCell cell_3_0 = CreateCell(row_3);
            cell_3_0.SetText("现任职务");
            XWPFTableCell cell_3_1 = CreateCell(row_3);
            SetColSpan(cell_3_1, 2);
            cell_3_1.SetText("");
            XWPFTableCell cell_3_2 = CreateCell(row_3);
            cell_3_2.SetText("任现职时间");
            XWPFTableCell cell_3_3 = CreateCell(row_3);
            SetColSpan(cell_3_3, 2);
            cell_3_3.SetText("");
            XWPFTableCell cell_3_4 = CreateCell(row_3);
            cell_3_4.SetText("典型类别");
            XWPFTableCell cell_3_5 = CreateCell(row_3);
            cell_3_5.SetText("");


            XWPFTableRow row_4 = CreateRow(table);//第五行
            XWPFTableCell cell_4_0 = CreateCell(row_4);
            cell_4_0.SetText("职称");
            XWPFTableCell cell_4_1 = CreateCell(row_4);
            cell_4_1.SetText("");
            XWPFTableCell cell_4_2 = CreateCell(row_4);
            cell_4_2.SetText("任现职称时间");
            XWPFTableCell cell_4_3 = CreateCell(row_4);
            cell_4_3.SetText("");
            XWPFTableCell cell_4_4 = CreateCell(row_4);
            cell_4_4.SetText("职工状况");
            XWPFTableCell cell_4_5 = CreateCell(row_4);
            cell_4_5.SetText("");
            XWPFTableCell cell_4_6 = CreateCell(row_4);
            cell_4_6.SetText("是否干部");
            XWPFTableCell cell_4_7 = CreateCell(row_4);
            cell_4_7.SetText("");

            XWPFTableRow row_5 = CreateRow(table);//第六行
            XWPFTableCell cell_5_0 = CreateCell(row_5);
            cell_5_0.SetText("毕业院校及专业");
            XWPFTableCell cell_5_1 = CreateCell(row_5);
            SetColSpan(cell_5_1, 2);
            cell_5_1.SetText("");
            XWPFTableCell cell_5_2 = CreateCell(row_5);
            cell_5_2.SetText("毕业时间");
            XWPFTableCell cell_5_3 = CreateCell(row_5);
            SetColSpan(cell_5_3, 2);
            cell_5_3.SetText("");
            XWPFTableCell cell_5_4 = CreateCell(row_5);
            cell_5_4.SetText("学位");
            XWPFTableCell cell_5_5 = CreateCell(row_5);
            cell_5_5.SetText("");

            XWPFTableRow row_6 = CreateRow(table);//第七行
            XWPFTableCell cell_6_0 = CreateCell(row_6);
            cell_6_0.SetText("所获荣誉");
            XWPFTableCell cell_6_1 = CreateCell(row_6);
            SetColSpan(cell_6_1, 7);
            cell_6_1.SetText("");

            XWPFTableRow row_7 = CreateRow(table);//第八行
            XWPFTableCell cell_7_0 = CreateCell(row_7);
            SetColSpan(cell_7_0, 8);
            
            XWPFParagraph aa = cell_7_0.AddParagraph();
            aa.Alignment = ParagraphAlignment.CENTER;
            XWPFRun aa_r = aa.CreateRun();
            aa_r.FontSize = 18;
            aa_r.SetTextPosition(20);
            aa_r.SetText("个人简介");


            XWPFParagraph bb = cell_7_0.AddParagraph();
            bb.Alignment = ParagraphAlignment.CENTER;
            XWPFRun bb_r = bb.CreateRun();
            bb_r.SetText("123123123123123123123123123123123123123123,123123123123123123123123123123123123123123,123123123123123123123123123123123123123123,123123123123123123123123123123123123123123,123123123123123123123123123123123123123123,123123123123123123123123123123123123123123,123123123123123123123123123123123123123123,123123123123123123123123123123123123123123,123123123123123123123123123123123123123123,123123123123123123123123123123123123123123,123123123123123123123123123123123123123123,123123123123123123123123123123123123123123,123123123123123123123123123123123123123123,123123123123123123123123123123123123123123,123123123123123123123123123123123123123123,123123123123123123123123123123123123123123,123123123123123123123123123123123123123123,123123123123123123123123123123123123123123,123123123123123123123123123123123123123123,123123123123123123123123123123123123123123,");
            





            #region 
            //table.GetRow(0).GetCell(0).SetText("姓名");//第一行
            //table.GetRow(0).GetCell(1).SetText("");
            //table.GetRow(0).GetCell(2).SetText("性别");
            //table.GetRow(0).GetCell(3).SetText("");
            //table.GetRow(0).GetCell(4).SetText("民族");
            //table.GetRow(0).GetCell(5).SetText("");
            //table.GetRow(0).GetCell(6).SetText("出生年月");
            //table.GetRow(0).GetCell(7).SetText("");

            //table.GetRow(1).GetCell(0).SetText("政治面貌");//第二行
            //table.GetRow(1).GetCell(1).SetText("");
            //table.GetRow(1).GetCell(2).SetText("籍贯");
            //table.GetRow(1).GetCell(3).SetText("");
            //table.GetRow(1).GetCell(4).SetText("学历");
            //table.GetRow(1).GetCell(5).SetText("");
            //table.GetRow(1).GetCell(6).SetText("参加工作时间");
            //table.GetRow(1).GetCell(7).SetText("");

            //table.GetRow(2).GetCell(0).SetText("所在单位");//第三行
            //table.GetRow(2).GetCell(1).SetText("");
            //table.GetRow(2).GetCell(2).SetText("所属部门");
            //table.GetRow(2).GetCell(3).SetText("");
            //table.GetRow(2).GetCell(4).SetText("隶属系统");
            //table.GetRow(2).GetCell(5).SetText("");

            //table.GetRow(3).GetCell(0).SetText("现任职务");//第四行
            //table.GetRow(3).GetCell(1).SetText("");
            //table.GetRow(3).GetCell(2).SetText("任现职时间");
            //table.GetRow(3).GetCell(3).SetText("");
            //table.GetRow(3).GetCell(4).SetText("典型类别");
            //table.GetRow(3).GetCell(5).SetText("");

            //table.GetRow(4).GetCell(0).SetText("职称");//第五行
            //table.GetRow(4).GetCell(1).SetText("");
            //table.GetRow(4).GetCell(2).SetText("任现职称时间");
            //table.GetRow(4).GetCell(3).SetText("");
            //table.GetRow(4).GetCell(4).SetText("职工状况");
            //table.GetRow(4).GetCell(5).SetText("");
            //table.GetRow(4).GetCell(6).SetText("是否干部");
            //table.GetRow(4).GetCell(7).SetText("");

            //table.GetRow(5).GetCell(0).SetText("毕业院校及专业");//第六行
            //table.GetRow(5).GetCell(1).SetText("");
            //table.GetRow(5).GetCell(2).SetText("毕业时间");
            //table.GetRow(5).GetCell(3).SetText("");
            //table.GetRow(5).GetCell(4).SetText("学位");
            //table.GetRow(5).GetCell(5).SetText("");

            //table.GetRow(6).GetCell(0).SetText("所获荣誉");//第七行
            //table.GetRow(6).GetCell(1).SetText("");

            //table.GetRow(7).GetCell(0).SetText("个人简介");//第八行
            //table.GetRow(7).GetCell(1).SetText("");
            #endregion



            //导出
            DownloadByOutputStreamBlock(new MemoryStream(ToByte(doc)), "先进个人基本信息--"+DateTime.Now.ToString("yyyyMMddHHmmss")+".docx");

 

/// <summary>
        /// 创建行
        /// </summary>
        /// <param name="table"></param>
        /// <returns></returns>
        private XWPFTableRow CreateRow(XWPFTable table)
        {
            CT_Row m_NewRow = new CT_Row();
            XWPFTableRow m_row = new XWPFTableRow(m_NewRow, table);
            m_row.GetCTRow().AddNewTrPr().AddNewTrHeight().val = (ulong)426;
            table.AddRow(m_row);
            
            return m_row;
        }

 

/// <summary>
        /// 创建列
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        private XWPFTableCell CreateCell(XWPFTableRow row)
        {
            XWPFTableCell cell = row.CreateCell();
            CT_Tc cttc = cell.GetCTTc();
            CT_TcPr ctpr = cttc.AddNewTcPr();

            cttc.GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.center;//水平居中

            ctpr.AddNewVAlign().val = ST_VerticalJc.center;//垂直居中

            ctpr.tcW = new CT_TblWidth();
            ctpr.tcW.w = "1200";//默认列宽
            ctpr.tcW.type = ST_TblWidth.dxa;

            return cell;
        }
/// <summary>
        /// 合并单元格
        /// </summary>
        /// <param name="cell"></param>
        /// <param name="Span"></param>
        private void SetColSpan(XWPFTableCell cell,int Span)
        {
            CT_Tc cttc = cell.GetCTTc();
            CT_TcPr ctpr = cttc.AddNewTcPr();
            ctpr.gridSpan = new CT_DecimalNumber();
            ctpr.gridSpan.val = Span.ToString();//合并单元格

            cttc.GetPList()[0].AddNewPPr().AddNewJc().val = ST_Jc.center;//水平居中

            ctpr.AddNewVAlign().val = ST_VerticalJc.center;//垂直居中
        }
 /// <summary>
        /// 设置列宽
        /// </summary>
        /// <param name="table">表格</param>
        /// <param name="ColIndex">列号。从0开始编号</param>
        /// <param name="Width">列宽</param>
        private void SetColWith(XWPFTable table, int ColIndex, int Width)
        {
            CT_TcPr m_pr = table.GetRow(0).GetCell(ColIndex).GetCTTc().AddNewTcPr();
            m_pr.tcW = new CT_TblWidth();
            m_pr.tcW.w = Width.ToString();
            m_pr.tcW.type = ST_TblWidth.dxa;
        }

 

 /// <summary>
        /// 导出文件
        /// </summary>
        /// <param name="stream">流</param>
        /// <param name="fileName">导出文件名</param>
        public void DownloadByOutputStreamBlock(System.IO.Stream stream, string fileName)
        {
            using (stream)
            {
                //将流的位置设置到开始位置。
                stream.Position = 0;
                //块大小
                long ChunkSize = 102400;
                //建立100k的缓冲区
                byte[] buffer = new byte[ChunkSize];
                //已读字节数
                long dataLengthToRead = stream.Length;

                Response.ContentType = "application/octet-stream";
                Response.AddHeader("Content-Disposition",
                    string.Format("attachment; filename={0}", HttpUtility.UrlPathEncode(fileName)));

                while (dataLengthToRead > 0 && Response.IsClientConnected)
                {
                    int lengthRead = stream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小
                    Response.OutputStream.Write(buffer, 0, lengthRead);
                    Response.Flush();
                    Response.Clear();
                    dataLengthToRead -= lengthRead;
                }
                Response.Close();
            }
        }

        private byte[] ToByte(XWPFDocument wb)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                //XSSFWorkbook即读取.xlsx文件返回的MemoryStream是关闭
                //但是可以ToArray(),这是NPOI的bug
                wb.Write(ms);
                return ms.ToArray();
            }
        }

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!