[英]reading excel file in C# fast
我是C#的初學者,我有一個具有30000行的excel文件,我想閱讀它並在每個單元格上進行一些處理。 我用這段代碼進行讀取,然后在這個excel文件上有一個for循環,它是如此之慢(大約2小時)。 您能幫我解決這個問題嗎? 有什么方法可以非常快速地讀取此文件嗎?
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);
您尚未共享所要詢問的循環 ,但是如果代碼示例重復了30,000次,那么一個大問題是:
您打開同一文件30,000次 。
您將文件關閉了0次。
我很驚訝Windows沒有崩潰。 2小時后它還完成了嗎?
一個很好的起點是通過谷歌搜索您沒有完全理解的每個關鍵字。 就個人而言,我喜歡MSDN,但是那里有成千上萬的有用站點和示例。
例如:
我實際上對C#Interop並沒有太多的經驗,但是答案很容易獲得。 聽起來好像@Zameer的鏈接可能是您需要的-但不僅僅是復制/粘貼代碼,還請確保您也理解這些示例的每一步!
起初它可能會讓人不知所措,有時甚至不知道從哪里開始尋找,但是那里有無數的資源。
另請參閱:
祝好運!
我希望我一點都不貶低; 它有時很難判斷一個人的經驗水平,甚至不管 ,我認為簡單的東西往往是最容易被忽視(包括我自己!)我還在想着的對象處理是問題的至少一部分。
當我在處理多個對象時遇到內存或性能問題時,有時會使用一種故障排除方法來“簡化和總結”。 我將重命名以制作所有代碼的副本,刪除除對象之外的所有內容,然后將其重命名為“對象子類型+數字”,以此類推:
就像我說的那樣,我對C#還是很陌生,但是我知道在其他語言中,忘記關閉和釋放對象可能會導致嚴重的問題,尤其是當反復循環時。
我也不能肯定地說,但是我懷疑您最好重新使用在循環之前創建的單個Excel.Application
對象,而不是使用多個create / open / close / release來刻錄資源。
另一種可能性,而不是遍歷UsedRange
每一行和每一列,也許嘗試使用C等效於For Each
循環。
Dim c as Range
For Each c in xlWorkSheet.UsedRange
c.Value = .....
...etc..
Next
最后想到的是,如果仍然很慢,請在此處和此處添加一些計時器,以找出導致性能問題的確切原因。 (還請在運行Windows Task Manager時檢查它,以密切注意資源。)
您可以為此使用Open XML SDK
。 它不同於互操作方法,而且閱讀工作簿的互操作方法通常很慢。
我使用過openxml sdk來創建和讀取在不同工作表上也包含超過1萬行的excel。 這應該符合您的目的。
這里有一些鏈接可以幫助您入門。
希望能幫助到你
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.