-
Notifications
You must be signed in to change notification settings - Fork 16
Using FileCleaner namespace to ignore reading additional empty cells in Excel
Hadi edited this page Mar 5, 2019
·
1 revision
Sometimes will reading Excel files into DataTable, you will find hundreds of additional empty columns and rows imported into the DataTable. These additional empty cells may led to OutofMemoryException
while trying to read Excel files.
Using FileCleaner.MsExcelCleaner
class you have the choice to:
- Delete all empty Rows and Columns
- Get the used Rang for each worksheet to be able to pass it to the
Converters.MsExcelImport
class - Remove top empty rows and get used range
using(FileCleaner.MsExcelCleaner xClean = new FileCleaner.MsExcelCleaner(5000,ExcelCleanOperationType.CleanExcel){
xClean.CleanExcel(@"D:\SchemaMapperTest\Password_Test.xlsx");
}
string strUsedRange;
using(FileCleaner.MsExcelCleaner xClean = new FileCleaner.MsExcelCleaner(5000,ExcelCleanOperationType.GetUsedRange){
strUsedRange = xClean.CleanExcel(@"D:\SchemaMapperTest\Password_Test.xlsx");
}
using (Converters.MsExcelImport smExcel = new Converters.MsExcelImport(@"D:\SchemaMapperTest\Password_Test.xlsx",strUsedRange))
{
//Read Excel
smExcel.BuildConnectionString();
var lst = smExcel.GetSheets();
//Read only from the first worksheet and consider the first row as header
dtExcel = smExcel.GetTableByName(lst.First(), true, 0);
}
string strUsedRange;
using(FileCleaner.MsExcelCleaner xClean = new FileCleaner.MsExcelCleaner(5000,ExcelCleanOperationType.RemoveTopEmptyAndGetUsedRange){
strUsedRange = xClean.CleanExcel(@"D:\SchemaMapperTest\Password_Test.xlsx");
}
using (Converters.MsExcelImport smExcel = new Converters.MsExcelImport(@"D:\SchemaMapperTest\Password_Test.xlsx",strUsedRange))
{
//Read Excel
smExcel.BuildConnectionString();
var lst = smExcel.GetSheets();
//Read only from the first worksheet and consider the first row as header
dtExcel = smExcel.GetTableByName(lst.First(), true, 0);
}