Spire.Spreadsheet is a powerful component to view spreadsheet. As a standalone spreadsheet class library, Spire.Spreadsheet is a companion component to Spire.XLS, which mainly focus on how to display spreadsheet.

Fri Nov 23, 2018 8:36 am

Hello,

I have two Worksheets in a Workbook.
When I remove Worksheets[0], save Workbook to file (Excel), and a exception will happen as "object reference not set to an instance of an object".

My code as below:

using (Workbook book = new Workbook())
{
book.LoadFromFile(@"D:\ClosedXMLDonutChart.xlsx");
Worksheet sheet = book.Worksheets[0];
Worksheet sheetChart = book.Worksheets.Add("Chart");
..................Add Chart in sheetChart .......................
book.Worksheets[0].Remove();
book.SaveToFile(@"D:\DonutChart.xlsx", ExcelVersion.Version2010);
}


Image in attachment.
Because I am from Taiwan, error showed in Chinese .
It translated to English is "object reference not set to an instance of an object".

Please help me!
Thanks!

rickyjessy
 
Posts: 16
Joined: Fri Nov 23, 2018 8:20 am

Fri Nov 23, 2018 8:56 am

Hi,

Thank you for your post.
To help us look into the issue, could you please share your sample Excel file with us? For the privacy, you could send it to me via email(Jane.Bai@e-iceblue.com). Besides, could you please tell us which product you are using? Spire.Spreadsheet or Spire.XLS?
Thank you in advance!

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Fri Nov 23, 2018 10:21 am

Dear Chen Ricky,

Thank you for providing the details via email.
I have reproduced the issue and referred it to our dev team.
Once it is fixed, I will let you know. Sorry for the inconvenience caused!

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Tue Dec 11, 2018 7:39 am

Dear Chen Ricky,

Thank you for your patient waiting.
Now the issue has been resolved. Please download the hotfix Spire.XLS Pack(Hotfix) Version:8.12.3.
Besides, kindly note that you need to copy the sheet before inserting the chart, so that the chart has data source after deleting the worksheet. Below is the code for your reference.
Code: Select all
using (Workbook book = new Workbook())
{
    book.LoadFromFile(@"D:\ClosedXMLDonutChart.xlsx");
    Worksheet sheet = book.Worksheets[0];
    Worksheet sheetChart = book.Worksheets.Add("Chart");

    int firstCellRow = 1;
    int firstCellColumn = 1;
    int lastCellRow = 2;
    int lastCellColumn = 2;

    sheet.Copy(sheet.Range[firstCellRow, firstCellColumn, lastCellRow, lastCellColumn],
   sheetChart.Range[firstCellRow, firstCellColumn, lastCellRow, lastCellColumn], true);
    //Add chart and set chart data range 
    Chart chart = sheetChart.Charts.Add(ExcelChartType.Doughnut);
    chart.DataRange = sheet.Range[firstCellRow, firstCellColumn, lastCellRow, lastCellColumn];
    chart.SeriesDataFromRange = false;
    //Chart position 
    chart.LeftColumn = 2;
    chart.TopRow = lastCellRow + 2;
    chart.RightColumn = 10;
    chart.BottomRow = lastCellRow + 22;
    //Chart title 
    chart.ChartTitle = "Chart";
    //Chart legend 
    chart.Legend.Position = LegendPositionType.Bottom;
    //Chart DataLabel
    foreach (ChartSerie cs in chart.Series)
    {
        cs.DataPoints.DefaultDataPoint.DataLabels.HasValue = true;
        cs.DataPoints.DefaultDataPoint.DataLabels.HasPercentage = true;
    }

    book.SaveToFile("chart.xlsx", ExcelVersion.Version2010);
    sheetChart.SaveToPdf(@"ClosedXMLResultDonutChart.pdf");

    book.Worksheets[0].Remove();
    book.SaveToFile(@"ClosedXMLResultDonutChart.xlsx", ExcelVersion.Version2010);
}


Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Thu Dec 13, 2018 6:30 am

Hi,

Greetings from e-iceblue!
Has your issue been resolved?
Your feedback would be greatly appreciated!

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Mon Dec 17, 2018 7:19 am

HI Jane,

Sorry for the late reply.

For some reason, I deal with other things first.

If I try your solution, I will give feedback asap.

Thanks you!!

rickyjessy
 
Posts: 16
Joined: Fri Nov 23, 2018 8:20 am

Mon Dec 17, 2018 7:48 am

Hi,

Thank you for your response.
Will look forward to your feedback!

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Thu Jan 03, 2019 2:32 am

HI Jane,

Thanks you for your response.

It's really help me!

If I have any other question, I will contact you.

Thanks!

rickyjessy
 
Posts: 16
Joined: Fri Nov 23, 2018 8:20 am

Thu Jan 03, 2019 3:47 am

Dear Ricky,

Thank you for your feedback!
Just feel free to contact us if you need any assistance in the future. :)

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Return to Spire.Spreadsheet