vue实现world导出

话不多说 上代码

1.前端

 let postData = {
        starttime: new Date(this.$moment(starttime).format('YYYY-MM-DD')),
        endtime: new Date(this.$moment(endtime).format('YYYY-MM-DD')),
        type: 2
      }

      var xhr = new XMLHttpRequest()
      var url = window.SITE_CONFIG['baseUrl'] + 'Api/Arrange/ExportPerListByTimeDoc'
      var filename = this.myDateType === 'DAY' ? '1.docx' : '2.xls'
      xhr.open('post', url, true)
      xhr.responseType = 'blob'
      xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8')
      xhr.setRequestHeader('Authorization', 'BasicAuth123 ')
      xhr.onreadystatechange = function () {
        if (this.readyState === 4) {
          if (this.status === 200) {
            if (this.response.type === 'application/ms-excel') {
              var eleLink = document.createElement('a')
              eleLink.download = filename
              eleLink.style.display = 'none'
              var blob = new Blob([this.response], {
                type: 'application/ms-excel'
              })
              eleLink.href = URL.createObjectURL(blob)
              document.body.appendChild(eleLink)
              eleLink.click()
              document.body.removeChild(eleLink)
              // showObj.loading = false
            } else if (this.response.type === 'application/ms-world') {
              var eleLink1 = document.createElement('a')
              eleLink1.download = filename
              eleLink1.style.display = 'none'
              var blob1 = new Blob([this.response], {
                type: 'application/ms-world'
              })
              eleLink1.href = URL.createObjectURL(blob1)
              document.body.appendChild(eleLink1)
              eleLink1.click()
              document.body.removeChild(eleLink1)
            }
          }
        }
      }
      xhr.send(JSON.stringify(postData))//传递参数

2.自己搭建一些对world操作的方法 最好建个实体 弄个助手类啥的 我就直接上方法了

#region
        //1 创建标题
        /// <summary>
        /// 创建标题       
        /// 
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="title"></param>
        public void SetDocTitle(XWPFDocument doc, string title)
            {
                XWPFParagraph p0 = doc.CreateParagraph();//创建段落
                p0.Alignment = ParagraphAlignment.CENTER;//居中显示
                XWPFRun r0 = p0.CreateRun();
                //设置字体
                r0.FontFamily = "宋体";
                //设置字体大小
                r0.FontSize = 20;
                //字体是否加粗,这里加粗了
                r0.IsBold = true;
                r0.SetText(title);//写入文本
            }
            /// <summary>
            /// 创建H1
            /// </summary>
            /// <param name="doc"></param>
            /// <param name="title"></param>
            public void SetDocH1(XWPFDocument doc, string title)
            {
                XWPFParagraph p0 = doc.CreateParagraph();//创建段落
                p0.Alignment = ParagraphAlignment.LEFT;//靠左显示
                XWPFRun r0 = p0.CreateRun();
                r0.IsCapitalized = true;
                //设置字体
                r0.FontFamily = "宋体";
                //设置字体大小
                r0.FontSize = 16;
                //字体是否加粗,这里加粗了
                r0.IsBold = true;
                r0.SetText(title);//写入文本

            }

            public void SetDocH2(XWPFDocument doc, string title)
            {
                XWPFParagraph p0 = doc.CreateParagraph();//创建段落
                p0.Alignment = ParagraphAlignment.LEFT;//靠左显示
                XWPFRun r0 = p0.CreateRun();
                r0.IsCapitalized = true;
                //设置字体
                r0.FontFamily = "宋体";
                //设置字体大小
                r0.FontSize = 14;
                //字体是否加粗,这里加粗了
                r0.IsBold = true;
                r0.SetText(title);//写入文本

            }

            public void SetTableTitle(XWPFDocument doc, string title)
            {
                XWPFParagraph p1 = doc.CreateParagraph();
                p1.Alignment = ParagraphAlignment.LEFT;
                XWPFRun r1 = p1.CreateRun();
                r1.SetText(title);

            }

            public void SetTable(XWPFDocument doc, DataTable dt)
            {
                if (dt != null && dt.Rows.Count > 0)
                {
                    int rowCount = dt.Rows.Count;
                    int colCount = dt.Columns.Count;
                    XWPFTable table = doc.CreateTable(rowCount + 1, colCount);
                    int colWidth = 38 / colCount;
                    colWidth = colWidth == 0 ? 1 : colWidth;
                    //设置宽度
                    for (int i = 0; i < colCount; i++)
                    {
                        table.SetColumnWidth(i, (ulong)colWidth * 256);//设置列的宽度
                    }
                    //填写表的第一行
                    for (int i = 0; i < colCount; i++)
                    {
                        string colName = dt.Columns[i].ColumnName;
                        table.GetRow(0).GetCell(i).SetText(colName);
                    }

                    //填写表的内容
                    for (int i = 0; i < rowCount; i++)
                    {
                        for (int j = 0; j < colCount; j++)
                        {
                            string value = dt.Rows[i][j].ToString();
                            table.GetRow(i + 1).GetCell(j).SetText(value);
                        }
                    }
                }
            }
        
        #endregion

3.后端 都弄好了之后 可以创建文件了

 DateTime startDate = Convert.ToDateTime(starttime);
                DateTime endDate = Convert.ToDateTime(endtime);


                XWPFDocument docx = new XWPFDocument();
                MemoryStream ms = new MemoryStream();
                ExcelTools wordHelper = new ExcelTools();
                //1 设置标题
                wordHelper.SetDocTitle(docx, DateTime.Now.ToString("yyyy/MM/dd")+"哈哈哈");
                // 2 创建副标题
                wordHelper.SetDocH1(docx, "模块");
                //3 写入内容
                wordHelper.SetTableTitle(docx, "你好");
             
                docx.Write(ms);
                System.Web.HttpContext.Current.Response.Clear();
                System.Web.HttpContext.Current.Response.Buffer = true;
                System.Web.HttpContext.Current.Response.Charset = "utf-8";
                System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=xxx.docx");
                System.Web.HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
                System.Web.HttpContext.Current.Response.ContentType = "application/ms-world";
                System.Web.HttpContext.Current.Response.BinaryWrite(ms.ToArray());
                System.Web.HttpContext.Current.Response.End();
                ms.Close();
                ms.Dispose();

完事 不会的可以留言询问 比较简单那

等风来,不如追风去。
原文地址:https://www.cnblogs.com/ning-xiaowo/p/14830670.html