Free Support Forum - aspose.com

Combine multiple excel to single csv

I have multiple excel sheets (each excel sheet contains data in “sheet1” workbook only). I would like to combine all the ratesheets into a single csv file format.


or save each excel file to csv and then combine all csv files into a single file.

Is it possible to be done by apose.cells component? any example would be helpful.

Thanks


Hi,

Thanks for your posting and using Aspose.Cells.

Yes, you can save your workbook active sheet into csv file. Then you can combine all csv files into a single csv file.

Please see the following code. I have attached the source excel files used in this code and combined csv file generated by it for your reference.

C#
string[] fileNames = { “s1.xlsx”, “s2.xlsx”, “s3.xlsx”, “s4.xlsx”,};

//0-byte array
byte[] workbookData = new byte[0];

//Text save options. You can use any type of separator
//For CSV, we will use Comma as separator
TxtSaveOptions opts = new TxtSaveOptions();
opts.Separator = ‘,’;

//Copy each worksheet data in text format inside workbook data array
for (int idx = 0; idx < fileNames.Length; idx++)
{
Workbook workbook = new Workbook(fileNames[idx]);

//Save the active worksheet into text format
MemoryStream ms = new MemoryStream();
workbook.Worksheets.ActiveSheetIndex = 0;
workbook.Save(ms, opts);

//Save the worksheet data into sheet data array
ms.Position = 0;
byte[] sheetData = ms.ToArray();

//Combine this worksheet data into workbook data array
byte[] combinedArray = new byte[workbookData.Length + sheetData.Length];
Array.Copy(workbookData, 0, combinedArray, 0, workbookData.Length);
Array.Copy(sheetData, 0, combinedArray, workbookData.Length, sheetData.Length);

workbookData = combinedArray;
}

//Save entire workbook data into file
File.WriteAllBytes(“output.csv”, workbookData);