I'm trying to create an application that will merge the excel spreadsheets in a given directory into a single, tabbed Excel workbook. I only have access to VS Community 2015, so VSTO is out of the question; I'm using Interop and NPOI 2.1.3.1 instead.
Here's my code:
files = Directory.GetFiles(sourcePath);
XSSFWorkbook test = new XSSFWorkbook();
try
{
string fPath = files[0];
FileStream fs2 = new FileStream(fPath, FileMode.Open, FileAccess.ReadWrite);
XSSFWorkbook myBook = new XSSFWorkbook(fs2);
test.CreateSheet(myBook.GetSheetName(0));
var sheet1 = myBook.GetSheetAt(0).CopySheet(test.GetSheetName(0));
test.Write(new FileStream("testFile.xlsx", FileMode.Create, FileAccess.ReadWrite));
}
catch { }
I'm not trying to do anything fancy, here, just get the first file returned by the GetFiles function, copy everything on the first worksheet, and create a new worksheet in new workbook "test" with what was copied. I know the source file is being found, because sheet1 of the output file "testFile.xlsx" is getting the unique sheet name of the source worksheet -- it isn't "Sheet1". However, the rest of the sheet comes up blank, and I don't know why.
I'm using this against Excel 2016, if that makes a difference.