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

Convertng an xml string to an xls formatted string

What I am trying to do with the following code snippet is convert an xml string into a string with an xls format. The output gets downloaded as a .xls file. when the resultant .xls file is opened it says the file format and extension don't match. How do I get the string into an xls format?

                // convert bytes to a string so we can insert worksheet data.
                var spreadSheetTemplate = System.Text.Encoding.UTF8.GetString(rawSpreadsheet);

                // create a worksheet on which to manage fields.
                var spreadSheet = spreadSheetTemplate.Replace("<Worksheets/>", results.ToString());

                // convert string to memory stream
                MemoryStream msSpreadSheet = new MemoryStream(Encoding.UTF8.GetBytes(spreadSheet));
                // open msSpreadSheet into the excel engine and at the same time convert it to .xls format (I think)
                var excelEngine = new ExcelEngine().Excel.Workbooks.OpenFromXml(msSpreadSheet, ExcelXmlOpenType.MSExcel);

                // store the content of the engine which should (I think) now  be in .xls format back into a string
                spreadSheet = excelEngine.ToString();

                //var spreadSheet = new ExcelEngine().Excel.Workbooks.OpenFromXml(new MemoryStream(Encoding.UTF8.GetBytes(spreadSheetTemplate.Replace("<Worksheets/>", results.ToString()))), ExcelXmlOpenType.MSExcel).ToString();

                return Encoding.UTF8.GetBytes(spreadSheet);

5 Replies

MM Mathu Mohan Vijayakumar Syncfusion Team September 8, 2015 12:03 PM UTC

Hi Avril,

We can see that your code is invalid.

XlsIO supports spreadsheetML and does not support for other XML formats. We have shared the UG Documentation link for your reference.

UG Link: http://help.syncfusion.com/wpf/xlsio/opening-excel-workbook#open-an-xml-workbook

V.A.Mathu Mohan

AC Avril Craighead September 11, 2015 08:18 AM UTC

I am sorry but I don't understand how that helps me to get the xml format string into a xls format string.

MM Mathu Mohan Vijayakumar Syncfusion Team September 14, 2015 10:44 AM UTC

Hi Avril,

Currently XlsIO supports only for XML Spreadsheet 2003 format. In your code you are using XML tag “<Worksheets/>” which is not a XML Spreadsheet predefined tag. And you have used ToString() method, which does not convert the xls to XML format string.

If you have any queries, you can contact us through our Direct Trac support system.

Mathu Mohan V A

AC Avril Craighead September 15, 2015 08:16 AM UTC

Thanks - I am already successfully outputting a .xml - thank you for letting me know that Syncfusion cannot output a .xls file.

MM Mathu Mohan Vijayakumar Syncfusion Team September 16, 2015 11:35 AM UTC

Hi Avril,

Thank you for updating us.

If you have any queries, you can contact us through our Direct Trac support system.

Support Link: https://www.syncfusion.com/account/login?ReturnUrl=%2fsupport%2fdirecttrac%2fincidents

Mathu Mohan V A

Live Chat Icon For mobile
Up arrow icon