XLS to FileStream

Technical support for Spire.XLS

Moderators: iceblue support, Flash, Manager

XLS to FileStream

Postby donbrookes » Tue Feb 07, 2012 2:24 pm

Hello,
I am evaluating the XLS component and i want to be able to open an excel file and convert it to a text file. I noticed in your sample that i downloaded that i can save a workbook to a stream. So I put together my own sample code that opens my excel file and writes it out to a file. When i run this, you seem to be overwriting my xls file with your copyright information because this is a trial version. My question is: Does the component support this?

Thanks for your help.

Dim workbook As Workbook = New Workbook()
Dim sheet As Worksheet = workbook.Worksheets(0)
Dim sFile As String


'Save an excel workbook to stream
Dim fileStream As FileStream = New FileStream("An_Inventory_Example.xls", FileMode.Open)
workbook.SaveToStream(fileStream)

Dim bytes() As Byte = New Byte((fileStream.Length) - 1) {}
Dim numBytesToRead As Integer = CType(fileStream.Length, Integer)
Dim numBytesRead As Integer = 0
Dim objWriter As New System.IO.StreamWriter("An_Inventory_Example.prn")

objWriter.Write(bytes)

While (numBytesToRead > 0)
' Read may return anything from 0 to numBytesToRead.
Dim n As Integer = fileStream.Read(bytes, numBytesRead, _
numBytesToRead)
' Break when the end of the file is reached.
If (n = 0) Then
Exit While
End If
numBytesRead = (numBytesRead + n)
numBytesToRead = (numBytesToRead - n)

objWriter.Write(bytes)


End While
numBytesToRead = bytes.Length


objWriter.Close()
donbrookes
 
Posts: 1
Joined: Tue Feb 07, 2012 12:45 am

Re: XLS to FileStream

Postby Suvi.Wu » Wed Feb 08, 2012 2:32 am

Hello donbrookes,

Thanks for your inquiry.

The spire.Xls component support to save a worksheet to a text file directly. You can use the following code:
Code: Select all
 //load the workbook
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("An_Inventory_Example.xls",ExcelVersion.Version97to2003);
            Worksheet sheet = workbook.Worksheets[0];

            //save the first worksheet as txt file
            sheet.SaveToFile("sample.txt", ",");


Please have a kind try. If you still have any other questions,please feel free to contact us.

Have a great day.

BR
Suvi
e-iceblue support
User avatar
Suvi.Wu
 
Posts: 156
Joined: Thu Oct 20, 2011 2:53 am


Return to Spire.XLS

Who is online

Users browsing this forum: No registered users and 0 guests

cron