【问题标题】:Trying to list files on excel spreadsheet VB.Net试图在excel电子表格VB.Net上列出文件
【发布时间】:2020-03-21 10:32:12
【问题描述】:

我正在尝试使用 Openfile Dialog 命令导航到一个文件夹,然后选择一些文件。使用这些选定的文件,我正在尝试将这些文件的名称写入 Excel 电子表格。我可以导航到文件夹,但无法将文件写入电子表格。

我有以下代码:

    Dim strFileName As String
    Dim openFile1 As OpenFileDialog = New OpenFileDialog()


    Dim Row As Integer
    Row = 14

    Const Column_A As Integer = 1
    Const Column_B As Integer = 2
    Const Column_C As Integer = 3
    Const Column_D As Integer = 4
    Const Column_E As Integer = 5
    Const Column_F As Integer = 6
    Const Column_U As Integer = 21

    openFile1.Title = "Open File Dialog"    'Title header for dialog box
    openFile1.InitialDirectory = "C:\" 'Initial directory for file dialog box
    openFile1.Filter = "All files (*.*)|" 'Filter all files
    openFile1.Multiselect = True 'Allows the user to select to select multiple files


    Dim Folder As IO.FileInfo()
    Dim FileName As IO.FileInfo

    If openFile1.ShowDialog() = DialogResult.OK Then 'This gives the "ok" to actually open the pop-up window
        For Each FileName In Folder.SelectedItems
            strFileName = openFile1.FileName  'Stores the Filename inside the variable
            Big_Sheet.Cells(Row, Column_B) = FileName
            Row = Row + 1
            Big_Sheet.Cells(Row, Column_C).Value = strFileName 'Shows the variable FilePath
        Next
    End If

【问题讨论】:

  • 您是否遇到任何错误或在任何情况下使用 epplus 或 npoi 等工具来操作 excel 文件会更有效 IMO

标签: excel vb.net openfiledialog


【解决方案1】:

可能需要进行更多研究才能写入 excel 文件,例如让您的程序知道您在做什么。

Dim xls As Microsoft.Office.Interop.Excel.Application
Dim xlsWorkBook As Microsoft.Office.Interop.Excel.Workbook
Dim xlsWorkSheet As Microsoft.Office.Interop.Excel.Worksheet

xls = New Microsoft.Office.Interop.Excel.Application
xlsWorkBook = xls.Workbooks.Open("Insert file directory here")
xlsWorkSheet = xlsWorkBook.Sheets("sheet1")

xlsWorkSheet.Cells(1, 1) = TextBox1.Text

希望你能用它来让你更好地理解,研究是关键,我的朋友。

【讨论】:

  • 嗨 S Mac,这不是我要找的,我正在尝试打开一个弹出文件对话框窗口,并使用该文件对话框窗口,我想选择一些文件.使用这些选定的文件,我希望将文件名写入 excel 电子表格。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-04
  • 2016-05-28
  • 1970-01-01
  • 2023-03-28
  • 2011-11-09
相关资源
最近更新 更多