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. (Last updated on : Nov 16th 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

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

Thread ID:

Created:

Updated:

Platform:

Replies:

123584 Apr 1,2016 08:48 AM UTC Apr 6,2016 12:33 PM UTC ASP.NET MVC 3
loading
Tags: XlsIO
Testname
Asked On April 1, 2016 08:48 AM UTC

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*/)


Abirami Varadharajan [Syncfusion]
Replied On 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.

Testname
Replied On April 5, 2016 05:42 AM UTC

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

Abirami Varadharajan [Syncfusion]
Replied On 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.

CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;