I am a beginner in c# and I have a excel file with 30000 row and I want to read it and do some processes on each cell. I used this code for reading and after that I have a for loop on this excel file that is so slow (about 2 hours). can you help me about this problem? is there any way to read this file very fast?
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Workbook xlWorkBookahan1;
Excel.Workbook xlWorkBookahan2;
Excel.Workbook xlWorkBookahangh;
Excel.Worksheet xlWorkSheet;
Excel.Worksheet xlWorkSheetahan1;
Excel.Worksheet xlWorkSheetahan2;
Excel.Worksheet xlWorkSheetahangh;
Excel.Range range;
string str;
int rCnt;
int cCnt;
int rw = 0;
int cl = 0;
//
Excel.Application xlahan1 = new Microsoft.Office.Interop.Excel.Application();
Excel.Application xlahan2 = new Microsoft.Office.Interop.Excel.Application();
Excel.Application xlahangh = new Microsoft.Office.Interop.Excel.Application();
object misValue = System.Reflection.Missing.Value;
if (xlahan1 == null)
{
MessageBox.Show("Excel is not properly installed!!");
return;
}
xlWorkBookahan1 = xlahan1.Workbooks.Add(misValue);
xlWorkBookahan2 = xlahan2.Workbooks.Add(misValue);
xlWorkBookahangh = xlahangh.Workbooks.Add(misValue);
xlWorkSheetahan1 = (Excel.Worksheet)xlWorkBookahan1.Worksheets.get_Item(1);
xlWorkSheetahan2 = (Excel.Worksheet)xlWorkBookahan2.Worksheets.get_Item(1);
xlWorkSheetahangh = (Excel.Worksheet)xlWorkBookahangh.Worksheets.get_Item(1);
//
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Open(@"C:\Users\maedeh\Desktop\Base.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
range = xlWorkSheet.UsedRange;
rw = range.Rows.Count;
cl = range.Columns.Count;
int ahanghtei = 0, ahan1 = 0, ahan2 = 0, fooladghatei = 0, foolad1 = 0, foolad2 = 0, ngarmghatei = 0, ngarm1 = 0, ngarm2 = 0, nsardghatei = 0, nsard1 = 0, nsard2 = 0, energyghatei = 0, energy1 = 0, energy2 = 0, hamlghatei = 0, haml1 = 0, haml2 = 0, tmarkazighatei = 0, tmarkazi1 = 0, tmarkazi2 = 0, tgmarkazighatei = 0, tgmarkazi1 = 0, tgmarkazi2 = 0, dfnetghatei = 0, dfnet1 = 0, dfnet2 = 0, mtnasoozghatei = 0, mtnasooz1 = 0, mtnasooz2 = 0, fooladsabaghatei = 0, fooladsaba1 = 0, fooladsaba2 = 0;
for (rCnt = 2; rCnt <= rw; rCnt++)
{
if (range.Cells[rCnt, 25].value == 1)//
{
if (((range.Cells[rCnt, 16]).value <= 1999 && (range.Cells[rCnt, 16]).value >= 1000))
{
if ((range.Cells[rCnt, 120]).value >= 1000 && (range.Cells[rCnt, 120]).value <= 1999)
{
ahanghtei++;
xlWorkSheetahangh.Cells[ahanghtei, 1] = range.Cells[rCnt, 1];
xlWorkSheetahangh.Cells[ahanghtei, 2] = range.Cells[rCnt, 2];
xlWorkSheetahangh.Cells[ahanghtei, 3] = range.Cells[rCnt, 3];
}
else
{
xlWorkSheet.Rows[rCnt].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
ahan1++;
xlWorkSheetahan1.Cells[ahan1, 1] = range.Cells[rCnt, 1];
xlWorkSheetahan1.Cells[ahan1, 2] = range.Cells[rCnt, 2];
xlWorkSheetahan1.Cells[ahan1, 3] = range.Cells[rCnt, 3];
}
}
else
{
if ((range.Cells[rCnt, 120]).value >= 1000 && (range.Cells[rCnt, 120]).value <= 1999)
{
xlWorkSheet.Rows[rCnt].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow);
//xlWorkSheet.Cells[rCnt, 1].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow);
ahan2++;
xlWorkSheetahan2.Cells[ahan2, 1] = range.Cells[rCnt, 1];
xlWorkSheetahan2.Cells[ahan2, 2] = range.Cells[rCnt, 2];
xlWorkSheetahan2.Cells[ahan2, 3] = range.Cells[rCnt, 3];
}
}
xlWorkBookahan1.SaveAs("C:\\ahan1.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBookahan2.SaveAs("C:/Users/maedeh/Desktop/ahan2.xls");
xlWorkBookahangh.SaveAs("C:/Users/maedeh/Desktop/ahangh.xls");
xlWorkBookahan1.Close(true, misValue, misValue);
xlWorkBookahan2.Close(true, misValue, misValue);
xlWorkBookahangh.Close(true, misValue, misValue);
xlahan1.Quit();
xlahan2.Quit();
xlahangh.Quit();
Marshal.ReleaseComObject(xlWorkSheetahan1);
Marshal.ReleaseComObject(xlWorkSheetahan2);
Marshal.ReleaseComObject(xlWorkSheetahangh);
Marshal.ReleaseComObject(xlWorkBookahan1);
Marshal.ReleaseComObject(xlWorkBookahan2);
Marshal.ReleaseComObject(xlWorkBookahangh);
Marshal.ReleaseComObject(xlahan1);
Marshal.ReleaseComObject(xlahan2);
Marshal.ReleaseComObject(xlahangh);
Marshal.ReleaseComObject(range);
Marshal.ReleaseComObject(xlWorkSheet);
//close and release
xlWorkBook.Close();
Marshal.ReleaseComObject(xlWorkBook);
//quit and release
xlApp.Quit();
Marshal.ReleaseComObject(xlApp);