简体   繁体   中英

Reading xlsx file int vb.net VS 2012

I'm trying to read/write an .xlsx file with vb.net, but I have some problems. I'm using the following code:

Imports Microsoft.Office.Interop.Excel
Imports Microsoft.Office.Interop

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Dim fileTest As String = "C:\testFile.xlsx"
        Dim oExcel As Object
        oExcel = CreateObject("Excel.Application")
        oExcel.Workbooks.Open(fileTest)
        Dim oBook As Excel.Workbook
        Dim oSheet As Excel.Worksheet
        oBook = oExcel.ActiveWorkbook
        oSheet = oExcel.Worksheets(1)

        oSheet.Range("C2").Value = "testing c2"

        oExcel.DisplayAlerts = False
        oBook.SaveAs(fileTest, 51)
        oBook.Close()
        oBook = Nothing

    End Sub
End Class

What I get is this dialog:

And when I open the testFile.xlsx I get just a bunch of some awkward symbols.

What might be the issue (that I suspect) is the thing that there are two versions of Excel application on my computer: Excel 2007 and Excel 2003, in order of installation. When I go to Task Manager, Excel applicaton which is running is 2003 version (which can't handle .xlsx files). Maybe the solution could be to force vb.net application to use 2007 version of Excel, but I don't know how to do that (if it's even possible).

Any help would be appreciated.

And I have tried number of codes found on internet for reading .xlsx files, but with no success.

you need to update your Interop dll to point to your 2007 excel assembly. Try this for reference:

https://social.msdn.microsoft.com/Forums/vstudio/en-US/87e65b6a-f211-4b4a-9d42-a264dcf2a6f4/microsoftofficeinteropexceldll?forum=csharpgeneral

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM