From 3cfeab2d4d9877b9b0f0f33bd3d33612465d5a03 Mon Sep 17 00:00:00 2001 From: changj Date: Thu, 26 Dec 2024 17:28:31 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A6=96=E5=88=97=E5=81=8F=E7=A7=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ExcelImporterAttribute.cs | 6 ++++++ .../Utility/ImportHelper.cs | 10 +++++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/Magicodes.ExporterAndImporter.Excel/ExcelImporterAttribute.cs b/src/Magicodes.ExporterAndImporter.Excel/ExcelImporterAttribute.cs index 846a6d22..9d7c6454 100644 --- a/src/Magicodes.ExporterAndImporter.Excel/ExcelImporterAttribute.cs +++ b/src/Magicodes.ExporterAndImporter.Excel/ExcelImporterAttribute.cs @@ -64,5 +64,11 @@ public class ExcelImporterAttribute : ImporterAttribute /// 是否仅导出错误数据 /// public bool IsOnlyErrorRows { get; set; } + + + /// + /// 首列偏移量(默认为0,数据从第2列开始则偏移量为1) + /// + public int FirstColumnOffset { get; set; } = 0; } } \ No newline at end of file diff --git a/src/Magicodes.ExporterAndImporter.Excel/Utility/ImportHelper.cs b/src/Magicodes.ExporterAndImporter.Excel/Utility/ImportHelper.cs index 7a7aebb8..9be08664 100644 --- a/src/Magicodes.ExporterAndImporter.Excel/Utility/ImportHelper.cs +++ b/src/Magicodes.ExporterAndImporter.Excel/Utility/ImportHelper.cs @@ -567,8 +567,8 @@ protected virtual void ParseTemplate(ExcelPackage excelPackage, ImportResultEnum { ExcelImporterSettings.HeaderRowIndex++; } - - for (var columnIndex = 1; columnIndex <= endColumnCount; columnIndex++) + var firstColumnOffset = ExcelImporterSettings.FirstColumnOffset; + for (var columnIndex = firstColumnOffset + 1; columnIndex <= endColumnCount; columnIndex++) { var header = worksheet.Cells[ExcelImporterSettings.HeaderRowIndex, columnIndex].Text; @@ -659,7 +659,7 @@ protected virtual void ParseTemplate(ExcelPackage excelPackage, ImportResultEnum }); throw new Exception($"{Resource.AnUnknownErrorOccurredInTheTemplate}{ex.Message}", ex); } - + } @@ -1149,13 +1149,13 @@ protected virtual void ParseData(ExcelPackage excelPackage) ImportResult.Data = new List(); var propertyInfos = new List(typeof(T).GetProperties()); - + var firstColumnOffset = ExcelImporterSettings.FirstColumnOffset; for (var rowIndex = ExcelImporterSettings.HeaderRowIndex + 1; rowIndex <= worksheet.Dimension.End.Row; rowIndex++) { //跳过空行 - if (worksheet.Cells[rowIndex, 1, rowIndex, worksheet.Dimension.End.Column].All(p => p.Text == string.Empty)) + if (worksheet.Cells[rowIndex, 1 + firstColumnOffset, rowIndex, worksheet.Dimension.End.Column].All(p => p.Text == string.Empty)) { EmptyRows.Add(rowIndex); continue;