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

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;
}
Solution 1:[1]
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);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | user2250152 |
