[後端] 修改匯出程序, 更具需求更改成所有棟別資料, 修改監控系統列表排序

This commit is contained in:
dev02 2023-06-27 10:34:38 +08:00
parent dea56822c3
commit 8dbfa4938a
3 changed files with 311 additions and 280 deletions

View File

@ -176,7 +176,7 @@ namespace FrontendWebApi.ApiControllers
select distinct main_system_tag, sub_system_tag from building_menu where is_link = 1
) as bm on v2.system_value = bm.sub_system_tag and v1.system_value = bm.main_system_tag
where c.account = @account
order by v2.system_priority", new { @account = "webUser", @sub_system_type = sub_system_type, @main_system_type = main_system_type });
order by build.priority, v2.system_priority", new { @account = "webUser", @sub_system_type = sub_system_type, @main_system_type = main_system_type });
var building = dbsub.GroupBy(x => x.device_building_tag).ToList();
apiResult.Data = new List<History_Building>();

View File

@ -13,6 +13,7 @@ using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Ionic.Zip;
using System.Collections.Immutable;
namespace FrontendWebApi.ApiControllers
{
@ -408,17 +409,19 @@ namespace FrontendWebApi.ApiControllers
[Route("api/ExportElectricList")]
public FileResult OpeExportExcelElec([FromBody] HydroMeterInput input)
{
input = new HydroMeterInput();
input.building_tag = "G6";
input.floor_tag = new List<string>();
input.startTime = "2023-06";
input.tableType = "day";
var result = this.ElectricList(input).Result.Value.Data.ToList();
List<List<HydroMeterOutput>> result = new List<List<HydroMeterOutput>>();
var building = backendRepository.GetAllAsync<HydroBuildList>("select * from building where deleted = 0").Result;
foreach(var b in building)
{
input.building_tag = b.building_tag;
result.Add(this.ElectricList(input).Result.Value.Data.ToList());
}
List<Dictionary<string, byte[]>> docFile = new List<Dictionary<string, byte[]>>();
var workbook = new XSSFWorkbook();
#region excel設定
foreach (var r in result)
{
var workbook = new XSSFWorkbook();
#region excel設定
IFont font12 = workbook.CreateFont();
font12.FontName = "新細明體";
font12.FontHeightInPoints = 12;
@ -459,106 +462,101 @@ namespace FrontendWebApi.ApiControllers
stylein12.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
stylein12.WrapText = true;
#endregion
var sheet = workbook.CreateSheet("電表報表");
int RowPosition = 0;
if (result.Count > 0)
{
#region set cell
IRow row = sheet.CreateRow(RowPosition);
sheet.SetColumnWidth(0, 4 * 160 * 12);
sheet.SetColumnWidth(1, 4 * 160 * 12);
sheet.SetColumnWidth(2, 4 * 160 * 12);
int i = 0;
ICell cell = row.CreateCell(i++);
cell.SetCellValue("棟別");
cell.CellStyle = styleLine12;
cell = row.CreateCell(i++);
cell.SetCellValue("樓層");
cell.CellStyle = styleLine12;
cell = row.CreateCell(i++);
cell.SetCellValue("設備");
cell.CellStyle = styleLine12;
foreach (var rr in result.FirstOrDefault().rawData)
string buildingName = r.Select(x => x.building_name).FirstOrDefault();
var sheet = workbook.CreateSheet($"{buildingName}電表報表");
int RowPosition = 0;
if (result.Count > 0)
{
cell = row.CreateCell(i++);
cell.SetCellValue(rr.timeStamp);
#region set cell
IRow row = sheet.CreateRow(RowPosition);
sheet.SetColumnWidth(0, 4 * 160 * 12);
sheet.SetColumnWidth(1, 4 * 160 * 12);
sheet.SetColumnWidth(2, 4 * 160 * 12);
int i = 0;
ICell cell = row.CreateCell(i++);
cell.SetCellValue("棟別");
cell.CellStyle = styleLine12;
}
cell = row.CreateCell(i++);
cell.SetCellValue("小計");
cell.CellStyle = styleLine12;
cell = row.CreateCell(i++);
cell.SetCellValue("單價");
cell.CellStyle = styleLine12;
cell = row.CreateCell(i++);
cell.SetCellValue("金額總計");
cell.CellStyle = styleLine12;
#endregion
foreach (var r in result)
{
RowPosition += 1;
int k = 3;
row = sheet.CreateRow(RowPosition);
for (int j = 0; j <= i; j++)
cell = row.CreateCell(i++);
cell.SetCellValue("樓層");
cell.CellStyle = styleLine12;
cell = row.CreateCell(i++);
cell.SetCellValue("設備");
cell.CellStyle = styleLine12;
foreach (var rr in r.FirstOrDefault().rawData)
{
cell = row.CreateCell(j);
if (j == 0)
{
cell.SetCellValue(r.building_name);
}
if (j == 1)
{
cell.SetCellValue(r.floor_tag);
}
if (j == 2)
{
cell.SetCellValue(r.device_full_name);
}
if (j == 3)
{
foreach (var rr in r.rawData)
{
cell.SetCellValue(rr.avg_rawdata.ToString());
j++;
k++;
cell = row.CreateCell(j);
}
}
if (j == k)
{
cell.SetCellValue(r.total);
}
if (j == k+1)
{
cell.SetCellValue(r.price);
}
if (j == k+2)
{
cell.SetCellValue(r.total_price);
}
cell = row.CreateCell(i++);
cell.SetCellValue(rr.timeStamp);
cell.CellStyle = styleLine12;
}
cell.CellStyle = style12;
cell = row.CreateCell(i++);
cell.SetCellValue("小計");
cell.CellStyle = styleLine12;
cell = row.CreateCell(i++);
cell.SetCellValue("單價");
cell.CellStyle = styleLine12;
cell = row.CreateCell(i++);
cell.SetCellValue("金額總計");
cell.CellStyle = styleLine12;
#endregion
foreach (var rr in r)
{
RowPosition += 1;
int k = 3;
row = sheet.CreateRow(RowPosition);
for (int j = 0; j <= i; j++)
{
cell = row.CreateCell(j);
if (j == 0)
{
cell.SetCellValue(rr.building_name);
}
if (j == 1)
{
cell.SetCellValue(rr.floor_tag);
}
if (j == 2)
{
cell.SetCellValue(rr.device_full_name);
}
if (j == 3)
{
foreach (var rrr in rr.rawData)
{
cell.SetCellValue(rrr.avg_rawdata.ToString());
j++;
k++;
cell = row.CreateCell(j);
}
}
if (j == k)
{
cell.SetCellValue(rr.total);
}
if (j == k+1)
{
cell.SetCellValue(rr.price);
}
if (j == k+2)
{
cell.SetCellValue(rr.total_price);
}
cell.CellStyle = style12;
}
}
}
MemoryStream ms = new MemoryStream();
workbook.Write(ms);
docFile.Add(new Dictionary<string, byte[]>() { { $"{buildingName}電表報表.xlsx", ms.ToArray() } });
}
var ms = new NpoiMemoryStream
{
AllowClose = false
};
workbook.Write(ms);
ms.Flush();
ms.Seek(0, SeekOrigin.Begin);
docFile.Add(new Dictionary<string, byte[]>() { { "電表報表", ms.ToArray() } });
var memoryStream = new MemoryStream();
var zipMs = new MemoryStream();
using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile(System.Text.Encoding.UTF8))
{
foreach(var d in docFile)
@ -568,63 +566,32 @@ namespace FrontendWebApi.ApiControllers
zip.AddEntry(dic.Key, dic.Value);
}
}
zip.Save(memoryStream);
zip.Save(zipMs);
}
Response.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition");
return File(memoryStream, "application/octet-stream", "電表報表.zip");
return File(zipMs.ToArray(), "application/octet-stream", "電表報表.zip");
}
[HttpPost]
[Route("api/ExportElectricCompareList")]
public FileResult OpeExportCompareExcelElec([FromBody] List<HydroMeterInput> input)
{
var result = new List<HydroMeterOutput>();
var result1 = this.ElectricList(input[0]).Result.Value.Data.ToList();
var result2 = this.ElectricList(input[1]).Result.Value.Data.ToList();
List<HydroMeterOutput> result = new List<HydroMeterOutput>();
List<List<HydroMeterOutput>> result1 = new List<List<HydroMeterOutput>>();
List<List<HydroMeterOutput>> result2 = new List<List<HydroMeterOutput>>();
List<Dictionary<string, byte[]>> docFile = new List<Dictionary<string, byte[]>>();
var building = backendRepository.GetAllAsync<HydroBuildList>("select * from building where deleted = 0").Result;
foreach (var b in building)
{
input[0].building_tag = b.building_tag;
input[1].building_tag = b.building_tag;
result1.Add(this.ElectricList(input[0]).Result.Value.Data.ToList());
result2.Add(this.ElectricList(input[1]).Result.Value.Data.ToList());
}
Dictionary<string, List<string>> compareDict = new Dictionary<string, List<string>>();
var workbook = new XSSFWorkbook();
#region excel設定
IFont font12 = workbook.CreateFont();
font12.FontName = "新細明體";
font12.FontHeightInPoints = 12;
ICellStyle style12 = workbook.CreateCellStyle();
style12.SetFont(font12);
style12.Alignment = HorizontalAlignment.Center;
style12.VerticalAlignment = VerticalAlignment.Center;
IFont font12Times = workbook.CreateFont();
font12Times.FontName = "Times New Roman";
font12Times.FontHeightInPoints = 12;
IFont font18 = workbook.CreateFont();
font18.FontName = "新細明體";
font18.FontHeightInPoints = 18;
font18.IsBold = true;
ICellStyle styleTitle18 = workbook.CreateCellStyle();
styleTitle18.SetFont(font18);
styleTitle18.Alignment = HorizontalAlignment.Center;
styleTitle18.VerticalAlignment = VerticalAlignment.Center;
ICellStyle styleLeft12 = workbook.CreateCellStyle();
styleLeft12.SetFont(font12);
styleLeft12.Alignment = HorizontalAlignment.Left;
styleLeft12.VerticalAlignment = VerticalAlignment.Center;
ICellStyle styleLine12 = workbook.CreateCellStyle();
styleLine12.SetFont(font12);
styleLine12.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
styleLine12.VerticalAlignment = VerticalAlignment.Center;
styleLine12.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
styleLine12.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
styleLine12.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
styleLine12.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
ICellStyle stylein12 = workbook.CreateCellStyle();
stylein12.SetFont(font12Times);
stylein12.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;
stylein12.VerticalAlignment = VerticalAlignment.Center;
stylein12.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
stylein12.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
stylein12.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
stylein12.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
stylein12.WrapText = true;
#endregion
var rawDataTitle = new List<string>();
if (input[0].tableType == "month")
@ -644,151 +611,209 @@ namespace FrontendWebApi.ApiControllers
}
}
var sheet = workbook.CreateSheet("電表報表");
int RowPosition = 0;
if (result1.Count > 0 || result2.Count > 0)
foreach (var b in building)
{
result = result1;
foreach (var r2 in result2)
var res1 = result1.Where(x => x.Select(x => x.building_tag).Contains(b.building_tag)).ToList();
var res2 = result2.Where(x => x.Select(x => x.building_tag).Contains(b.building_tag)).ToList();
if (res1.Count > 0 || res2.Count > 0)
{
var target = result.Where(r => r.building_tag == r2.building_tag && r.device_serial_tag == r2.device_serial_tag && r.floor_tag == r2.floor_tag).FirstOrDefault();
compareDict[r2.building_tag + r2.floor_tag + r2.device_serial_tag] = new List<string>() { r2.total, r2.price, r2.total_price };
if (target != null)
var workbook = new XSSFWorkbook();
#region excel設定
IFont font12 = workbook.CreateFont();
font12.FontName = "新細明體";
font12.FontHeightInPoints = 12;
ICellStyle style12 = workbook.CreateCellStyle();
style12.SetFont(font12);
style12.Alignment = HorizontalAlignment.Center;
style12.VerticalAlignment = VerticalAlignment.Center;
IFont font12Times = workbook.CreateFont();
font12Times.FontName = "Times New Roman";
font12Times.FontHeightInPoints = 12;
IFont font18 = workbook.CreateFont();
font18.FontName = "新細明體";
font18.FontHeightInPoints = 18;
font18.IsBold = true;
ICellStyle styleTitle18 = workbook.CreateCellStyle();
styleTitle18.SetFont(font18);
styleTitle18.Alignment = HorizontalAlignment.Center;
styleTitle18.VerticalAlignment = VerticalAlignment.Center;
ICellStyle styleLeft12 = workbook.CreateCellStyle();
styleLeft12.SetFont(font12);
styleLeft12.Alignment = HorizontalAlignment.Left;
styleLeft12.VerticalAlignment = VerticalAlignment.Center;
ICellStyle styleLine12 = workbook.CreateCellStyle();
styleLine12.SetFont(font12);
styleLine12.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
styleLine12.VerticalAlignment = VerticalAlignment.Center;
styleLine12.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
styleLine12.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
styleLine12.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
styleLine12.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
ICellStyle stylein12 = workbook.CreateCellStyle();
stylein12.SetFont(font12Times);
stylein12.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;
stylein12.VerticalAlignment = VerticalAlignment.Center;
stylein12.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
stylein12.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
stylein12.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
stylein12.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
stylein12.WrapText = true;
#endregion
var sheet = workbook.CreateSheet($"{b.full_name}電表報表");
int RowPosition = 0;
result = res1.FirstOrDefault() ?? new List<HydroMeterOutput>();
var r2 = res2.FirstOrDefault() ?? new List<HydroMeterOutput>();
foreach (var r in r2)
{
target.rawData = target.rawData.Concat(r2.rawData).ToList();
var target = result.Where(x => x.building_tag == r.building_tag && x.device_serial_tag == r.device_serial_tag && x.floor_tag == r.floor_tag && x.device_number == r.device_number).FirstOrDefault();
compareDict[r.building_tag + r.floor_tag + r.device_serial_tag + r.device_number] = new List<string>() { r.total, r.price, r.total_price };
if (target != null)
{
target.rawData = target.rawData.Concat(r.rawData).ToList();
}
else
{
r.total = null;
r.price = null;
r.total_price = null;
result.Add(r);
}
}
else {
r2.total = null;
r2.price = null;
r2.total_price= null;
result.Add(r2);
}
}
#region set cell
IRow row = sheet.CreateRow(RowPosition);
sheet.SetColumnWidth(0, 4 * 160 * 12);
sheet.SetColumnWidth(1, 4 * 160 * 12);
sheet.SetColumnWidth(2, 4 * 160 * 12);
#region set cell
IRow row = sheet.CreateRow(RowPosition);
sheet.SetColumnWidth(0, 4 * 160 * 12);
sheet.SetColumnWidth(1, 4 * 160 * 12);
sheet.SetColumnWidth(2, 4 * 160 * 12);
int i = 0;
ICell cell = row.CreateCell(i++);
cell.SetCellValue("棟別");
cell.CellStyle = styleLine12;
cell = row.CreateCell(i++);
cell.SetCellValue("樓層");
cell.CellStyle = styleLine12;
cell = row.CreateCell(i++);
cell.SetCellValue("設備");
cell.CellStyle = styleLine12;
foreach (var rr in rawDataTitle)
{
sheet.SetColumnWidth(i, 2 * 160 * 12);
cell = row.CreateCell(i++);
cell.SetCellValue(rr);
int i = 0;
ICell cell = row.CreateCell(i++);
cell.SetCellValue("棟別");
cell.CellStyle = styleLine12;
cell = row.CreateCell(i++);
cell.SetCellValue("樓層");
cell.CellStyle = styleLine12;
cell = row.CreateCell(i++);
cell.SetCellValue("設備");
cell.CellStyle = styleLine12;
}
cell = row.CreateCell(i++);
cell.SetCellValue(input[0].startTime + "小計");
sheet.SetColumnWidth(i-1, 2 * 160 * 12);
cell.CellStyle = styleLine12;
cell = row.CreateCell(i++);
cell.SetCellValue(input[1].startTime + "小計");
sheet.SetColumnWidth(i - 1, 2 * 160 * 12);
cell.CellStyle = styleLine12;
cell = row.CreateCell(i++);
cell.SetCellValue(input[0].startTime + "單價");
sheet.SetColumnWidth(i - 1, 2 * 160 * 12);
cell.CellStyle = styleLine12;
cell = row.CreateCell(i++);
cell.SetCellValue(input[1].startTime + "單價");
sheet.SetColumnWidth(i - 1, 2 * 160 * 12);
cell.CellStyle = styleLine12;
cell = row.CreateCell(i++);
cell.SetCellValue(input[0].startTime + "金額總計");
sheet.SetColumnWidth(i - 1, 2 * 160 * 12);
cell.CellStyle = styleLine12;
cell = row.CreateCell(i++);
cell.SetCellValue(input[1].startTime + "金額總計");
sheet.SetColumnWidth(i - 1, 2 * 160 * 12);
cell.CellStyle = styleLine12;
#endregion
foreach (var r in result)
{
string compareDictKey = r.building_tag + r.floor_tag + r.device_serial_tag;
RowPosition += 1;
int k = 3;
row = sheet.CreateRow(RowPosition);
for (int j = 0; j <= i; j++)
foreach (var rr in rawDataTitle)
{
cell = row.CreateCell(j);
if (j == 0)
{
cell.SetCellValue(r.building_name);
}
if (j == 1)
{
cell.SetCellValue(r.floor_tag);
}
if (j == 2)
{
cell.SetCellValue(r.device_serial_tag);
}
if (j == 3)
{
foreach (var rr in rawDataTitle)
{
var targetRaw = r.rawData.Where(rd => rd.timeStamp == rr).FirstOrDefault();
cell.SetCellValue(targetRaw?.avg_rawdata.ToString() ?? "");
j++;
k++;
cell = row.CreateCell(j);
}
}
if (j == k)
{
cell.SetCellValue(r.total);
}
if (j == k + 1)
{
cell.SetCellValue(compareDict.ContainsKey(compareDictKey) ? compareDict[compareDictKey][0] : "");
}
if (j == k + 2)
{
cell.SetCellValue(r.price);
}
if (j == k + 3)
{
cell.SetCellValue(compareDict.ContainsKey(compareDictKey) ? compareDict[compareDictKey][1] : "");
}
if (j == k + 4)
{
cell.SetCellValue(r.total_price);
}
if (j == k + 5)
{
cell.SetCellValue(compareDict.ContainsKey(compareDictKey) ? compareDict[compareDictKey][2] : "");
}
cell.CellStyle = style12;
sheet.SetColumnWidth(i, 2 * 160 * 12);
cell = row.CreateCell(i++);
cell.SetCellValue(rr);
cell.CellStyle = styleLine12;
}
cell = row.CreateCell(i++);
cell.SetCellValue(input[0].startTime + "小計");
sheet.SetColumnWidth(i - 1, 2 * 160 * 12);
cell.CellStyle = styleLine12;
cell = row.CreateCell(i++);
cell.SetCellValue(input[1].startTime + "小計");
sheet.SetColumnWidth(i - 1, 2 * 160 * 12);
cell.CellStyle = styleLine12;
cell = row.CreateCell(i++);
cell.SetCellValue(input[0].startTime + "單價");
sheet.SetColumnWidth(i - 1, 2 * 160 * 12);
cell.CellStyle = styleLine12;
cell = row.CreateCell(i++);
cell.SetCellValue(input[1].startTime + "單價");
sheet.SetColumnWidth(i - 1, 2 * 160 * 12);
cell.CellStyle = styleLine12;
cell = row.CreateCell(i++);
cell.SetCellValue(input[0].startTime + "金額總計");
sheet.SetColumnWidth(i - 1, 2 * 160 * 12);
cell.CellStyle = styleLine12;
cell = row.CreateCell(i++);
cell.SetCellValue(input[1].startTime + "金額總計");
sheet.SetColumnWidth(i - 1, 2 * 160 * 12);
cell.CellStyle = styleLine12;
#endregion
foreach (var r in result)
{
string compareDictKey = r.building_tag + r.floor_tag + r.device_serial_tag + r.device_number;
RowPosition += 1;
int k = 3;
row = sheet.CreateRow(RowPosition);
for (int j = 0; j <= i; j++)
{
cell = row.CreateCell(j);
if (j == 0)
{
cell.SetCellValue(r.building_name);
}
if (j == 1)
{
cell.SetCellValue(r.floor_tag);
}
if (j == 2)
{
cell.SetCellValue(r.device_full_name);
}
if (j == 3)
{
foreach (var rr in rawDataTitle)
{
var targetRaw = r.rawData.Where(rd => rd.timeStamp == rr).FirstOrDefault();
cell.SetCellValue(targetRaw?.avg_rawdata.ToString() ?? "");
j++;
k++;
cell = row.CreateCell(j);
}
}
if (j == k)
{
cell.SetCellValue(r.total);
}
if (j == k + 1)
{
cell.SetCellValue(compareDict.ContainsKey(compareDictKey) ? compareDict[compareDictKey][0] : "");
}
if (j == k + 2)
{
cell.SetCellValue(r.price);
}
if (j == k + 3)
{
cell.SetCellValue(compareDict.ContainsKey(compareDictKey) ? compareDict[compareDictKey][1] : "");
}
if (j == k + 4)
{
cell.SetCellValue(r.total_price);
}
if (j == k + 5)
{
cell.SetCellValue(compareDict.ContainsKey(compareDictKey) ? compareDict[compareDictKey][2] : "");
}
cell.CellStyle = style12;
}
}
MemoryStream ms = new MemoryStream();
workbook.Write(ms);
docFile.Add(new Dictionary<string, byte[]>() { { $"{b.full_name}電表報表.xlsx", ms.ToArray() } });
}
}
var ms = new NpoiMemoryStream
var zipMs = new MemoryStream();
using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile(System.Text.Encoding.UTF8))
{
AllowClose = false
};
workbook.Write(ms);
ms.Flush();
ms.Seek(0, SeekOrigin.Begin);
foreach (var d in docFile)
{
foreach (var dic in d)
{
zip.AddEntry(dic.Key, dic.Value);
}
}
zip.Save(zipMs);
}
Response.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition");
return File(ms, "application/vnd.ms-excel", "電表報表.xlsx");
return File(zipMs.ToArray(), "application/octet-stream", "電表報表.zip");
}
public static int GetDayInMonth(string yearMonth)

View File

@ -54,4 +54,10 @@ namespace FrontendWebApi.Models
{
public List<HydroMeterCompareOutput> exportList { get; set; }
}
public class HydroBuildList
{
public string full_name { get; set; }
public string building_tag { get; set; }
}
}