zoukankan      html  css  js  c++  java
  • .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);
                }
            }
        }
        }
  • 相关阅读:
    网页转图片,html生成图片,网页生成图片(基于linnux+phantomjs)和wkhtmltoimage
    51Nod 1031 骨牌覆盖 | Fibonacci
    51Nod 1024 矩阵中不重复的元素 | 技巧 数学
    51Nod 1014 X^2 Mod P
    51Nod 1010 只包含因子2 3 5的数 | 预处理+二分
    51Nod 1007 正整数分组 | DP (01背包)
    51Nod 1381 硬币游戏 | 概率(数学期望)
    51Nod 1347 旋转字符串 | 规律
    51Nod 1344 走格子 | 贪心
    51Nod 1305 Pairwise Sum and Divide | 思维 数学
  • 原文地址:https://www.cnblogs.com/Aaron-Lee/p/10436348.html
Copyright © 2011-2022 走看看