Logo 
Search:

MS Office Answers

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds
  Question Asked By: Jason Perry   on Dec 30 In MS Office Category.

  
Question Answered By: James Currier   on Dec 30

Not the cleanest code in the world but I think you will get the idea...


Imports Excel = Microsoft.Office.Interop.Excel
Imports System.IO
Imports Microsoft.VisualBasic
Imports Microsoft.Office.Interop.Excel
Imports Microsoft.Office.Interop.Excel.XlColumnDataType


Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim xlApp As Excel.Application
Dim xlWorkbook As Excel.Workbook
Dim xlWorksheet As Excel.Worksheet
Dim myStr As String
Dim FileReader As StreamReader
Dim FileWriter As StreamWriter
Dim results As DialogResult
Dim openFileDialog1 As New OpenFileDialog()
Dim mycol(,) As Object = New Object(,) {{1, 2}, {2, 2}, {3, 5}, {4, 1}, {5, 2}, {6, 1}}

openFileDialog1.InitialDirectory = "c:\"
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
openFileDialog1.FilterIndex = 2
openFileDialog1.RestoreDirectory = True
results = openFileDialog1.ShowDialog

If results = DialogResult.OK Then

FileReader = New StreamReader(openFileDialog1.FileName)

myStr = FileReader.ReadToEnd()
'myStr = Replace(myStr, "'", "")

FileWriter = New StreamWriter("c:\Temp\invoices.txt")

FileWriter.Write(myStr)

FileWriter.Close()
FileReader.Close()

xlApp = CreateObject("Excel.Application")

xlApp.Visible = False

xlApp.Workbooks.OpenText("C:\Temp\invoices.txt", , , , XlTextQualifier.xlTextQualifierSingleQuote, True, FieldInfo:=mycol)
xlWorkbook = xlApp.ActiveWorkbook
xlWorksheet = xlWorkbook.Worksheets("invoices")

MsgBox(xlWorksheet.Cells(2, 1).value)
xlWorkbook.SaveAs("c:\Temp\invoices.xlsx", FileFormat:=51)
xlWorkbook.Close()
xlApp.Quit()
releaseObject(xlApp)
releaseObject(xlWorkbook)
releaseObject(xlWorksheet)
End If

End Sub

Share: 

 
 
Didn't find what you were looking for? Find more on How to use Workbooks.opentext method in VB.NET Or get search suggestion and latest updates.


Tagged: