How to convert a .txt file extension to a .xls file extension?

  • Thread starter Thread starter Steve
  • Start date Start date
S

Steve

Using VB.NET, How do I convert a .txt file extension to a .xls file
extension? The text file is already created and saved in a folder on the
server.
Thanks
Steven
 
Hi,

Augustin said:
Steve,

System.IO.File.Move("c:\test.txt", "c:\test.xls")

If the text file is really just a text file, changing its extension will
not convert it to an excel file magically...

If you have a text file, you'll need to parse it and insert the data in
the Excel file. Fortunately, you can use ADO.NET to connect to Excel
files like to a DB (though with some restrictions), so it's not that
complicated to interact with Excel files even in environments where
Excel itself is not available or cannot be automated.

HTH,
Laurent
 
Thanks guys, I haven't tried System.IO.File.Move("c:\test.txt",
"c:\test.xls") yet, but I have found that the following code will magically
produce an Excel file with cells A1:A5 populated: This is not documented
anywhere, I just tried it to see what would happen!


Dim pathAndFileName As String = Server.MapPath("Data/Test.xls")

Dim sr As StreamWriter = File.CreateText(pathAndFileName)

Dim xData, str_1,str_2,str_3,str_4,str_5 As String

str_1="This"

str_2="is"

str_3="a"

str_4="Test"

xData = "str_1 & vbTab & str_2 & vbTab & str_3 & vbTab & str_4

sr.WriteLine(xData)

sr.Close()
 
Back
Top