.net core 导出Excel(epplus 创建excel )
[Route("getopenfrequencyexcel")]
[HttpGet]
public IActionResult GetOpenFrequencyExcel(int projectid,int quetionnaireid,string demographicIds)
{
// HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
if (projectid > 0 )
{
string sWebRootFolder = _hostingEnvironment.WebRootPath; //core 获取路径
//hostingEnvironment.WebRootPath; XXX/wwwroot hostingEnvironment.ContentRootPath; XXX
string name = "选项频次.xlsx";
var path = Path.Combine(sWebRootFolder, name);
var file = new FileInfo(path);
using (var package = new ExcelPackage(file)) {
var count = package.Workbook.Worksheets.Count; if (count > 0) //删除已存在的文件
{
for (var i = 0; i < count; i++)
{
package.Workbook.Worksheets.Delete(i + 1);
}
package.File.Delete();
}
analysisService.CreateOptionExcel(demographicIds, projectid, quetionnaireid,package);//通过epplus 创建excel参考(https://www.cnblogs.com/rumeng/p/3785748.html)
}
var returnFile = File(name, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", name);
returnFile.FileDownloadName = name;
return returnFile;
}
else
{
return null;
}
}