一、下载工具包

 

 

 

       string sWebRootFolder = GetTupleDocx("Word模板").Item1;
            string sFileName = GetTupleDocx("Word模板").Item2;
            FileInfo file = new FileInfo(Path.Combine(sWebRootFolder, sFileName));//创建一个FileInfo实例,创建路径
            if (!System.IO.Directory.Exists(sWebRootFolder + sFileName))//文件是否存在如果存在删掉文件重新创建。
            {
                file.Delete(); //删除服务器上的临时文件
            }
              path = sWebRootFolder;
                FileHelper.CreatePath(path);
                path = path + "\\" + sFileName;
                XWPFDocument doc = new XWPFDocument();
                // 添加段落
                XWPFParagraph gp = doc.CreateParagraph();
  gr = gp.CreateRun();
                gp.Alignment = ParagraphAlignment.CENTER;//水平居中
                CT_RPr rpr = gr.GetCTR().AddNewRPr();
                CT_Fonts rfonts = rpr.AddNewRFonts();
                rfonts.ascii = "宋体";
                rfonts.eastAsia = "宋体";
                rpr.AddNewSz().val = (ulong)40;//5号字体
                rpr.AddNewSzCs().val = (ulong)40;
                gr.GetCTR().AddNewRPr().AddNewB().val = true; //加粗
                gr.SetText("设计方案"); //赋值


   //添加表格
                XWPFTable table = doc.CreateTable(TaskMaterial.Rows.Count+1, 5);//创建多行5列表
                CT_Tbl m_CTTbl = doc.Document.body.GetTblArray()[0];//获得文档第一张表
                CT_TblPr m_CTTblPr = m_CTTbl.AddNewTblPr();
                m_CTTblPr.AddNewTblW().w = "7000"; //表宽
                m_CTTblPr.AddNewTblW().type = ST_TblWidth.dxa;
                m_CTTblPr.tblpPr = new CT_TblPPr();//表定位
                m_CTTblPr.tblpPr.tblpX = "4003";//表左上角坐标
                m_CTTblPr.tblpPr.tblpY = "365";
                m_CTTblPr.tblpPr.tblpXSpec = ST_XAlign.center;//若不为“Null”,则优先tblpX,即表由tblpXSpec定位
                m_CTTblPr.tblpPr.tblpYSpec = ST_YAlign.center;//若不为“Null”,则优先tblpY,即表由tblpYSpec定位  
                m_CTTblPr.tblpPr.leftFromText = (ulong)180;
                m_CTTblPr.tblpPr.rightFromText = (ulong)180;
                m_CTTblPr.tblpPr.vertAnchor = ST_VAnchor.text;
                m_CTTblPr.tblpPr.horzAnchor = ST_HAnchor.page;
                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("数量");
                CT_Row m_NewRow = new CT_Row();//创建1行
                XWPFTableRow m_Row = new XWPFTableRow(m_NewRow, table);
                table.AddRow(m_Row); //必须要!!!
                  //TaskMaterial 需要给表格赋值的数据table
                for (int i = 0; i < TaskMaterial.Rows.Count; i++)
                {
                    int num = i + 1;
                    table.GetRow(num).GetCell(0).SetText(TaskMaterial.Rows[i]["RowNums"].ToString());
                    table.GetRow(num).GetCell(1).SetText(TaskMaterial.Rows[i]["ProductName"].ToString());
                    table.GetRow(num).GetCell(2).SetText(TaskMaterial.Rows[i]["ProductIntro"].ToString());
                    table.GetRow(num).GetCell(3).SetText(TaskMaterial.Rows[i]["UnitName"].ToString());
                    table.GetRow(num).GetCell(4).SetText(TaskMaterial.Rows[i]["Num"].ToString());
                    m_NewRow = new CT_Row();//创建1行
                    m_Row = new XWPFTableRow(m_NewRow, table);
                    table.AddRow(m_Row); //必须要!!!
                }
              //写入数据
                using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    doc.Write(fs);
                }

//返回导出
 return File(sFileName, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", taskName + ".docx");//导出excel表格

导出的格式表格效果图:

 

 

 

posted on 2021-11-30 15:05  lwmwsh  阅读(500)  评论(0编辑  收藏  举报