Hello.
I am looping through files in a folder and need to remove empty columns of a spreadsheet in a given range of A-G (at least any column between these ranges may be blank). I eventually want to do the same thing with the rows. I have revised my code, but am getting a nullException error. How can I properly check if the column is empty and then remove it? I don't want to open the files as this will become an overnight process. Please advise. Thanks in advance.
class removeEmpty
{
public void removeEmptyCols()
{
Microsoft.Office.Interop.Excel.Application oXL = null;
Microsoft.Office.Interop.Excel.Workbook oWB = null;
Microsoft.Office.Interop.Excel.Worksheet oWS = null;
Excel.Range oRng = null;
int numSheets = oWB.Sheets.Count;
for (int sheetnum = 1; sheetnum < numSheets + 1; sheetnum++)
{
oWS = (Microsoft.Office.Interop.Excel.Worksheet)oWB.Sheets[sheetnum];
oRng = oWS.get_Range("A2", "G64000");
oRng.EntireColumn.Delete(Missing.Value);
}
Console.WriteLine("Successfully removed columns");
}
}
in my overall program, I am looping through the directory in a transaction:
using (TransactionScope tx = new TransactionScope())
{
removeEmpty em = new removeEmpty();
foreach (string myfile in excelFiles)
{
//remove empty columns
em.removeEmptyCols();
}
}