I'm writing a console app that will extract data from many xls files and lump them into one large csv. I have all the Excel application loading/GC done between each file and I worry it's causing the application to take longer than needed.
Here is my code:
namespace SubconDataExtractToCSV
{
class Program
{
static void Main(string[] args)
{
StreamWriter outputCSV = new StreamWriter(@"C:\Users\mbelmer\Desktop\Helpful Files\Subcon Files\SubconEmailExtract\ExtractData\ExtractedData.csv", true);
DirectoryInfo folder = new DirectoryInfo(@"C:\Users\mbelmer\Desktop\Helpful Files\Subcon Files\SubconEmailExtract\");
FileInfo[] files = folder.GetFiles("*.xls");
foreach (var file in files)
{
ExtractData(file, outputCSV);
}
outputCSV.Close();
}
static void ExtractData(FileInfo filename, StreamWriter output)
{
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(filename.FullName);
Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
Excel.Range xlRange = xlWorksheet.UsedRange;
int rowCount = xlRange.Rows.Count;
int colCount = xlRange.Columns.Count;
string fileDate = filename.Name.Substring(0, 7);
if (filename.Name.Contains("WIP"))
{
//EXTRACT MMT
for(int i = 2; i <= rowCount; i++)
{
string dataLine = fileDate + ",";
for(int j = 1; j <= 10; j++)
{
if (xlRange.Cells[i, j].Value2 != null)
{
dataLine += xlRange.Cells[i, j].Value2 + ",";
}
else
{
dataLine += ",";
}
}
output.WriteLine(dataLine);
}
}
else
{
//EXTRACT AMKOR
for(int i = 2; i <= rowCount; i++)
{
string dataLine = fileDate + ",,,,,,,,,,,";
for(int j = 1; j <= colCount; j++)
{
if(xlRange.Cells[i,j].Value2 != null)
{
dataLine += xlRange.Cells[i, j].Value2 + ",";
}
else
{
dataLine += ",";
}
}
output.WriteLine(dataLine);
}
}
GC.Collect();
GC.WaitForPendingFinalizers();
Marshal.ReleaseComObject(xlRange);
Marshal.ReleaseComObject(xlWorksheet);
xlWorkbook.Close();
Marshal.ReleaseComObject(xlWorkbook);
xlApp.Quit();
Marshal.ReleaseComObject(xlApp);
}
}
}
Should I put the application open and close outside the initial foreach? It's currently going through 38 files and takes just over 6 minutes to do so. Thank you!