excel - 试图在excel电子表格VB.Net上列出文件

标签 excel vb.net openfiledialog

我正在尝试使用 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

最佳答案

可能需要更多的研究来写入一个 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

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

关于excel - 试图在excel电子表格VB.Net上列出文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59043593/

相关文章:

vba - 在 VBA 中获取 "error: type mismatch"

vba - Do Loop 不循环

vba - 清除 K 列中包含 "remove"的每一行的 B 至 G 列内容

vb.net - 函数不返回时,VB.NET中没有警告

c# - 移动按钮和图片框 vb/c#

c# - C#OpenFileDialog打开包含单个文件的zip文件夹?

excel - DAX 中半加和的平均值

c# - .NET OpenFileDialog 是否可以设置为允许用户选择 .lnk 文件

c# - 打开多个文件(OpenFileDialog,C#)

vb.net - 将 Double 转换为 Integer 的最佳方法是什么