Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Import Text File into Excel using VB.NET

Status
Not open for further replies.

StephDN

MIS
Nov 13, 2001
8
US
Hi!

Problem: I need to import a text file into Excel using (preferrably) ASP.NET or VB.NET. The clincher is I need the datat to be imported based on an array of spacing.

I started the code out in Excel/VBA using the following method:

xls.Workbooks.OpenText(Filename:="P_022103.txt", Origin:=xlWindows, StartRow:=1, DataType:=xlFixedWidth, FieldInfo:=Array(Array(0, 2), Array(5, 2), Array(12, 2), Array(19, 2)))

This worked GREAT in VBA - but now I need to be able to use this in a Web App or WinApp instead of VBA.

I have added an Excel 9.0 library reference to my project and am using the following code:

Dim xls As New Excel.Application()
xls.Workbooks.Add()
xls.Workbooks.OpenText(Filename:="P_022103.txt", Origin:=Excel.XlPlatform.xlWindows, StartRow:=1, DataType:=Excel.XlTextParsingType.xlFixedWidth, FieldInfo:=Array(myArray(0, 2), myArray(5, 2), myArray(12, 2), myArray(19, 2)))

If I don't use the "Array" and just call the OpenText method ending with the DataType element - the text file opens fine in Excel, but I need it to open following the column definitions set up by the array.

Sooo...if anyone knows how to use Array to import or if there is another way to do this - I would greatly appreciate your help. Thanks in advance!
 
I have encountered the same issue.

If anyone has advice on how to use the Array functionality when opening a text file within Excel, using VB.NET - it would be greatly appreciated.

Thank you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top