You're mixing UI and back-end code. There is no reason for the code to be in button1_Click
; it should be in a class of its own, expose a method that receives the values of textBox1.Text
and textBox2.Text
as a parameter, and report back the elapsed time.
Why define DateTime dt = DateTime.Now;
when you don't use it?
Don't call File.ReadLines(fileName)
twice. Do it once, store the result in a variable and use that variable.
EmptyRow
should be camelCase.
Also, this kind of initialization isn't typical for C#: int row = 1, EmptyRow = 0;
.
Your entire method is 100 lines long and contains two foreach
blocks. Split this up into smaller methods; worksheet
can be a variable at the class level so you don't need to pass it around.
Seeing that this functionality is repeated several times, consider making it into a method with the necessary parameters:
(worksheet.Cells[EmptyRow, 6] as Microsoft.Office.Interop.Excel.Range).Value2 =
I note that you keep the Excel file open and then parse one or more text files. Have you tried closing and opening the Excel file for each text file you parse?
Also, have you considered other ways to open and edit Excel files besides Microsoft.Office.Interop, like OpenXml?