John N
John N

Reputation: 3

Using how to import data set into excel and return as memorystream? receiving an error inside workbook.Save(stream)

enter image description here

public static MemoryStream GenerateExcelReport(DataSet objDS)
    {

        // Instantiating a Workbook object            
        Workbook workbook = new Workbook();

        // Obtaining the reference of the worksheet
        Worksheet worksheet = workbook.Worksheets[0];

        // Instantiating a "Products" DataTable object
        DataTable dataTable = objDS.Tables[0];


        // Importing the contents of DataTable to the worksheet starting from "A1" cell,
        // Where true specifies that the column names of the DataTable would be added to
        // The worksheet as a header row
      
        worksheet.Cells.ImportDataTable(dataTable, true, "A1");
        MemoryStream stream = new MemoryStream();
        // Saving the Excel file
        workbook.Save(stream);


        return stream;
    }

Upvotes: 0

Views: 671

Answers (1)

user2250152
user2250152

Reputation: 20823

Workbook has two Save methods that accept Stream.

public void Save(Stream stream, SaveFormat saveFormat) where SaveFormat represents the format in which the workbook is saved.

public void Save(Stream stream, SaveOptions saveOptions) where SaveOptions represents all save options.

You need to specify the second argument either SaveFormat or SaveOptions.

Try to call Save with one of possible SaveFormat value like Xlsx.

Example:

// Saving the Excel file
workbook.Save(stream, SaveFormat.Xlsx);

Upvotes: 0

Related Questions