Friday 15 June 2012

.net - Optimizing c# excel read / write to file -


Then I have this rows around 2200 that I can read and to a txt The need to write is the file, the problem is that it takes a lot of time, I have been told that read / write files usually take the time because it is nature , so I have the excel file Just tried to read a stringbiller and write per line (all Do not try to collect the text and write the entire .txt file)

But, what can I do to speed it up?

Choosing a small range, just like 1 row? As the line breaks up \ N builing a huge string and then typing all that to .txt?

My code here

  was given a sample of Excel = Microsoft.Office .Interop.Excel; [...] xlApp = New Excel.Application (); XlWorkBook = xlApp.Workbooks.Open ("C: /Users/MyUser/Desktop/SomeFolder/my_excel.xlsx", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows , "\ T", false, false, 0, truth, 1, 0); XlWorkSheet = (Excel Worksheet) xlWorkBook.Worksheets.get_Item (1); Excel. Change All Range = xlWorkSheet.UsedRange; {System.IO.StreamWriter File = New System. Try the IoStream Vector ("C: \\ test.txt"); String line = ""; // stringbuilder line; // This thousand sometimes for {(AllRange.Value2 [Row, 1] = "") {// line = 0 for line (round & lt; line; New StringBuilder are up to (); {//Console.WriteLine (All RangeValue 2Dev, Column)) for (Ent Column = 1; Columns & lt; = 6; Columns ++); Row + = all ranges.value 2 [row, column]; If (column! = 6) {line + = "|"; //line.Append ("|"); }} File.WriteLine (line); Line = ""; } Other {message box. Showing ("should not have been sent here."); break; }} File.Close (); } Catch (Exceptional) {MessageBox.Show ("File could not be written:" + ex.ToString ()); }  

Btw I am using Net v4.0.30319 ... I think that (on Environment.Version.ToString () )

or net v4.5.51209 (called "help"> "about Microsoft Visual Studio")

I think the main reason for this is because the code is slow due to the use of the use of Excel interop. It is very slow, try using OpenXml SDK instead - this office has a library to manipulate 2007+ documents (including * .xlsx). ExcelInterop is much faster and does not need to install Excel on the machine. The main disadvantage is that it can not open the XLS file. Here's a sample to read the large document:

Try using a stopwatch or any profiler and try to measure the slowest part of the code.


No comments:

Post a Comment