读取Excel文件并使用vb.net查找第一个空单元格

时间:2017-02-16 19:26:11

标签: excel vb.net

我必须创建一个桌面应用程序。 我使用Visual Studio 2015和Excel 我必须 : - 打开一个excel文件 - 找到第一个空行 - 在该行中写入新数据 - 保存那个文件(目前我只能创建一个新文件。我不知道如何更新广告保存同样我打开) 它一直有效,直到我打开文件.. 谢谢! 这是我写的用来打开文件的函数:

Public Function OpenExcel(filename As String) As Object
        Dim retval As Object = Nothing
        Try
            Dim excel As Microsoft.Office.Interop.Excel.Application
            Dim wb As Microsoft.Office.Interop.Excel.Workbook
            Dim ws As Microsoft.Office.Interop.Excel.Worksheet
            excel = New Microsoft.Office.Interop.Excel.Application
            wb = excel.Workbooks.Open(filename)
            excel.Visible = False
            wb.Activate()
            ws = wb.Worksheets.Item(1)
            ws = excel.ActiveSheet
            retval = ws.Cells(1, 1).value

        Catch ex As exception

        End Try
    End Function

这是我的代码的第一部分

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        Dim dialogo As OpenFileDialog
        dialogo = New OpenFileDialog()
        dialogo.ShowDialog()
        Dim ds As Object = f.OpenExcel(dialogo.FileName)

 Dim oExcel As Object
        oExcel = CreateObject("Excel.Application")
        Dim oBook As Object
        Dim oSheet1 As Object

        oBook = oExcel.Workbooks.Add()
        oSheet1 = oBook.Worksheets(1)

        oBook = oExcel.ActiveWorkbook

        Dim UR As Integer = 0
        UR = oSheet1.Range("A" & oSheet1.Rows.Count).End(Microsoft.Office.Interop.Excel.XlDirection.xlUp).Row
        For rr = 1 To UR
            If oSheet1.Range("A" & rr).Value = ("") Then
                oSheet1.Range("A" & rr).value = "text1"
            Else
                MsgBox("box pieno")
            End If
        Next rr

oExcel.DisplayAlerts = False
        oBook.SaveAs("C:\Users\an\Desktop\New" & ".xlsx")
        oBook.Close()
        oBook = Nothing

2 个答案:

答案 0 :(得分:0)

几乎拥有它。

您的功能已打开文件;只需更改它即可返回工作簿。

Public Function OpenExcel(ByVal filename As String) As Object
    Dim excel As Microsoft.Office.Interop.Excel.Application
    Dim wb As Microsoft.Office.Interop.Excel.Workbook
    excel = New Microsoft.Office.Interop.Excel.Application
    excel.Visible = False
    wb = excel.Workbooks.Open(filename, ReadOnly:=False)
    excel.Visible = False
    Return wb
End Function

(顺便说一句,在这里进行异常处理并不是一个好主意,除非你返回“status”值或默认文件。让调用者处理异常)

...然后让其余的代码更改并保存已打开的书

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim dialogo As OpenFileDialog
    dialogo = New OpenFileDialog()
    dialogo.ShowDialog()
    Dim oBook As Microsoft.Office.Interop.Excel.Workbook
    Dim oSheet1 As Object

    Try
        oBook = OpenExcel(dialogo.FileName)
    Catch ex As Exception
        'handle your exception here
        Exit Sub
    End Try

    oSheet1 = oBook.Worksheets(1)

    Dim UR As Integer = 0
    UR = oSheet1.Range("A" & oSheet1.Rows.Count).End(Microsoft.Office.Interop.Excel.XlDirection.xlUp).Row
    For rr = 1 To UR
        If oSheet1.Range("A" & rr).Value Is Nothing OrElse oSheet1.Range("A" & rr).Value.ToString() = ("") Then
            oSheet1.Range("A" & rr).value = "text1"
        End If
    Next rr

    Dim excel = oBook.Application

    oBook.Save()
    oBook.Close()
    excel.Quit() 'This is important: as your application is not visible, it would stay on memory until reboot or shutdown if you don't close it here
    oBook = Nothing
End Sub

答案 1 :(得分:0)

谢谢。它有效,我可以写下一个单元格。 但有个问题: 在我选择文件之前,如果我检查在我的电脑中打开的后台进程,我就没有任何Excel。 我选择了要打开的文件 - 窗户挡住了自己 - 在后台进程中,出现excel 现在,如果我关闭该过程,我再次按下打开按钮它的工作原理。 我不明白发生了什么