I have some Excel files which each one has only one sheet, my problem when trying to copy those sheets to a new excel I got these two exceptions:
Exception error.
Error message: Stream does not support reading.
Details:
at System.IO.FileStream.Read(Byte[] array, Int32 offset, Int32 count)
at ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputBuffer.Fill()
at ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputBuffer.ReadLeByte()
at ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputBuffer.ReadLeInt()
at ICSharpCode.SharpZipLib.Zip.ZipInputStream.GetNextEntry()
at NPOI.OpenXml4Net.Util.ZipInputStreamZipEntrySource..ctor(ZipInputStream inp)
at NPOI.OpenXml4Net.OPC.ZipPackage..ctor(Stream filestream, PackageAccess access)
at NPOI.OpenXml4Net.OPC.OPCPackage.Open(Stream in1)
at NPOI.Util.PackageHelper.Open(Stream is1)
at NPOI.XSSF.UserModel.XSSFWorkbook..ctor(Stream is1)
at MetroSet_UI_Example.Form1.metroSetButton1_Click(Object sender, EventArgs e)
And the second exception is this:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
The button to read and generate the new file:
private void metroSetButton1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
appuiListBox.Items.Clear();
appuiGridView.Rows.Clear();
try
{
String appuiName = "";
XSSFSheet sheet = null;
//XSSFWorkbook workbookMerged = new XSSFWorkbook(new FileStream("MergedAppuis.xlsx", FileMode.Append, FileAccess.Write));
XSSFWorkbook workbookMerged = new XSSFWorkbook();
foreach (String appui in openFileDialog1.FileNames)
{
var fileExtension = Path.GetExtension(appui);
if(fileExtension == ".xlsx")
{
using (var fs = new FileStream(appui, FileMode.Open, FileAccess.Read))
{
XSSFWorkbook wb = new XSSFWorkbook(fs);
sheet = (XSSFSheet)wb.GetSheetAt(0);
//((XSSFSheet)wb.GetSheetAt(0)).CopyTo(workbookMerged, sheet.GetRow(25).GetCell(0).StringCellValue, true, true);
sheet.CopyTo(workbookMerged, "5454", true, true);
//MessageBox.Show(((XSSFSheet)wb.GetSheetAt(0)).SheetName);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show($"Exception error.\n\nError message: {ex.Message}\n\n" +
$"Details:\n\n{ex.StackTrace}");
}
}
}