[英]How to transfer text from one excel file to another using vb.net
I have done this process many times in macros in excel but this is the first time trying to do it using vb.net and can't figure it out. 我已经在excel宏中多次执行了此过程,但这是第一次尝试使用vb.net进行操作,无法解决。 I have an xlsx with rows and columns of data in it.
我有一个xlsx,其中包含数据的行和列。 I would like to transfer the data over to a template that I made that has graphs waiting for the data.
我想将数据转移到我制作的模板中,该模板具有等待数据的图形。 The data needs to go to the sheet2, this file is a xlsm (read only).
数据需要转到sheet2,该文件是xlsm(只读)。 Nothing that I have tried from vba will work in vb.net.
我从vba尝试过的任何方法都无法在vb.net中工作。 different syntax.
不同的语法。 Help.
救命。
The code I have so far: 我到目前为止的代码:
Imports System
Imports System.IO
Imports System.Text
Imports Excel = Microsoft.Office.Interop.Excel
Imports Office = Microsoft.Office.Core
Public Class Form1
Private Sub cbo_FileList_Click(sender As Object, e As EventArgs) Handles cbo_FileList.Click
Dim folderpath = "C:\Users\aholiday\Desktop\Data Dump"
cbo_FileList.Items.Clear()
For Each file As String In System.IO.Directory.GetFiles(folderpath)
cbo_FileList.Items.Add(System.IO.Path.GetFileNameWithoutExtension(file))
Next
End Sub
Private Sub btn_Do_Click(sender As Object, e As EventArgs) Handles btn_Do.Click
Dim txtpath As String
Dim csvpath As String = "C:\Temp"
Dim FileTXT As String
Dim folderpath As String
Dim FinalFile As String
folderpath = "C:\Users\aholiday\Desktop\Data Dump"
FileTXT = cbo_FileList.Text
csvpath = "C:\Temp\" & FileTXT & ".csv"
txtpath = folderpath & "\" & FileTXT & ".txt"
FinalFile = "C:\Users\aholiday\Desktop\Book1"
File.Copy(txtpath, csvpath)
Process.Start("EXCEL.EXE", csvpath)
Process.Start("EXCEL.EXE", FinalFile)
'Need to have copy past and close here
'File.Delete(csvpath)
End Sub
End Class
Take a look at EPPlus (you can get this via nuget package manager) ... It´sa complete xlsx handling lib very useful for handling that. 看一下EPPlus(您可以通过nuget软件包管理器来获得它)...这是一个完整的xlsx处理lib,对于处理该文件非常有用。
There are also closed source libs, and also Excel Interop would be possible - but would need installed Office. 也有封闭的源代码库,也可以使用Excel Interop-但需要安装Office。
I used it already to make some customers happy 我已经用它来使一些顾客满意
Try this code to copy data from one workbook to another: 尝试使用以下代码将数据从一个工作簿复制到另一个工作簿:
Public Sub CopyData()
Dim ExcelApp As New Microsoft.Office.Interop.Excel.Application
Dim WB1, WB2 As Workbook
Try
'optional
ExcelApp.Visible = False
'open both workbooks
WB1 = ExcelApp.Application.Workbooks.Open("C:\...\File1.xlsx")
WB2 = ExcelApp.Application.Workbooks.Open("C:\...\File2.xlsx")
'rows
For r As Integer = 1 To 5
'columns
For c As Integer = 1 To 5
'copy the value of the cell
WB2.Sheets(2).Cells(r, c).Value = WB1.Sheets(1).Cells(r, c).Value
'or text:
'WB2.Sheets(2).Cells(r, c).Text = WB1.Sheets(1).Cells(r, c).Text
Next
Next
'save the second workbook
WB2.Save()
'close the workbooks
WB1.Close()
WB2.Close()
'and quit the Excel application
ExcelApp.Quit()
Catch ex As Exception
MessageBox.Show(ex.Message)
ExcelApp.Quit()
End Try
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.