Spire.XLS is a professional Excel API that enables developers to create, manage, manipulate, convert and print Excel worksheets. Get free and professional technical support for Spire.XLS for .NET, Java, Android, C++, Python.

Tue Dec 19, 2017 8:43 pm

Hi,
i am working on a common library which needs to return a MemoryStream in Spire.Xls.FileFormat to the caller. Then the caller uses the memoryStream to create an excel file. Is there a way to do it without invoke any Spire function on the caller side?

Thanks,
Lucy

lsong
 
Posts: 36
Joined: Wed Feb 01, 2017 4:13 pm

Wed Dec 20, 2017 2:35 am

Hello Lucy,

Thanks for your inquiry. Here is the code snippet for your reference. Hope it is helpful to you.
Code: Select all
            Workbook workbook = new Workbook();
            workbook.LoadFromFile(@"sample.xlsx");
            MemoryStream stream = new MemoryStream();
            workbook.SaveToStream(stream,FileFormat.Version2010);

            // save without Spire   
            stream.Seek(0, SeekOrigin.Begin);
            StreamWriter sw = new StreamWriter(@"f:\test\result.xlsx");
            stream.CopyTo(sw.BaseStream);
            sw.Flush();
            sw.Close();


Best regards,
Simon
E-iceblue support team
User avatar

Simon.yang
 
Posts: 620
Joined: Wed Jan 11, 2017 2:03 am

Wed Dec 20, 2017 7:04 pm

Thanks for the reply. It works. Could you please also help me on:
1. how to save byte[] in Spire.XLS file format to an excel file?
2. how to save memortstream in Spire.Pdf.FileFormat to a PDF file? I tried to use this code snippet but it didn't work:
_ms.Seek(0, SeekOrigin.Begin);
StreamWriter sw = new StreamWriter(@"D:\result.pdf");
_ms.CopyTo(sw.BaseStream);
sw.Flush();
sw.Close();

Thanks,
Lucy

lsong
 
Posts: 36
Joined: Wed Feb 01, 2017 4:13 pm

Thu Dec 21, 2017 2:51 am

Hello Lucy,

Thanks for your feedback. Please refer to the below code snippet.
Code: Select all
            Workbook workbook = new Workbook();
            workbook.LoadFromFile(@"sample.xlsx");
            MemoryStream streamXls = new MemoryStream();
            MemoryStream streamPdf = new MemoryStream();
            workbook.SaveToStream(streamXls, FileFormat.Version2013);
            workbook.SaveToStream(streamPdf, FileFormat.PDF);

            //stream to byte[]
            streamXls.Seek(0, SeekOrigin.Begin);
            byte[] bytes1 = new byte[streamXls.Length];
            streamXls.Read(bytes1, 0, bytes1.Length);

            streamPdf.Seek(0, SeekOrigin.Begin);
            byte[] bytes2 = new byte[streamPdf.Length];
            streamPdf.Read(bytes2, 0, bytes2.Length);

            //byte to file
            FileStream fs = new FileStream(@"f:\test\result.xlsx",FileMode.Create,FileAccess.Write);
            fs.Write(bytes1,0,bytes1.Length);
            fs.Flush();

            fs = new FileStream(@"f:\test\result.pdf", FileMode.Create, FileAccess.Write);
            fs.Write(bytes2, 0, bytes2.Length);
            fs.Flush();

            fs.Close();


Best regards,
Simon
E-iceblue support team
User avatar

Simon.yang
 
Posts: 620
Joined: Wed Jan 11, 2017 2:03 am

Fri Dec 22, 2017 3:29 pm

It worked. Thank you so much!

lsong
 
Posts: 36
Joined: Wed Feb 01, 2017 4:13 pm

Mon Dec 25, 2017 1:31 am

Hi Lucy,

Thanks for your feedback. If there is any other question, welcome to contact us.

Best regards,
Simon
E-iceblue support team
User avatar

Simon.yang
 
Posts: 620
Joined: Wed Jan 11, 2017 2:03 am

Return to Spire.XLS

cron