0

如何从 VB.NET 代码在 Excel 中打开制表符分隔的 .txt 文件?

这个问题已经在许多论坛上提出过,但我在任何地方都找不到真正解决这个问题的答案。

Dim fileName As String = "file.txt"
Dim filePath As String = System.Reflection.Assembly.GetExecutingAssembly().Location
Dim fullFilePath As String = filePath.Substring(0, filePath.LastIndexOf("\"c)) & "\" & fileName

Public Sub OpenFileInExcel()

    Process.Start("excel.exe", fullFilePath)

End Sub
4

2 回答 2

1

只需使用 Process.Start() 并将“excel.exe”作为第一个参数,并将文件名作为第二个参数:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim TabDelimitedFileName As String = "C:\Users\Mike\Documents\somefile.txt"
    If System.IO.File.Exists(TabDelimitedFileName) Then
        Process.Start("excel.exe", Chr(34) & TabDelimitedFileName & Chr(34))
    Else
        MessageBox.Show(TabDelimitedFileName, "File Not Found")
    End If
End Sub
于 2013-10-10T22:24:19.313 回答
0

您可以使用 Excel COM 对象打开文件并自动调整列:

   Dim X As New Microsoft.Office.Interop.Excel.Application()
            X.Workbooks.Open(FileNm)
            Dim W As Microsoft.Office.Interop.Excel.Worksheet = X.ActiveSheet
            Dim R As Microsoft.Office.Interop.Excel.Range = W.Range(W.Cells(1, 1), W.Cells(65000, 250))
            R.Columns.AutoFit()
            X.Visible = True
于 2019-07-14T02:04:58.747 回答