Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Null Reference Exception in XSSFSheet.CopySheetImages if there are charts in the original sheet. #287

Closed
tylkotroszke opened this issue Dec 19, 2019 · 4 comments

Comments

@tylkotroszke
Copy link

I have problem when trying to copy sheets from one excel file to another. The goal is to merge many excel files in to one. I got NullReferenceException.

Error message:
An unhandled exception was thrown by the application. System.NullReferenceExcepti on: Object reference not set to an instance of an object. at NPOI.XSSF.UserModel.XSSFSheet.CopySheetImages(XSSFWorkbook destWorkbook, XSSFSheet destSheet) at NPOI.XSSF.UserModel.XSSFSheet.CopyTo(XSSFWorkbook dest, String name, Boolean copyStyle, Boolean keepFormulas)

For some reason cellAnchor.picture is null so this is not even going deeper in to blipFill.blip.embed
string embed = cellAnchor.picture.blipFill.blip.embed; line 2939

This is the input file from which I want to copy sheets to new workbook. I got more of them but they look almost the same first sheet with table and second sheet with different types of charts.
input.xlsx

My code that isn't working:
`
public string MergeReports(List reportsPaths)
{
string path = "D:\02_Testy";
finalFile = Path.Combine(path,test.xlsx);
XSSFWorkbook workbookMerged = new XSSFWorkbook();

            foreach (string reportPath in reportsPaths)
            {
                XSSFWorkbook xssfWorkbook;
                using(FileStream fs = new FileStream(reportPath,FileMode.Open,FileAccess.Read))
                {
                    xssfWorkbook = new XSSFWorkbook(fs);
                    for(int i = 0; i<xssfWorkbook.NumberOfSheets;i++)
                    {
                        XSSFSheet xssfSheet = xssfWorkbook.GetSheetAt(i) as XSSFSheet;
                        xssfSheet.CopyTo(workbookMerged,xssfSheet.SheetName,true,true);
                    }
                }
            }
            using(FileStream fs = new FileStream(finalFile,FileMode.Append,FileAccess.Write))
            {
                workbookMerged.Write(fs,false);
            }
        
        return finalFile;
    }

`
Every other parts of that 2 sheets are copied just fine. Only issue is that application crash when there is chart in second sheet.

@tylkotroszke tylkotroszke changed the title Null Reference Exception when in XSSFSheet.CopySheetImages(XSSFWorkbook destWorkbook, XSSFSheet destSheet) [COPY CHART]Null Reference Exception in XSSFSheet.CopySheetImages(XSSFWorkbook destWorkbook, XSSFSheet destSheet) Dec 19, 2019
@tonyqus tonyqus added the bug label Mar 3, 2020
@YangShengJia
Copy link

I Have the same issue, the different has two-part.

  1. I only need one sheet to another xlsx file.
  2. I using Foreach to replace for-loop
    when I using for-loop, I found some issue which is getting wrong sheet name(PY<-rightname)
    it shows PYY<-wrongname> lend to an exception.
    Foreach getting right name but I write workbook to filestream shows another exception
    is 「只可以在唯寫模式中要求附加存取」.
    This is My Code, Hoping will help to fix.
using (FileStream fsOut = new FileStream("D:\\ExcelFile\\testtest.xls", FileMode.Append, FileAccess.ReadWrite))
            {
                HSSFWorkbook product = new HSSFWorkbook();

                foreach (HSSFSheet xSSFSheet in workbooks)
                {
                    if (xSSFSheet.SheetName == "WenttoCopysheet")
                    {
                        xSSFSheet.CopyTo(product, xSSFSheet.SheetName, true, false);
                    };
                }

                product.Write(fsOut);
                fsOut.Close();
            }

@tonyqus tonyqus added this to the NPOI 2.5.3 milestone Jul 31, 2020
@tonyqus tonyqus added the chart label May 25, 2021
@tonyqus
Copy link
Member

tonyqus commented Sep 14, 2021

@tylkotroszke I can reproduce your issue.

Although I resolve the issue by checking null, the chart still cannot be copied. (the charts elements are ignored by the logic). Here is the output file generated by latest NPOI.
test.xlsx

@tonyqus tonyqus reopened this Sep 14, 2021
@tonyqus tonyqus modified the milestones: NPOI 2.5.5, NPOI 2.5.6 Sep 14, 2021
@tylkotroszke
Copy link
Author

So generally speaking NPOI cannot copy charts right? @tonyqus

@tonyqus
Copy link
Member

tonyqus commented Sep 14, 2021

No, NPOI cannot copy charts so far.

@tonyqus tonyqus changed the title [COPY CHART]Null Reference Exception in XSSFSheet.CopySheetImages(XSSFWorkbook destWorkbook, XSSFSheet destSheet) Null Reference Exception in XSSFSheet.CopySheetImages if there are charts in the original sheet. Oct 1, 2021
@tonyqus tonyqus modified the milestones: NPOI 2.5.6, NPOI 2.5.5 Oct 18, 2021
@tonyqus tonyqus closed this as completed Oct 18, 2021
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

3 participants