码迷,mamicode.com
首页 > 其他好文 > 详细

vue 实现带模板的EXCEL导出

时间:2021-06-02 18:30:37      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:单元格   exce   前端   time   pos   border   template   实现   ica   

话不多说直接上代码 

1.前端(个人逻辑做了Excel导出和world导出,world导出会在下一个博客中列出)

 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.后端

 string filenname = null;
                try
                {
                    string pathMsg = System.Web.HttpContext.Current.Server.MapPath("/"); //获得应用程序根目录所在的位置.
                    string path = pathMsg + @"Download";//文件复制到的位置
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    string tempaltePath = pathMsg + @"ExportTemplate/" + copyTempName; //要拷贝的模板
                    filenname = Path.Combine(path, expName + DateTime.Now.ToString("yyyyMMddHHmmssms") + @".xls");
                    FileInfo info = new FileInfo(tempaltePath);//获得要拷贝的模板
                    info.CopyTo(filenname);                    //将模板拷贝到指定位置
                    XSSFWorkbook hssfworkbookDown;
                    using (FileStream file = new FileStream(filenname, FileMode.Open, FileAccess.Read))
                    {
                        hssfworkbookDown = new XSSFWorkbook(file);
                        file.Close();
                    }
                    XSSFSheet FirstSheet = (XSSFSheet)hssfworkbookDown.GetSheet(sheetName);
//这里写操作逻辑 我给你写个对单元格操作的例子

                     XSSFCell cell1 = (XSSFCell)FirstSheet.GetRow(i + 4).CreateCell(0);
                     cell1.CellStyle.BorderBottom = BorderStyle.Thin;//设置单元格边框
                     cell1.SetCellValue(listMsg[i].USER_NAME);//单元格赋值


                    FileStream files = new FileStream(filenname, FileMode.Create);
                    hssfworkbookDown.Write(files);
                    MemoryStream ms = new MemoryStream();
                    hssfworkbookDown.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.xls");
                    System.Web.HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
                    System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel";
                    System.Web.HttpContext.Current.Response.BinaryWrite(ms.ToArray());
                    System.Web.HttpContext.Current.Response.End();
                    files.Close();
                    files.Dispose();

  这种写法会自动创建一个文件存在系统,留着备用核对啥的  也可以把创建方法去掉 直接导出

vue 实现带模板的EXCEL导出

标签:单元格   exce   前端   time   pos   border   template   实现   ica   

原文地址:https://www.cnblogs.com/ning-xiaowo/p/14830651.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!