赞
踩
调用处代码:
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++;
}
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。