1212// ======================================================================
1313
1414using DynamicExpresso ;
15- using Magicodes . ExporterAndImporter . Core ;
1615using Magicodes . ExporterAndImporter . Core . Extension ;
1716using Magicodes . IE . Core ;
17+ using Magicodes . IE . EPPlus ;
18+ using Magicodes . IE . Excel . Images ;
1819using OfficeOpenXml ;
1920using OfficeOpenXml . Drawing ;
21+ using SixLabors . ImageSharp ;
22+ using SixLabors . ImageSharp . Formats ;
2023using System ;
2124using System . Collections . Generic ;
2225using System . Collections . Specialized ;
2326using System . IO ;
2427using System . Linq ;
2528using 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
3330namespace 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>
0 commit comments