none
Microsoft.Office.Interop.Excel and WES7 RRS feed

  • Domanda

  • Hi,

    I use the attached code to save the data in a Datagridview to an Excel file ...

    use as a reference "Microsoft.Office.Interop.Excel 14.0.0.0"

    The application runs on S.O. WES7, where I installed "o2010pia.msi"

    but I get the following error: (the following code ...)

    Imports Excel = Microsoft.Office.Interop.Excel
    Imports System.Data.OleDb
    Imports System.IO
    Imports System
    
    Public Class Form1
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim I As Integer, CopiaOk As Boolean = False
            CopiaOk = False
            For Each drive As DriveInfo In My.Computer.FileSystem.Drives
                If drive.DriveType = DriveType.Removable Then
                    Dim FilePath = drive.Name + "BACKUP.xlsx"
                    'MsgBox(FilePath)
                    If File.Exists(FilePath) Then
                        File.Delete(FilePath)
                    End If
                    Dim oExcel As Object
                    oExcel = CreateObject("Excel.Application")
                    Dim oBook As Excel.Workbook
                    Dim oSheet As Excel.Worksheet
                    oBook = oExcel.Workbooks.Add
                    oSheet = oExcel.Worksheets(1)
                    For I = 0 To DataGridView1.RowCount - 1
                        oSheet.Cells(I + 2, 1) = DataGridView1.Rows(I).Cells(1).Value
                        oSheet.Cells(I + 2, 2) = DataGridView1.Rows(I).Cells(2).Value
                        oSheet.Cells(I + 2, 3) = DataGridView1.Rows(I).Cells(3).Value
                        oSheet.Cells(I + 2, 4) = DataGridView1.Rows(I).Cells(4).Value
                        oSheet.Cells(I + 2, 5) = DataGridView1.Rows(I).Cells(5).Value
                        oSheet.Cells(I + 2, 6) = DataGridView1.Rows(I).Cells(6).Value
                        oSheet.Cells(I + 2, 7) = DataGridView1.Rows(I).Cells(7).Value
                        oSheet.Cells(I + 2, 8) = DataGridView1.Rows(I).Cells(8).Value
                        oSheet.Cells(I + 2, 9) = DataGridView1.Rows(I).Cells(9).Value
                        oSheet.Cells(I + 2, 10) = DataGridView1.Rows(I).Cells(10).Value
                    Next
                    oBook.SaveAs(FilePath)
                    oBook.Close()
                    oBook = Nothing
                    oExcel.Quit()
                    oExcel = Nothing
                    CopiaOk = True
                    Exit For
                End If
            Next
                 
        End Sub
    End Class

     
    • Modificato Tnn-msdn martedì 1 luglio 2014 07:18
    martedì 1 luglio 2014 07:15