当前位置:   article > 正文

.Net导出EXcel(多sheet)_导出多个sheet .netcode

导出多个sheet .netcode

调用处代码:
public ActionResult ExportDatasToExcel()
{
//指定项目存放的路径
string sWebRootFolder = AppDomain.CurrentDomain.BaseDirectory + “DownLoadExcel”;
//指定项目名字
string sFileName = “表格名称”;
//文件是否存在
var filenames = Directory.GetFiles(sWebRootFolder, “*.xlsx”);
int filecount = 0;
foreach (var nameitem in filenames)
{
if (nameitem.Contains(sFileName))
{
filecount++;
}
}
sFileName += “(” + filecount + “)” + “.xlsx”;
//把项目名加到指定存放的路径
FileInfo file = new FileInfo(Path.Combine(sWebRootFolder, sFileName));
using (ExcelPackage package = new ExcelPackage(file))
{
var stocklist= Service.GetTeamStockInfoList("");
//第一个sheet
List headerforstock = new List { “列名1”, “列名2”, “列名3”, “列名4”, “列名5” };
Service.CreatCells(“sheet标识1”, stocklist, package, headerforstock,“sheet1”);
//第二个sheet
List headerforinstock = new List { “列名1”, “列名2”, “列名3”, “列名4”, “列名5” };
Service.CreatCells(“sheet标识2”, stocklist, package, headerforinstock, “sheet2”);
//第三个sheet
Service.CreatCells(“sheet标识名3”, stocklist, package, headerforinstock, “sheet3”);
package.Save();
}
//获取到存在项目中文件的路径
var fileUrl = Path.Combine(sWebRootFolder, sFileName);
//application/ms-excel为内部固定
return File(fileUrl, “application/ms-excel”, sFileName);
}
被调用方法:生成sheet数据:
/// TeamStock 团队总统计 CaseInStock 计入库存数据 CaseNotInStock 不计入库存数据
/// 团队库存列表
/// 创建的文件
/// 表格头部集合List
/// sheet名称
public void CreatCells(string sheettag, List teamstocklist, ExcelPackage package, List header, string sheetname)
{
//添加worksheet的名字
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add(sheetname);
for (var i = 0; i < header.Count; i++)
{
worksheet.Cells[1, i + 1].Value = header[i];
}
//添加值
var rowNum = 2; // rowNum 1 is head
var listNotinstockTotal = new List();
//构建需要的导出数据
if (sheettag == “sheet标识2” || sheettag == “sheet标识3”)
{
foreach (var item in teamstocklist)
{
var listnotinstock = new List();
else if (sheettag == “sheet标识1”)
{
listnotinstock = GetTeamNotInStockDetail("", item.TeamID);
}
foreach (var notinstockitem in listnotinstock)
{
notinstockitem.TeamName = item.TeamName;
listNotinstockTotal.Add(notinstockitem);
}
}
}
//
if (sheettag == “sheet标识1”)
{
foreach (var item in teamstocklist)
{
worksheet.Cells[“A” + rowNum].Value = item.TeamName;
worksheet.Cells[“B” + rowNum].Value = item.TeamStockCount;
worksheet.Cells[“C” + rowNum].Value = item.InvalidTeamStockCount;
worksheet.Cells[“D” + rowNum].Value = item.ClosedTeamStockCount;
worksheet.Cells[“E” + rowNum].Value = item.TotalTeamStockCount;
rowNum++;
}
}
//
else if (sheettag.ToString() != “”)
{
foreach (var item in listNotinstockTotal)
{
worksheet.Cells[“A” + rowNum].Value = item.TeamName;
worksheet.Cells[“B” + rowNum].Value = item.CaseNO;
worksheet.Cells[“C” + rowNum].Value = item.CaseReason;
worksheet.Cells[“D” + rowNum].Value = item.CaseNatureName;
worksheet.Cells[“E” + rowNum].Value = item.Party;
rowNum++;
}
}
}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/216037
推荐阅读
相关标签
  

闽ICP备14008679号