0

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}");
        }
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
H Aßdøµ
  • 2,925
  • 4
  • 26
  • 37
  • remove `XSSFSheet sheet = null;` then change `XSSFSheet sheet = (XSSFSheet)wb.GetSheetAt(0);` the problem may be: `GetSheetAt(0);` or `"5454"` . just remove try cast then debug and see where the line get error.! – Dang D. Khanh Mar 16 '20 at 07:06
  • @TheGridLock CAn you tell to what I have to change to? Also, `5454` is the sheet copied name, so it is irrelevant here. Debugging always redirect me to this `NPOI.OOXML.pdb contains the debug information required to find the source for the module NPOI.OOXML.dll` – H Aßdøµ Mar 16 '20 at 08:29
  • You must distinguish `Worksheet.copy` and `Range.copyto`. not available `worksheet.copyto` – Dang D. Khanh Mar 16 '20 at 11:11
  • maybe you are missing both .dll. find and add this .dll to reference – Dang D. Khanh Mar 16 '20 at 11:13
  • It is available starting from version 2.4, see this [example](https://github.com/tonyqus/npoi/blob/master/examples/xssf/CopySheet/Program.cs) from the official repo. – H Aßdøµ Mar 16 '20 at 18:43

0 Answers0