简体   繁体   English

如何使用vb.net将文本从一个Excel文件传输到另一个

[英]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.

相关问题 想要使用 VB.net 中的按钮将文件从一个位置传输到另一个位置 - Wanted to Transfer the file from one location to another location using Button in VB.net 从PHP到vb.net; 如何在Visual Basic Asp.net中将文本从一页转移到另一页 - From PHP to vb.net; How to transfer a text from one page to another in Visual Basic Asp.net 如何使用VB.NET中的电子表格光源将一个Excel文件复制到另一个文件? - How to copy one Excel file to another using spreadsheet light in VB.NET? 如何将图像从一个图片框传输到另一个图片框? VB.NET & SQL - How can I Transfer an image from one picturebox to another? VB.NET & SQL 使用vb.net进行ftp文件传输 - ftp file transfer using vb.net 如何使用vb.net传输文件 - how to transfer files using vb.net 在VB.NET中以编程方式从Excel文件中删除垃圾文本 - Programmatically Removing Garbage Text from Excel File in VB.NET 使用 VB.net,如何将一本书中的一个工作表中的一列复制到另一本书的另一个工作表中? - Using VB.net, how to copy a column from one worksheet in one book to another worksheet in another book? 如何将文本框文本插入到现有的Excel文件中? vb.net - how to insert textbox text to existing excel file? vb.net 使用vb.net从Excel文件获取列表列表 - Get list of lists from excel file using vb.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM