0
点赞
收藏
分享

微信扫一扫

asp.net通过HTML直接创建doc文档

kiliwalk 2022-03-16 阅读 79

asp.net可以通过HTML直接产生doc文档,打开时默认为“Web板式视图”,切换成"页面视图",另存为docx即可。

如果直接在服务器端调用word组件,速度较慢,网络状况下有时难以接受。

1  产生doc文件函数


    //通过HTML方式,产生doc文件
    public static void CreateWordFromHTML(string fileName)
    {
        //创建采用HTML格式创建doc文件,默认为“Web板式视图”,需要切换成“文档视图”

        CreateFile(fileName);
        //创建字符输出流
        StreamWriter sw = new StreamWriter(fileName, true, System.Text.UnicodeEncoding.UTF8);

        //需要导出的内容
        string str = "";
        str += "<html><head><title>无标题文档</title></head><body>";
        str += "<div align='center'>  <b>阅读报表</b></div>";

        str += "<p>  <b>加粗</b>   </p>";  //加粗+字号
        str += "<p>  <font size='3'>  3为小四 </font>      </p>";  //字号
        str += "<p>  <font size='4'>  4为13.5 </font>      </p>";  //字号


        str += "<table align='center' border='1' width='100%' >";

        str += "<tr width='100%'>";
        str += "<td width='20%' colspan='4'><font color='blue'>合并单元格颜色</font></td>";

        str += "</tr>";

        str += "<tr>";
        str += "<td align='right' width='20%'>宽度比例</td>";
        str += "<td align='left'  width='30%'>20000</td>";
        str += "<td align='right' width='20%'>宽度比例</td>";
        str += "<td align='left'  width='30%'>20000</td>";
        str += "</tr>";

        str += "<tr>";
        str += "<td align='right'>参数3</td>";
        str += "<td align='left'>20000</td>";
        str += "<td align='right'>参数4</td>";
        str += "<td align='left'>20000</td>";
        str += "</tr>";

        str += "<tr>";
        str += "<td align='right'>参数3</td>";
        str += "<td align='left' colspan='3'>20000</td>";
        str += "</tr>";

        str += "<tr>";
        str += "<td align='right'>参数3</td>";
        str += "<td align='left'>20000</td>";
        str += "<td align='right'>参数4</td>";
        str += "<td align='left'>20000</td>";
        str += "</tr>";

        str += "</table>";

        str += "<p>  <b>加粗</b>   </p>";  //加粗+字号
        str += "<p>  <font size='5'>5小二 </font>      </p>";  //字号

        str += "<table align='center' border='1' width='100%' >";

        str += "<tr width='100%'>";
        str += "<td width='20%' colspan='4'><font color='blue'>合并单元格颜色</font></td>";

        str += "</tr>";

        str += "<tr>";
        str += "<td align='right' width='20%'>宽度比例</td>";
        str += "<td align='left'  width='30%'>20000</td>";
        str += "<td align='right' width='20%'>宽度比例</td>";
        str += "<td align='left'  width='30%'>20000</td>";
        str += "</tr>";

        str += "<tr>";
        str += "<td align='right'>参数3</td>";
        str += "<td align='left'>20000</td>";
        str += "<td align='right'>参数4</td>";
        str += "<td align='left'>20000</td>";
        str += "</tr>";

        str += "<tr>";
        str += "<td align='right'>参数3</td>";
        str += "<td align='left' colspan='3'>20000</td>";
        str += "</tr>";

        str += "<tr>";
        str += "<td align='right'>参数3</td>";
        str += "<td align='left'>20000</td>";
        str += "<td align='right'>参数4</td>";
        str += "<td align='left'>20000</td>";
        str += "</tr>";

        str += "</table>";
        str += "</body></html>";
        //写入
        sw.Write(str);
        sw.Close();
    }

2  下载文件函数

    

 //向客户端下载生成文件
    public static void DownloadWord(string wordfile, string UserFileName)
    {
        //string fileName = "干式变压器状态检测报告.docx";//客户端保存的文件名  
        string filePath = wordfile;                       //路径
        FileInfo fileInfo = new FileInfo(filePath);
        System.Web.HttpContext.Current.Response.Clear();
        System.Web.HttpContext.Current.Response.ClearContent();
        System.Web.HttpContext.Current.Response.ClearHeaders();
        System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + UserFileName);
        System.Web.HttpContext.Current.Response.AddHeader("Content-Length", fileInfo.Length.ToString());
        System.Web.HttpContext.Current.Response.AddHeader("Content-Transfer-Encoding", "binary");
        System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
        System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
        System.Web.HttpContext.Current.Response.WriteFile(fileInfo.FullName);
        System.Web.HttpContext.Current.Response.Flush();
        System.Web.HttpContext.Current.Response.End();
    }

3  调用样例

   

 protected void btDownWord_Click(object sender, EventArgs e)
    {
            //文件存储路径
            string fileName = "test.doc";           
            string filePath = WxpUserManager.ServerMappath + "\\UserLogin\\" + fileName;

            WxpStateManager.CreateWordFromHTML(filePath);
            WxpStateManager.DownloadWord(filePath, fileName);         

    }

4  辅助函数(创建文件,如果已经存在则删除)

 //删除已经存在的文件,并新建文件
    private static void CreateFile(string filePath)
    {
        if (File.Exists(filePath))
            File.Delete(filePath);

        //新建文件
        using (FileStream fs = File.Create(filePath)) { }
    }
举报

相关推荐

0 条评论