| |

VerySource

 Forgot password?
 Register
Search
View: 829|Reply: 5

Ask a simple question of VB.NET

[Copy link]

3

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-10-16 09:00:01
| Show all posts |Read mode
I want to use EXCEL to export data from DATAGRIDVIEW1 and wrote the following code:
Public Sub ExportToExcel()
        If DataGridView1.RowCount> 0 Then
            Try
                Dim ds As New DataSet
                ds = DataGridView1.DataSource
                Dim i, j As Integer
                Dim rows As Integer = ds.Tables(0).Rows.Count
                Dim cols As Integer = ds.Tables(0).Columns.Count
                Dim DataArray(rows-1, cols-1) As String
                For i = 0 To rows-1
                    For j = 0 To cols-1
                        If ds.Tables(0).Rows(i).Item(j) Is System.DBNull.Value Then
                        Else
                            DataArray(i, j) = ds.Tables(0).Rows(i).Item(j)
                        End If
                    Next
                Next
                Dim myExcel As Excel.Application = New Excel.Application
                myExcel.Application.Workbooks.Add(True)
                myExcel.Visible = True
                For j = 0 To cols-1
                    myExcel.Cells(1, j + 1) = ds.Tables(0).Columns(j).ColumnName
                Next
                myExcel.Range("A2").Resize(rows, cols).Value = DataArray
            Catch exp As Exception
                MessageBox.Show("Data export failed! Please check whether Excel has been installed", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning)
            End Try
        Else
            MessageBox.Show("No data!", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

However, there will be an error in the sentence Dim myExcel As Excel.Application = New Excel.Application. The error message is: undefined type EXCEL.application. How to change it?
Reply

Use magic Report

0

Threads

7

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-10-16 10:00:02
| Show all posts
Excel.Appplication is an abstract class
To use the method of obtaining, you cannot use the method of construction
Please refer to the Office Development Manual in detail
Reply

Use magic Report

0

Threads

48

Posts

30.00

Credits

Newbie

Rank: 1

Credits
30.00

 Unknown

Post time: 2020-10-16 16:15:01
| Show all posts
Excel is not quoted
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-10-16 16:45:01
| Show all posts
I have encountered this problem
Change Dim myExcel As Excel.Application = New Excel.Application to
  Dim myExcel As Interop.Excel.Application = New Interop.Excel.ApplicationClass
That's it
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-10-16 17:00:01
| Show all posts
The following is my program snippet from DataTable to Excel, see if it helps
Public Sub DStoXls(ByVal Table As DataTable, ByVal DefFileName As String)
  
        Dim MyOleDbCn As New System.Data.OleDb.OleDbConnection
        Dim MyOleDbCmd As New System.Data.OleDb.OleDbCommand
        Dim MyTable As New DataTable
        Dim intRowsCnt, intColsCnt As Integer
        Dim strSql As String, strFlName As String
        Dim Fso As New System.Object
        If Table Is Nothing Then
            MessageBox.Show("Data not obtained, unable to export", "Export error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If
        MyTable = Table
        If MyTable.Rows.Count = 0 Then
            MessageBox.Show("Data not obtained, unable to export", "Export error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If
        Dim FileName As String
        Dim SaveFileDialog As New SaveFileDialog
        SaveFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
        SaveFileDialog.Title = "Save as"
        SaveFileDialog.Filter = ".xls|*.xls"
        SaveFileDialog.FileName = DefFileName
        If (SaveFileDialog.ShowDialog() = System.Windows.Forms.DialogResult.OK) Then
            FileName = SaveFileDialog.FileName
            'TODO: Add the code to open the file here.
        End If
        If FileName = "" Then Exit Sub
        strFlName = FileName
        If Dir(FileName) <> "" Then
            Kill(FileName)
        End If
        Try
            Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor

            MyOleDbCn.ConnectionString = "Provider=Microsoft.Jet.OleDb.4.0;"&_
            "Data Source="&strFlName&";"&_
            "Extended ProPerties=""Excel 8.0;HDR=Yes;"""
            MyOleDbCn.Open()
            MyOleDbCmd.Connection = MyOleDbCn
            MyOleDbCmd.CommandType = CommandType.Text

            'Insert column headers in the first row
            strSql = "CREATE TABLE "&DefFileName&"("
            For intColsCnt = 0 To MyTable.Columns.Count-1
                If intColsCnt <> MyTable.Columns.Count-1 Then
                    strSql = strSql&ChangeChar(MyTable.Columns(intColsCnt).Caption)&"text,"
                Else
                    strSql = strSql&ChangeChar(MyTable.Columns(intColsCnt).Caption)&"text)"
                End If
            Next
            MyOleDbCmd.CommandText = Replace(strSql, ".", "_")'When creating a table, "." field names are not allowed, so use "&#717;" to replace
            MyOleDbCmd.ExecuteNonQuery()

            'Insert rows
            For intRowsCnt = 0 To MyTable.Rows.Count-1
                strSql = "INSERT INTO "&DefFileName&" VALUES('"
                For intColsCnt = 0 To MyTable.Columns.Count-1
                    If intColsCnt <> MyTable.Columns.Count-1 Then
                        strSql = strSql&ChangeChar(MyTable.Rows(intRowsCnt).Item(intColsCnt))&"','"
                    Else
                        strSql = strSql&ChangeChar(MyTable.Rows(intRowsCnt).Item(intColsCnt))&"')"
                    End If
                Next
                MyOleDbCmd.CommandText = strSql
                MyOleDbCmd.ExecuteNonQuery()
            Next
            MessageBox.Show("Data has been successfully imported into EXCEL file"&strFlName, "Data Export", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ErrCode As Exception
            MsgBox("Error message:"&ErrCode.Message&vbCrLf&vbCrLf&_
            "Event raised:"&ErrCode.TargetSite.ToString, MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "Error source:"&ErrCode.Source)
            Exit Sub
            'Finally
            MyOleDbCmd.Dispose()
            MyOleDbCn.Close()
            MyOleDbCn.Dispose()
        End Try

    End Sub
Reply

Use magic Report

0

Threads

10

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-10-16 17:15:01
| Show all posts
I have encountered similar problems. .

It does not matter after adding a reference in the development environment. . . . .
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list