This is a migrated thread and some comments may be shown as answers.

XlsxFormatProvider.Export value cannot be null parameter workbook

1 Answer 262 Views
SpreadProcessing
This is a migrated thread and some comments may be shown as answers.
Carlos
Top achievements
Rank 1
Carlos asked on 15 Apr 2019, 01:14 PM

Hello,

When attempting to export a manually created workbook I obtain this error message: 

Value cannot be null.
Parameter name: workbook at Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsx.Utilities.XlsxHelper.ConvertColumnPixelWidthToExcelWidth(Workbook workbook, Double columnWidthInPixels)

   at Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsx.Contexts.XlsxWorksheetExportContext..ctor(XlsxWorkbookExportContext workbookContext, Worksheet worksheet, Int32 sheetNo)
   at Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsx.Contexts.XlsxWorkbookExportContext.InitializeWorksheetContexts()
   at Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsx.Contexts.XlsxWorkbookExportContext.Initialize()
   at Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsx.XlsxFormatProvider.ExportOverride(Workbook workbook, Stream output)
   at [Redacted] Controller.cs:line 203

I Generate the workbook using this method:

public Workbook WriteExcelFile(List<Object> list)
        {

            Workbook workbook = new Workbook();
            using (Worksheet worksheet = workbook.Worksheets.Add())
            {
                CellRange range = new CellRange(1, 1, list.Count, 5);
                CellSelection selection = worksheet.Cells[range];

                if (selection.CanInsertOrRemove(range, ShiftType.Right))
                {
                    selection.Insert(InsertShiftType.Right);
                }

                for (int row = 0; row < list.Count; row++)
                {
                    worksheet.Cells[row, 0].SetValue(list[row].string1);
                    worksheet.Cells[row, 1].SetValue(list[row].string2);
                    worksheet.Cells[row, 2].SetValue(list[row].string3);
                    worksheet.Cells[row, 3].SetValue(list[row].string4);
                    worksheet.Cells[row, 4].SetValue(list[row].string5);
               }
            }

            return workbook;
        }

The attempt to write it to a memory stream (FileStream was also attempted)

using (Stream output = new MemoryStream(10000))
      formatProvider.Export(workbook, output);

 

Could you help me understand the reason behind this error?

1 Answer, 1 is accepted

Sort by
0
Nikolay Demirev
Telerik team
answered on 18 Apr 2019, 06:20 AM
Hi Carlos,

Your code is disposing the worksheet before exporting it. Internally it is setting the workbook of that worksheet to null, and when you try to export it it throws an exception.

Regards,
Nikolay Demirev
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
SpreadProcessing
Asked by
Carlos
Top achievements
Rank 1
Answers by
Nikolay Demirev
Telerik team
Share this question
or