@GeP
As a workaround, save your workbook in new memory stream object and then write that memory stream object to your original stream object
C#
MemoryStream o = new MemoryStream();
workbook.Save(o, SaveFormat.Xlsx);
ms.SetLength(0);
ms.Write(o.GetBuffer(), 0, (int)o.Length);
ms.Seek(0, SeekOrigin.Begin);
Here is the full code for your reference.
C#
using Aspose.Cells;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SampleCellsApp
{
class Program
{
static void Main(string[] args)
{
using (Stream ms = new MemoryStream())
{
int worksheetId1 = AddWorksheet("Product", ms);
DataTable dataTable = GetDataTable("Products");
Workbook workbook = new Workbook(ms);
workbook.FileFormat = FileFormatType.Xlsx;
// Obtaining the reference of the worksheet
Worksheet worksheet = workbook.Worksheets[worksheetId1];
worksheet.Name = "Product";
worksheet.Cells.ImportDataTable(dataTable, false, 1, 0, true);
MemoryStream o = new MemoryStream();
workbook.Save(o, SaveFormat.Xlsx);
//workbook.Save(ms, opts);
ms.SetLength(0);
ms.Write(o.GetBuffer(), 0, (int)o.Length);
ms.Seek(0, SeekOrigin.Begin);
//// Obtaining the reference of the worksheet
//int worksheetId = AddWorksheet("Sales", ms);
//Workbook workbook1 = new Workbook(ms);
//Worksheet worksheet1 = workbook1.Worksheets[worksheetId];
//worksheet1.Name = "Sales";
//DataTable dataTable1 = GetDataTable("Sales");
//worksheet1.Cells.ImportDataTable(dataTable1, false, 1, 0, true);
//workbook1.Save(ms, SaveFormat.Xlsx);
//ms.Seek(0, SeekOrigin.Begin);
byte[] bData = new byte[ms.Length];
ms.Read(bData, 0, bData.Length);
File.WriteAllBytes("F:\\Download\\ABC1.xlsx", bData);
}
}
public static int AddWorksheet(string name, Stream _fileStream)
{
if (string.IsNullOrEmpty(name) || string.IsNullOrWhiteSpace(name))
throw new ArgumentNullException("Name");
//Instantiating a Workbook object
Workbook workbook = new Workbook(_fileStream);
//removed the Extra Sheet 1 from WorkBook
workbook.Worksheets.RemoveAt("Sheet1");
SaveFormat saveFormat = SaveFormat.Xlsx; //Default Format
//TODO : Format Mapper should be separate class and Each component we use should have mapping with standard GEP File Format
workbook.FileFormat = FileFormatType.Xlsx;
//saveFormat = SaveFormat.Xlsx;
int wkId = workbook.Worksheets.Add();
workbook.Worksheets[wkId].Name = name;
workbook.Save(_fileStream, saveFormat);
return wkId;
}
public static DataTable GetDataTable(string tableName)
{
// Instantiating a "Products" DataTable object
DataTable dataTable = new DataTable(tableName);
// Adding columns to the DataTable object
dataTable.Columns.Add("Action", typeof(string));
dataTable.Columns.Add("Product ID", typeof(Int32));
dataTable.Columns.Add("Product Name", typeof(string));
dataTable.Columns.Add("Units In Stock", typeof(Int32));
dataTable.Columns.Add("Change In Price", typeof(Int32));
dataTable.Columns.Add("FORMULA", typeof(string));
dataTable.Columns.Add("Change", typeof(string));
// Creating an empty row in the DataTable object
DataRow dr = dataTable.NewRow();
// Adding data to the row
dr[0] = "Modified From";
dr[1] = 1;
dr[2] = "Aniseed Syrup";
dr[3] = 15;
dr[4] = DBNull.Value;
dr[5] = "";
dr[6] = "";
// Adding filled row to the DataTable object
dataTable.Rows.Add(dr);
// Creating another empty row in the DataTable object
dr = dataTable.NewRow();
// Adding data to the row
dr[0] = "Modified To";
dr[1] = 1;
dr[2] = "Boston Crab Meat";
dr[3] = 15;
dr[4] = 100;
dr[5] = "";
dr[6] = "=COUNTIF(F7:F12,\"*'\"&$B$6&\"'*\")";
// Adding filled row to the DataTable object
dataTable.Rows.Add(dr);
// Creating another empty row in the DataTable object
dr = dataTable.NewRow();
// Adding data to the row
dr[0] = "Added";
dr[1] = 2;
dr[2] = "Boston Crab";
dr[3] = 15;
dr[4] = 100;
dr[5] = "";
dr[6] = "=COUNTIF(F7:F12,\"*'\"&$C$6&\"'*\")";
// Adding filled row to the DataTable object
dataTable.Rows.Add(dr);
// Creating another empty row in the DataTable object
dr = dataTable.NewRow();
// Adding data to the row
dr[0] = "Deleted";
dr[1] = 3;
dr[2] = "Boston";
dr[3] = 15;
dr[4] = 100;
dr[5] = "";
dr[6] = "=COUNTIF(F7:F12,\"*'\"&$D$6&\"'*\")";
// Adding filled row to the DataTable object
dataTable.Rows.Add(dr);
// Creating another empty row in the DataTable object
dr = dataTable.NewRow();
// Adding data to the row
dr[0] = "Modified From";
dr[1] = 4;
dr[2] = "Boston";
dr[3] = 15;
dr[4] = 100;
dr[5] = "";
dr[6] = "=COUNTIF(F7:F12,\"*'\"&$E$6&\"'*\")";
// Adding filled row to the DataTable object
dataTable.Rows.Add(dr);
// Creating another empty row in the DataTable object
dr = dataTable.NewRow();
// Adding data to the row
dr[0] = "Modified To";
dr[1] = 4;
dr[2] = "Boston S";
dr[3] = 20;
dr[4] = 50;
dr[5] = "";
dr[6] = "";
// Adding filled row to the DataTable object
dataTable.Rows.Add(dr);
return dataTable;
}
}
}