导出excel功能开发

This commit is contained in:
范露尧
2023-06-30 11:37:23 +08:00
parent 7fa72212d4
commit cdd2235535
15 changed files with 12007 additions and 17 deletions

View File

@@ -72,15 +72,17 @@ namespace Vote.Services.ApiController
[HttpPost]
[UnitOfWork]
[Route("export_excel")]
public async Task<dynamic> ExportExcel(NbzcGetListInput args)
public async Task<dynamic> ExportExcel(OutsideWallQueryInput args)
{
var list = await QueryBuildings(args);
//var list = await repoutside_wall.AsQueryable().Where(a => !a.IsDeleted).OrderByDescending(a => a.CreatedTime).ToListAsync();
//var filepath = Tools.ExcelHelper.WriteExcelNingBoZhiChun(list);
//return new FileStreamResult(new FileStream(filepath, FileMode.Open), "application/octet-stream") { FileDownloadName = filepath };
var filepath = Tools.ExcelHelper.WriteExcelOutsideWallBuilding(list);
return new FileStreamResult(new FileStream(filepath, FileMode.Open), "application/octet-stream") { FileDownloadName = filepath };
return 1;
}
[HttpPost("querybuildings")]
public async Task<dynamic> QueryBuildings(OutsideWallQueryInput args)
public async Task<List<OutsideWallBuildingsOutput>> QueryBuildings(OutsideWallQueryInput args)
{
var entity = await repoutside_wall_building.AsQueryable().LeftJoin<outside_wall>((a, b) => a.outsidewallId == b.Id)
.WhereIF(!string.IsNullOrWhiteSpace(args.communityId), (a, b) => b.communityId == args.communityId)

View File

@@ -267,5 +267,77 @@ namespace Vote.Services.Tools
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public static string WriteExcelOutsideWallBuilding(List<OutsideWallBuildingsOutput> data)
{
try
{
string template_name = "外墙问卷调查模板.xlsx";
string excelFilePath = $"{App.WebHostEnvironment.WebRootPath}\\ExcelTemplate\\{template_name}";
string outputPath = string.Empty;
if (!string.IsNullOrEmpty(excelFilePath))
{
using (FileStream excelFileStream = System.IO.File.OpenRead(excelFilePath))
{
bool isCompatible = ExcelHelper.GetIsCompatible(excelFilePath);
IWorkbook workbook = ExcelHelper.CreateWorkbook(isCompatible, excelFileStream);
ISheet sheet = null;
sheet = workbook.GetSheetAt(0);
//从第几行开始 , 比如 行号是4 , 就写3
var startRowIndex = 1;
for (int i = 0; i < data.Count; i++)
{
var c_rowindex = startRowIndex + i;
var row = sheet.CreateRow(c_rowindex);
int cell_start_index = 0;
row.CreateCell(cell_start_index).SetCellValue(data[i].submitCode);
row.CreateCell(cell_start_index + 1).SetCellValue(data[i].communityName);
row.CreateCell(cell_start_index + 2).SetCellValue(data[i].createtime.Value.ToString("yyyy-MM-dd HH:mm:ss"));
row.CreateCell(cell_start_index + 3).SetCellValue(data[i].BuildingName);
row.CreateCell(cell_start_index + 4).SetCellValue(data[i].Address);
row.CreateCell(cell_start_index + 5).SetCellValue(data[i].LevelCount?.ToString());
row.CreateCell(cell_start_index + 6).SetCellValue(data[i].Households?.ToString());
row.CreateCell(cell_start_index + 7).SetCellValue(data[i].BuildingUnit);
row.CreateCell(cell_start_index + 8).SetCellValue(data[i].DesingerUnit);
row.CreateCell(cell_start_index + 9).SetCellValue(data[i].ConstructionUnit);
row.CreateCell(cell_start_index + 10).SetCellValue(data[i].MonitorUnit);
row.CreateCell(cell_start_index + 11).SetCellValue(data[i].WuYeUnit);
row.CreateCell(cell_start_index + 12).SetCellValue(data[i].curwallproblems);
row.CreateCell(cell_start_index + 13).SetCellValue(data[i].curwallproblemother);
row.CreateCell(cell_start_index + 14).SetCellValue(data[i].wallproblemsfirst);
row.CreateCell(cell_start_index + 15).SetCellValue(data[i].firstproblemdate);
row.CreateCell(cell_start_index + 16).SetCellValue(data[i].problemfrequency);
row.CreateCell(cell_start_index + 17).SetCellValue(data[i].problemseason);
row.CreateCell(cell_start_index + 18).SetCellValue(data[i].wallproblemtoward);
row.CreateCell(cell_start_index + 19).SetCellValue(data[i].problemfanwei);
row.CreateCell(cell_start_index + 20).SetCellValue(data[i].problemheight);
row.CreateCell(cell_start_index + 21).SetCellValue(data[i].diaoluowu);
}
var file = new FileInfo(excelFilePath);
var savePath = file.DirectoryName + "\\OutPut\\";
if (!Directory.Exists(savePath))
Directory.CreateDirectory(savePath);
outputPath = savePath + DateTime.Now.ToString("yyyyMMddHHmmsss") + "-" + template_name;
using (var filess = new FileStream(outputPath, FileMode.Create, FileAccess.Write, FileShare.Read))
{
workbook.Write(filess);
}
}
}
return outputPath;
}
catch (System.IO.IOException ioex)
{
throw Oops.Oh("文件被占用,请检查文件模板");
}
}
}
}

View File

@@ -113,7 +113,7 @@
<param name="_repoutside_wall_photo"></param>
<param name="memoryCache"></param>
</member>
<member name="M:Vote.Services.ApiController.OutsideWallService.ExportExcel(Vote.Services.Dto.NbzcGetListInput)">
<member name="M:Vote.Services.ApiController.OutsideWallService.ExportExcel(Vote.Services.Dto.OutsideWallQueryInput)">
<summary>
导出Excel
</summary>
@@ -979,6 +979,12 @@
</summary>
<returns></returns>
</member>
<member name="M:Vote.Services.Tools.ExcelHelper.WriteExcelOutsideWallBuilding(System.Collections.Generic.List{Vote.Services.Dto.OutsideWallBuildingsOutput})">
<summary>
</summary>
<returns></returns>
</member>
<member name="M:Vote.Services.Tools.HtmlHelper.GetHtmlSource2(System.String)">
<summary>方法一:比较推荐
用HttpWebRequest取得网页源码