Excel automation using C#
Posted
by tecnodude
on Stack Overflow
See other posts from Stack Overflow
or by tecnodude
Published on 2010-03-12T10:02:08Z
Indexed on
2010/03/12
10:07 UTC
Read the original article
Hit count: 255
Hi,
I have a folder with close to 400 excel files. I need to copy the worksheets in all these excel files to a single excel file.
using Interop and Reflection namespaces heres is what I have accomplished so far.
I use folderBrowserDialog to browse to the folder and select it, this enable me to get the file names of the files within the folder and iterate through them this is as far as i got, any help would be appreciated.
if (result == DialogResult.OK)
{
string path = fbd1.SelectedPath; //get the path
int pathLength = path.Length + 1;
string[] files = Directory.GetFiles(fbd1.SelectedPath);// getting the names of files in that folder
foreach (string i in files)
{
MessageBox.Show("1 " + i);
myExcel.Application excelApp = new myExcel.ApplicationClass();
excelApp.Visible = false;
MessageBox.Show("2 " + i);
myExcel.Workbook excelWorkbook = excelApp.Workbooks.Add(excelApp.Workbooks._Open(i, 0, false, 5, "", "", false, myExcel.XlPlatform.xlWindows, "", true, false, 0, true));
myExcel.Sheets excelSheets = excelWorkbook.Worksheets;
MessageBox.Show("3 " + i);
excelApp.Workbooks.Close();
excelApp.Quit();
}
MessageBox.Show("Done!");
}
How do i append the copied sheets to the destination file. Hope the question is clear?
thanks.
© Stack Overflow or respective owner