Skip to content

Commit 1c2dc3f

Browse files
committed
fix: 同一行多表格导出,出现错误模板
1 parent 45251a1 commit 1c2dc3f

File tree

4 files changed

+81
-20
lines changed

4 files changed

+81
-20
lines changed

src/Magicodes.ExporterAndImporter.Excel/Utility/TemplateExport/TemplateExportHelper.cs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,20 @@
1212
// ======================================================================
1313

1414
using DynamicExpresso;
15-
using Magicodes.ExporterAndImporter.Core;
1615
using Magicodes.ExporterAndImporter.Core.Extension;
1716
using Magicodes.IE.Core;
17+
using Magicodes.IE.EPPlus;
18+
using Magicodes.IE.Excel.Images;
1819
using OfficeOpenXml;
1920
using OfficeOpenXml.Drawing;
21+
using SixLabors.ImageSharp;
22+
using SixLabors.ImageSharp.Formats;
2023
using System;
2124
using System.Collections.Generic;
2225
using System.Collections.Specialized;
2326
using System.IO;
2427
using System.Linq;
2528
using System.Text.RegularExpressions;
26-
using Magicodes.IE.Excel.Images;
27-
using SixLabors.ImageSharp;
28-
using SixLabors.ImageSharp.Formats;
29-
using SkiaSharp;
30-
using Magicodes.IE.EPPlus;
31-
using System.Reflection;
3229

3330
namespace Magicodes.ExporterAndImporter.Excel.Utility.TemplateExport
3431
{
@@ -319,6 +316,8 @@ private void RenderTable(Interpreter target, Parameter[] tbParameters, string sh
319316
foreach (var itemTable in item)
320317
{
321318
itemTable.NewRowStart += insertRows;
319+
// 如果一行有多表格则记录最大行数,用于后续清理多余的行
320+
itemTable.SameRowMaxRowCount = table.RowCount;
322321
}
323322
}
324323
else
@@ -371,7 +370,7 @@ private void RenderTable(Interpreter target, Parameter[] tbParameters, string sh
371370
continue;
372371
}
373372

374-
RenderTableCells(target, tbParameters, sheet, table.NewRowStart - table.RawRowStart, tableKey, table.RowCount, col, address);
373+
RenderTableCells(target, tbParameters, sheet, table.NewRowStart - table.RawRowStart, tableKey, table.RowCount, col, address, table.SameRowMaxRowCount);
375374
}
376375
}
377376
}
@@ -438,7 +437,8 @@ private static void RenderRowsHeight(ExcelWorksheet sheet)
438437
/// <param name="rowCount"></param>
439438
/// <param name="writer"></param>
440439
/// <param name="address"></param>
441-
private void RenderTableCells(Interpreter target, Parameter[] tbParameters, ExcelWorksheet sheet, int insertRows, string tableKey, int rowCount, IWriter writer, ExcelAddressBase address)
440+
/// <param name="sameRowMaxRowCount"></param>
441+
private void RenderTableCells(Interpreter target, Parameter[] tbParameters, ExcelWorksheet sheet, int insertRows, string tableKey, int rowCount, IWriter writer, ExcelAddressBase address, int? sameRowMaxRowCount = null)
442442
{
443443
var cellString = writer.CellString;
444444
if (cellString.Contains("{{Table>>"))
@@ -448,7 +448,7 @@ private void RenderTableCells(Interpreter target, Parameter[] tbParameters, Exce
448448
//{{Remark|>>Table}}
449449
cellString = cellString.Split('|')[0].Trim() + "}}";
450450

451-
RenderTableCells(target, tbParameters, sheet, insertRows, tableKey, rowCount, cellString, address);
451+
RenderTableCells(target, tbParameters, sheet, insertRows, tableKey, rowCount, cellString, address, sameRowMaxRowCount);
452452
}
453453

454454
/// <summary>
@@ -529,7 +529,8 @@ private void RenderCell(Interpreter target, ExcelWorksheet sheet, string express
529529
/// <param name="rowCount"></param>
530530
/// <param name="cellString"></param>
531531
/// <param name="address"></param>
532-
private void RenderTableCells(Interpreter target, Parameter[] parameters, ExcelWorksheet sheet, int insertRows, string tableKey, int rowCount, string cellString, ExcelAddressBase address)
532+
/// <param name="sameRowMaxRowCount"></param>
533+
private void RenderTableCells(Interpreter target, Parameter[] parameters, ExcelWorksheet sheet, int insertRows, string tableKey, int rowCount, string cellString, ExcelAddressBase address, int? sameRowMaxRowCount = null)
533534
{
534535
//var dataVar = !IsDynamicSupportTypes ? ("\" + data." + tableKey + "[index].") : ("\" + data[\"" + tableKey + "\"][index]");
535536
string dataVar;
@@ -555,6 +556,17 @@ private void RenderTableCells(Interpreter target, Parameter[] parameters, ExcelW
555556
sheet.Row(rowIndex).Height = sheet.Row(address.Start.Row).Height;
556557
RenderCell(target, sheet, cellString, targetAddress.Address, dataVar, null, parameters, i);
557558
}
559+
560+
if (sameRowMaxRowCount.HasValue && sameRowMaxRowCount.Value > rowCount)
561+
{
562+
// 清理多余的行
563+
for (var i = rowCount; i < sameRowMaxRowCount.Value; i++)
564+
{
565+
var rowIndex = address.Start.Row + i + insertRows;
566+
var targetAddress = new ExcelAddress(rowIndex, address.Start.Column, rowIndex, address.Start.Column);
567+
sheet.Cells[targetAddress.Address].Clear();
568+
}
569+
}
558570
}
559571

560572
/// <summary>

src/Magicodes.ExporterAndImporter.Excel/Utility/TemplateExport/TemplateTableInfo.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
1+
using System.Linq;
62

73
namespace Magicodes.ExporterAndImporter.Excel.Utility.TemplateExport
84
{
@@ -35,5 +31,10 @@ public class TemplateTableInfo
3531
/// 写入器
3632
/// </summary>
3733
public IGrouping<string, IWriter> Writers { get; set; }
34+
35+
/// <summary>
36+
/// 同行最大行数
37+
/// </summary>
38+
public int? SameRowMaxRowCount { get; set; } = null;
3839
}
3940
}

src/Magicodes.ExporterAndImporter.Tests/ExcelExporter_Tests.cs

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,13 @@
1616
using Magicodes.ExporterAndImporter.Excel;
1717
using Magicodes.ExporterAndImporter.Tests.Extensions;
1818
using Magicodes.ExporterAndImporter.Tests.Models.Export;
19-
using Magicodes.ExporterAndImporter.Tests.Models.Export.ExportByTemplate_Test1;
2019
using Magicodes.IE.Core;
21-
using Newtonsoft.Json.Linq;
2220
using OfficeOpenXml;
2321
using OfficeOpenXml.Drawing;
2422
using OfficeOpenXml.Style;
2523
using Shouldly;
2624
using System;
2725
using System.Collections.Generic;
28-
using System.ComponentModel;
29-
using System.ComponentModel.DataAnnotations;
3026
using System.Data;
3127
using System.Dynamic;
3228
using System.IO;
@@ -1094,5 +1090,57 @@ await Task.Run(async () =>
10941090
await exporter.ExportByTemplate(filePath, data, tplPath);
10951091
});
10961092
}
1093+
1094+
[Fact(DisplayName = "一行不同表行数测试")]
1095+
public async Task ExportWithSameRowMultiTable_Test()
1096+
{
1097+
//模板路径
1098+
var tplPath = Path.Combine(Directory.GetCurrentDirectory(), "TestFiles", "ExportTemplates", "ExportWithSameRowMultiTable.xlsx");
1099+
//创建Excel导出对象
1100+
IExportFileByTemplate exporter = new ExcelExporter();
1101+
//导出路径
1102+
var filePath = GetTestFilePath($"{nameof(ExportWithSameRowMultiTable_Test)}.xlsx");
1103+
if (File.Exists(filePath)) File.Delete(filePath);
1104+
//根据模板导出
1105+
var result =
1106+
await exporter.ExportByTemplate(filePath, new
1107+
{
1108+
ListA = new List<ExportTestDataWithSingleCol>()
1109+
{
1110+
new(){
1111+
Name = "A1"
1112+
},
1113+
new(){
1114+
Name = "A2"
1115+
},
1116+
new(){
1117+
Name = "A3"
1118+
}
1119+
},
1120+
ListB = new List<ExportTestDataWithSingleCol>(){
1121+
new(){
1122+
Name = "B1"
1123+
},
1124+
new(){
1125+
Name = "B2"
1126+
}
1127+
}
1128+
}, tplPath);
1129+
1130+
result.ShouldNotBeNull();
1131+
File.Exists(filePath).ShouldBeTrue();
1132+
1133+
using (var pck = new ExcelPackage(new FileInfo(filePath)))
1134+
{
1135+
pck.Workbook.Worksheets.Count.ShouldBe(1);
1136+
var sheet = pck.Workbook.Worksheets.First();
1137+
sheet.Cells["A2"].Text.ShouldBe("A1");
1138+
sheet.Cells["A3"].Text.ShouldBe("A2");
1139+
sheet.Cells["A4"].Text.ShouldBe("A3");
1140+
sheet.Cells["B2"].Text.ShouldBe("B1");
1141+
sheet.Cells["B3"].Text.ShouldBe("B2");
1142+
sheet.Cells["B4"].Text.ShouldBe("");
1143+
}
1144+
}
10971145
}
10981146
}

0 commit comments

Comments
 (0)