标签:创建文件 ESS ntc ica core cond 建行 dir webroot
[HttpPost]
        public ActionResult ExpEleAcc(string linknos)
        {
            string filenname = null;
            CommonResult<string> result = new CommonResult<string>();
            try
            {
                string path = WebSettings.TempDirectory+ "Download" ;
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
               string template = "****导出模板.xls";
               string  tempaltePath = $"{env.WebRootPath}{Path.DirectorySeparatorChar}Template{Path.DirectorySeparatorChar}Export{Path.DirectorySeparatorChar}Dec导出模板{Path.DirectorySeparatorChar}{template}";
                string guid = CommonHelper.GetGuid();
                filenname = Path.Combine(path, "电子底账" + guid + ".xls");
                FileInfo info = new FileInfo(tempaltePath);
                info.CopyTo(filenname);
                HSSFWorkbook hssfworkbookDown;
                //读入刚复制的要导出的excel文件
                using (FileStream file = new FileStream(filenname, FileMode.Open, FileAccess.Read))
                {
                    hssfworkbookDown = new HSSFWorkbook(file);
                    file.Close();
                }
                DecEHeadService DecEHeadService = new DecEHeadService();
                //file = new FileStream(filenname, FileMode.Open, FileAccess.ReadWrite);
                List<ExpLedgerField> FirstSheetList = DecEHeadService.GetFirstSheetList(linknos, _appUser.CurrentCorpInfo.Site);
                List<ExpLedgerField> SecondSheetList = DecEHeadService.GetSecondSheetList(linknos, _appUser.CurrentCorpInfo.Site);
                List<ExpLedgerField> LastSheetList = DecEHeadService.GetLastSheetList(linknos, _appUser.CurrentCorpInfo.Site);
                // HSSFSheet sheet = (HSSFSheet)hssfworkbookDown.GetSheetAt(0);
                //通过sheet 名字得到sheet页
                // HSSFWorkbook workbook = new HSSFWorkbook();
               // HSSFSheet sheet = (HSSFSheet)workbook.GetSheet("sheet1");//读取名称为sheet1的sheet 
 
                HSSFSheet FirstSheet = (HSSFSheet)hssfworkbookDown.GetSheet("商品信息");
                HSSFSheet SecondSheet = (HSSFSheet)hssfworkbookDown.GetSheet("许可证信息");
                HSSFSheet LastSheet = (HSSFSheet)hssfworkbookDown.GetSheet("许可证VIN信息");
                // string create = "2" + "-" + (i + 1).ToString();
               // sheet.CopyTo(hssfworkbookDown, create, true, true);
                HSSFCellStyle cellstyle = (HSSFCellStyle)hssfworkbookDown.CreateCellStyle();
           
//第一个页签关于表体得循环
                for (int i = 0; i < FirstSheetList.Count; i++)
                {
//比较重要的一点  需要创建行
                    IRow row = FirstSheet.CreateRow(i+3);
                    if (FirstSheetList[i].G_NO!=null)
                    {
//获得行   
                        HSSFCell cell1 = (HSSFCell)FirstSheet.GetRow(i + 3).CreateCell(0);
                        cell1.SetCellValue(FirstSheetList[i].G_NO);
                    }
}
//第二个页签关于表体得循环
                for (int i = 0; i < SecondSheetList.Count; i++)
                {
                    IRow row = SecondSheet.CreateRow(i + 2);
                    if (SecondSheetList[i].G_NO != null)
                    {
                        HSSFCell cell1 = (HSSFCell)SecondSheet.GetRow(i + 2).CreateCell(0);
                        cell1.SetCellValue(SecondSheetList[i].G_NO);
                    }
                }
//第三个页签关于表体得循环
                for (int i = 0; i < LastSheetList.Count; i++)
                  {
                    IRow row = LastSheet.CreateRow(i + 2);
                    if (LastSheetList[i].G_NO != null)
                    {
                        HSSFCell cell1 = (HSSFCell)LastSheet.GetRow(i + 2).CreateCell(0);
                        cell1.SetCellValue(LastSheetList[i].G_NO);
                    }
}
                //创建文件
                FileStream files = new FileStream(filenname, FileMode.Create);
                hssfworkbookDown.Write(files);
                files.Close();
                files.Dispose();
                result.Success = true;
                // tempaltePath = $"{env.WebRootPath}{Path.DirectorySeparatorChar}Template{Path.DirectorySeparatorChar}Export{Path.DirectorySeparatorChar}Dec导出模板{Path.DirectorySeparatorChar}{template}";
                //tempaltePath  模板路径
                //filenname     下载路径
                //浏览器识别的路径:window.location.href="http://localhost:33795//\\Temp\Export\\导出模板_快递无LOT-20190928062051.xls";
                string host = "";
                // string host =this.Request.Host.Value + @"//\\Temp\\Download\\"+"电子底账" + guid + ".xls";
                if (Request.IsHttps == true)
                {
                    host = @"https://" + this.Request.Host.Value + @"//\\\temp\\Download\\" + "电子底账" + guid + ".xls";
                }
                else
                {
                    host = @"http://" + this.Request.Host.Value + @"//\\\temp\\Download\\" + "电子底账" + guid + ".xls";
                }
                result.Data = host;
               // return Json(result);
#region 拼接成浏览器识别的路径
//浏览器跳转路径: http://localhost:33795//\\\\Temp\\Export\\\\快递无LOT.NOT_NO-20190927015613.xls
//文件所在的实际物理路径(绝对路径) D:\new v3\src\Happ.Web\Happ.Web.UI\Temp\Export
//获取当前端口, 当前端口名称 , 文件所在文件夹/Temp/Export/aaa.xml 转换为浏览器识别的路径
               
                //Path = BaseController.Host + BaseController.ApplicationPath+ "\\\\Temp\\Export\\\\"+Path;
                //浏览器识别的路径:window.location.href="http://localhost:33795//\\Temp\Export\\导出模板_快递无LOT-20190928062051.xls";
                #endregion
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
            }
            return Json(result);
        }
关于简单的Excel多页签底层导出_电子底账导出为例(.net core)
标签:创建文件 ESS ntc ica core cond 建行 dir webroot
原文地址:https://www.cnblogs.com/ning-xiaowo/p/11888191.html