• .Net C# 使用NPIOI创建Word文档


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.IO;
    using System.Text;
    using NPOI;
    using NPOI.OpenXmlFormats.Wordprocessing;
    using NPOI.XWPF.UserModel;
    
    
    namespace ExportToWord.Report
    {
        public partial class WebForm1 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            protected void Export_Click(object sender, EventArgs e)
            {
                XWPFDocument doc;
                String template = AppDomain.CurrentDomain.BaseDirectory + "Template\ReportTemplate.docx";   //模板地址
          
                using (FileStream stream = new FileStream(template, FileMode.Open, FileAccess.Read))
                {
                    doc = new XWPFDocument(stream);
                    for(int i=0;i< doc.Tables.Count;i++)
                    {
                        for(int j=1;j<4; j++)  //给模板里的第一个table添加行和数据
                        {
                            if(j==1||j==2)   
                            { 
                            doc.Tables[0].Rows[j].GetCell(0).SetText("test");
                            }
                            else
                            { 
                            doc.Tables[0].CreateRow();
                            doc.Tables[0].Rows[j].GetCell(0).SetText("1");
                            doc.Tables[0].Rows[j].CreateCell().SetText("2");
                            doc.Tables[0].Rows[j].CreateCell().SetText("3");
                            doc.Tables[0].Rows[j].CreateCell().SetText("4");
                            }
                        }
    
                    }
    
                    stream.Close();
                }
                String filename = "Report_" + DateTime.Now.ToString("yyyyMMddHHmm") + ".docx";
                String fullpath = AppDomain.CurrentDomain.BaseDirectory + "Documents\" + filename;
                FileStream file = new FileStream(fullpath, FileMode.Create, FileAccess.Write);
                doc.Write(file);
                file.Close();
    
                System.IO.FileInfo fileInfo = new System.IO.FileInfo(fullpath);  //返回Word流文件
    
                if (fileInfo.Exists == true)
                {
                    const long ChunkSize = 102400;
                    byte[] buffer = new byte[ChunkSize];
    
                    Response.Clear();
                    System.IO.FileStream iStream = System.IO.File.OpenRead(fullpath);
                    long dataLengthToRead = iStream.Length;
                    Response.ContentType = "application/octet-stream";
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(filename));
                    while (dataLengthToRead > 0 && Response.IsClientConnected)
                    {
                        int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));
                        Response.OutputStream.Write(buffer, 0, lengthRead);
                        Response.Flush();
                        dataLengthToRead = dataLengthToRead - lengthRead;
                    }
                    Response.Close();
                    iStream.Close();
                    Response.End();
                }
        }
    
    
    
            private static void ReplaceKey(XWPFParagraph para, string content) //拓展方法
            {
                string text = "";
                foreach (var run in para.Runs)
                {
                    text = run.ToString();
                    run.SetText(content, 0);
                }
            }
        }
        }
  • 相关阅读:
    01_javaSE面试题:自增变量
    SpringBoot(二十)_404返回统一异常处理结果
    MD5加密工具代码
    SpringBoot(十九)_spring.profiles.active=@profiles.active@ 的使用
    读取本地文件转化成MultipartFile
    remote: http basic: access denied fatal: authentication failed for '‘解决办法
    git报错_you are not allowed to push code to protected branches on this project
    SpringBoot(十八)_springboot打成war包部署
    sql优化问题笔记(mysql)
    gitbook 简单使用
  • 原文地址:https://www.cnblogs.com/Aaron-Lee/p/10436348.html
Copyright © 2020-2023  润新知