我正在尝试使用vb.net向Excel工作簿中添加一些文本文件。我想在我的资源文件夹中添加6个文本文件作为要创建的6张工作表的模板。我能够为每个工作簿创建单独的工作簿文本文件,但是在将代码添加到单个工作簿中时遇到麻烦。我正在复制我现在拥有的代码。我该怎么做?
Dim xlApp As Excel.Application = New Excel.Application With {.Visible =True}
Dim XlDesTemplateBook As Excel.Workbook
Dim xlDesTemplateSheet As Excel.Worksheet
Dim xlDesTemplateSheet1 As Excel.Worksheet
XlDesTemplateBook = xlApp.Workbooks.Add()
xlDesTemplateSheet1 = XlDesTemplateBook.Worksheets.Add("File Path1")
xlDesTemplateSheet1 = XlDesTemplateBook.Worksheets.Add("File Path2")
尝试运行代码时出现此错误。
System.Runtime.InteropServices.COMException:'来自HRESULT的异常:0x800A03EC'
答案 0 :(得分:0)
这对我来说很好。
Imports System.IO
Imports System.Text
Imports Microsoft.Office.Interop
Public Class Form1
Dim objexcel As New Excel.Application
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Paths() As String = Directory.GetFiles("C:\your_path_here\", "*.txt")
For Each Path As String In Paths
File.AppendAllText("C:\your_path_here\TextFileToWriteTo.txt", File.ReadAllText(Path), Encoding.Default) 'The text file will be created if it does not already exist
Next
Dim objexcel As New Excel.Application
objexcel = CreateObject("Excel.Application")
objexcel.Visible = True
objexcel.Workbooks.Open("C:\your_path_here\TextFileToWriteTo.txt",,,,,,,,,,,,,,)
End Sub
End Class