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.

Sat Mar 23, 2019 12:24 pm

Hi everyone,

In my application, I need to extract the content of an excel file to a string. What I think of is to save the file to txt then use ReadAllText to read the content od the txt file to my string

Code: Select all
Dim workbook As New Workbook()
            workbook.LoadFromFile(file)
            Dim sheet As Worksheet = workbook.Worksheets(0)

            Dim temp_file As String = My.Application.Info.DirectoryPath & "temp_file.txt"
            sheet.SaveToFile(temp_file, ";")

            Dim str As String
            str = System.IO.File.ReadAllText(temp_file)

            My.Computer.FileSystem.DeleteFile(temp_file)


I want to ask if there is a direct way to do this instead of creating the temp text file. Thank you.

ducphu
 
Posts: 11
Joined: Tue Nov 24, 2015 5:13 am

Mon Mar 25, 2019 6:13 am

Hello,

Thank you for contacting us.
Sorry there is no direct way to extract the content of an excel file to a string, but there is an approach for you like the following sample code which doesn't create the temp text file. If there is any question, welcome to write back.
Code: Select all
Dim workbook As New Workbook()
workbook.LoadFromFile("test.xlsx")
Dim sheet As Worksheet = workbook.Worksheets(0)
Dim sb As New StringBuilder()
Dim dataRange As CellRange = sheet.Range(sheet.FirstRow, sheet.FirstColumn, sheet.LastRow, sheet.LastColumn)
For Each range As CellRange In dataRange
   Dim cellValue As String = range.Value
   sb.AppendLine(cellValue)
Next range
Dim resultString As String = sb.ToString()

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1261
Joined: Wed Apr 25, 2018 3:20 am

Return to Spire.XLS