所以我试图做一个Excel工作表聚合器。在我的工作中,我们得到了一些向我们发送了一堆单独的excel文件的人,这些文件全部仅与1张相关。如何合并多个Excel文件到一个Excel文件
我在某种程度上跟随this previous post's ideas。但是在这样做的时候,我复制的一些excel表单变得空白。只有某些人。我不知道为什么有些是空白的,其他的都没问题。
下面是代码,我用它来打开并复制Excel文件
OpenFileDialog browse = new OpenFileDialog();
browse.Multiselect = true;
DialogResult result = browse.ShowDialog();
if (result == DialogResult.OK)
try //try to open it. If its a proper excel file
{
excel = new Excel.Application();
excel.Workbooks.Add("");
finalized = excel.Workbooks[1];
excel.SheetsInNewWorkbook = 1;
for(int i=0; i< browse.FileNames.Length; i++)
{
excel.Workbooks.Add(browse.FileNames[i]);
}
//skip the first workbook as it is the finalized one
//also note everything in excel starts at 1 and not 0
for(int i=2; i<excel.Workbooks.Count; i++)
{
int count = excel.Workbooks[i].Worksheets.Count;
excel.Workbooks[i].Activate();
for (int j = 1; j < count; j++)
{
Excel._Worksheet pastee = (Excel._Worksheet)excel.Workbooks[i].Worksheets[j];
Excel._Worksheet sheet = (Excel._Worksheet)finalized.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
//Excel._Worksheet sheet = finalized.Sheets[1];
pastee.Copy(Before: sheet);
}//end of for j
}//end of for i
}//end of try
这里是我用来保存代码Excel文件
SaveFileDialog browse = new SaveFileDialog();
browse.Title = "Save as Excel";
browse.Filter = "Excel workbook | *.xlsx";
DialogResult result = browse.ShowDialog();
finalized.SaveAs(browse.FileName, Excel.XlFileFormat.xlWorkbookDefault);
MessageBox.Show("Success", "Message");
//unlock the file
Global.releaseComObjects(finalized, excel);
实际上你想要做什么?你想复制非空白的Excel文件,或复制时获得的Excel文件? @Alexander Ryan Baggett – 2014-12-04 04:33:50
正如我在说明中试图解释的那样。我想打开多个Excel文件。将这些文件中的每张纸复制到一个新的excel文件中。 – 2014-12-04 05:50:07
我打开的文件上的Excel表格不是空白的。 – 2014-12-04 05:50:25