zoukankan      html  css  js  c++  java
  • vue 实现带模板的EXCEL导出

    话不多说直接上代码 

    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);
    //这里写操作逻辑 我给你写个对单元格操作的例子
     IRow row = FirstSheet.CreateRow(i + 4); //别忘了创建行

                         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();

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

    等风来,不如追风去。
  • 相关阅读:
    斐波那契数
    组合数学
    网络流
    UVA 1104 【芯片难题 Chips Challenge】
    Luogu P3181 【[HAOI2016]找相同字符】
    Luogu P4101 【[HEOI2014]人人尽说江南好 】
    Luogu P5842 【[SCOI2012]Blinker 的仰慕者】
    BZOJ 4502 串
    Luogu P5840 【[COCI2015]Divljak】
    Luogu P3295 【[SCOI2016]萌萌哒】
  • 原文地址:https://www.cnblogs.com/ning-xiaowo/p/14830651.html
Copyright © 2011-2022 走看看