We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

how to convert an EXCEL to CSV from an given byte array

hi

how to convert an given EXCEL to CSV from an given byte array and return the result as byte array?

Method-signature:

public byte[] ConvertExcelToCSV(byte[] inputFile/* this will be the excel file as an byte array*/)


3 Replies

AV Abirami Varadharajan Syncfusion Team April 4, 2016 03:01 PM UTC

Hi,

Thank you for contacting syncfusion support.

Kindly refer the below code snippet to convert the excel byte array to csv byte array.

Code Example:

            ExcelEngine excelEngine = new ExcelEngine();

            IApplication application = excelEngine.Excel;

            string inputPath = GetFullTemplatePath("Sample.xlsx");

            IWorkbook workbook = application.Workbooks.Open(inputPath,    ExcelOpenType.Automatic);

            //Byte Array of Input file.

            byte[] excel = System.IO.File.ReadAllBytes(inputPath);

            //Byte Array of Csv file converted from excel byte array.

            byte[] csv = ConvertExcelToCSV(excel);



            public byte[] ConvertExcelToCSV(byte[] buffer)

            {

               ExcelEngine excelEngine = new ExcelEngine();

               IApplication application = excelEngine.Excel;

               MemoryStream stream = new MemoryStream(buffer);

               IWorkbook workbook = application.Workbooks.Open(stream);

               MemoryStream csvStream = new MemoryStream();

               //Saving the file as csv

               workbook.SaveAs(csvStream, ",");

               csvStream.Seek(0, SeekOrigin.Begin);

               return csvStream.ToArray();

               workbook.Close();

               excelEngine.Dispose();
        }



We have also shared the sample illustrating this behaviour which can be downloaded from the following link. Kindly refer and let us know if it helps.

Sample Link: http://www.syncfusion.com/downloads/support/directtrac/general/ze/ExcelToCsvByteArray2113818427.zip

Regards,
Abirami.


TE Testname April 5, 2016 05:42 AM UTC

Ok, thank you but workbook.Close(); is unreachable and should be the memorystream in a using ?


AV Abirami Varadharajan Syncfusion Team April 6, 2016 12:33 PM UTC

Hi,

Thank you for updating us.

The workbook is closed after the byte array is returned, so unreachable code detected warning occurs. We request you to close the workbook and dispose excel engine before the byte array is returned.

Yes, you can also have Memory stream in using. Kindly refer the below code to achieve this.

Code Example:

public byte[] ConvertExcelToCSV(byte[] buffer)

        {

            ExcelEngine excelEngine = new ExcelEngine();

            IApplication application = excelEngine.Excel;

            using (MemoryStream stream = new MemoryStream(buffer))

            {

                IWorkbook workbook = application.Workbooks.Open(stream);

                MemoryStream csvStream = new MemoryStream();

                workbook.SaveAs(csvStream, ",");

                workbook.Close();

                excelEngine.Dispose();

                csvStream.Seek(0, SeekOrigin.Begin);

                buffer = csvStream.ToArray();

            }

                return buffer;

        }


Please let us know if you have any concerns.

Regards,
Abirami.

Loader.
Up arrow icon