WeihanLi / WeihanLi.Npoi

NPOI Extensions, excel/csv importer/exporter for IEnumerable<T>/DataTable, fluentapi(great flexibility)/attribute configuration
https://weihanli.github.io/WeihanLi.Npoi/index.html
Apache License 2.0
203 stars 53 forks source link

Add maxColumns and removeEmptyRows parameters to ExcelHelper.ToDataTable() #122

Closed Ninjanaut closed 3 years ago

Ninjanaut commented 3 years ago

Is your feature request related to a problem? Please describe.

Many times ExcelHelper.ToDataTable() returns DataTable like

image

or it does not load DataTable at all, because of weird System.IndexOutOfRangeException, when cell count is for example 5, but row count is 4 (I do not know how to create such an excel, but I attach an example with that exception ExcelWithOutOfRangeException.xlsx

image

image

Describe the solution you'd like So I thought an elegant solution would be to extend method signature of ExcelHelper.ToDataTable() with maxColumns and removeEmptyRows like

public static DataTable ToDataTable(byte[] excelBytes, ExcelFormat excelFormat, bool removeEmptyRows = false, int? maxColumns = null)

Describe alternatives you've considered Removing columns and rows can be done later when DataTable is already loaded (except that case throwing System.IndexOutOfRangeException) but it requires unnecessary additional loop through DataTable and is so common operation that it would be great to have it in one method and not have to write it separately.

Additional context if there will be an interest in these features, I'll be happy to send a pull request.

WeihanLi commented 3 years ago

@Ninjanaut, great thanks for your reporting and your valuable thoughts, sounds great to me, and it would be great if you could make a PR to fix it

WeihanLi commented 3 years ago

Should be closed by #123 , #124 A new version 1.20.0 should work now, please report if this issue still exists, thanks.