關於簡單的Excel多頁籤底層導出_電子底帳導出爲例(.net core)

[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信息");app

// string create = "2" + "-" + (i + 1).ToString();
// sheet.CopyTo(hssfworkbookDown, create, true, true);
HSSFCellStyle cellstyle = (HSSFCellStyle)hssfworkbookDown.CreateCellStyle();
ui

//第一個頁籤關於表體得循環
for (int i = 0; i < FirstSheetList.Count; i++)
{this

//比較重要的一點  須要建立行
IRow row = FirstSheet.CreateRow(i+3);
if (FirstSheetList[i].G_NO!=null)
{excel

//得到行   
HSSFCell cell1 = (HSSFCell)FirstSheet.GetRow(i + 3).CreateCell(0);
cell1.SetCellValue(FirstSheetList[i].G_NO);
}xml

}string

//第二個頁籤關於表體得循環it

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);
}
}io

//第三個頁籤關於表體得循環

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

相關文章
相關標籤/搜索