zoukankan      html  css  js  c++  java
  • NPOI创建doc

    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 NPOI.XWPF.UserModel;
    
    public partial class Default2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            CreateDoc();
        }
    
        private void CreateDoc()
        {
            XWPFDocument doc = new XWPFDocument();      //创建新的word文档
    
            XWPFParagraph p1 = doc.CreateParagraph();   //向新文档中添加段落
            p1.Alignment = ParagraphAlignment.CENTER;//段落对其方式为居中
    
    
            XWPFRun r1 = p1.CreateRun();                //向该段落中添加文字
            r1.SetText("测试段落一");
    
            XWPFParagraph p2 = doc.CreateParagraph();
            p2.Alignment = ParagraphAlignment.LEFT;
    
            XWPFRun r2 = p2.CreateRun();
            r2.SetText("测试段落二");
    
    
            FileStream sw = File.Create("cutput.docx"); //...
            doc.Write(sw);                              //...
            sw.Close();                                 //在服务端生成文件
    
            FileInfo file = new FileInfo("cutput.docx");//文件保存路径及名称  
            //注意: 文件保存的父文件夹需添加Everyone用户,并给予其完全控制权限
            Response.Clear();
            Response.ClearHeaders();
            Response.Buffer = false;
            Response.ContentType = "application/octet-stream";
            Response.AppendHeader("Content-Disposition", "attachment;filename="
                + HttpUtility.UrlEncode("output.docx", System.Text.Encoding.UTF8));
            Response.AppendHeader("Content-Length", file.Length.ToString());
            Response.WriteFile(file.FullName);
            Response.Flush();                           //以上将生成的word文件发送至用户浏览器
    
            File.Delete("cutput.docx");                 //清除服务端生成的word文件
    
        }
    }
    

     程序员的基础教程:菜鸟程序员 

  • 相关阅读:
    jmeter测试mysql数据库之JDBC请求
    接口测试浅谈
    python import xxx 与 from xxx import xx 模块引入的区别
    交互模式下测试python代码及变量的四则运算
    python入门之一python安装及程序运行
    zabbix命令之:zabbix_get命令
    snmpwalk工具使用
    zabbix基础知识
    Centos7.5 rpm安装zabbix_agent4.0.3
    mysql常用命令
  • 原文地址:https://www.cnblogs.com/guohu/p/5572090.html
Copyright © 2011-2022 走看看