Exporting data into Excel with multiple spreadsheets

你离开我真会死。 提交于 2019-12-13 04:28:31

问题


Has anyone successfully transferred data into Excel with multiple spreadsheets? I'm stuck with it. I'm using Visual Basic 2010.


回答1:


Yes, using EPPlus. It's as simple as:

Dim ws = package.Workbook.Worksheets.Add("Name") 

Thanks Tim :)




回答2:


You can use this method to export data to excel file:

Public Shared Function ExportDataTableToDataFile(ByVal srcDataTable As DataTable, ByVal dbTbName As String, _
                                               ByVal dbFilePath As String, ByVal dbFileType As String, _
                                               Optional ByVal schemaTable As DataTable = Nothing) As Boolean

        Dim expConnStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbFilePath

        Select Case dbFileType
            Case ".dbf"
                If dbTbName.Length > 8 Then
                    dbTbName = dbTbName.Remove(8)
                End If

                expConnStr &= ";Extended Properties=dBase IV"
            Case ".xls"
                expConnStr &= dbTbName & ".xls;Extended Properties=""Excel 8.0;HDR=YES;IMEX=0"""
            Case ".mdb"
                expConnStr &= dbTbName & ".mdb;User Id=admin;Password="
            Case ".csv"
                expConnStr &= ";Extended Properties=""text;HDR=Yes;FMT=Delimited(,)"""
                dbTbName &= ".csv"
            Case ".mpp"
                expConnStr = "Provider=Microsoft.Project.OLEDB.10.0;Project Name=" & dbFilePath & dbTbName & ".mpp"
            Case Else
                Return False
        End Select

        Dim res As Boolean = True
        Dim createCmdStr As String = "CREATE TABLE [" & dbTbName & "] ("
        Dim insertCmdStr As String = "INSERT INTO [" & dbTbName & "] ("
        Dim insertParams As String = " VALUES ("
        Dim paramPrefix As String = "@p_"
        Dim oleDbCon As New OleDbConnection(expConnStr)
        Dim oleDbDa As New OleDbDataAdapter
        Dim createCmd As New OleDbCommand
        Dim insertCmd As New OleDbCommand
        Dim param As OleDbParameter
        Dim oleDbColTp As OleDbType

        Try
            If String.IsNullOrEmpty(dbTbName) Or String.IsNullOrEmpty(dbFilePath) Or srcDataTable Is Nothing Then
                res = False
                Return res
            End If

            If Not System.IO.Directory.Exists(dbFilePath) Then
                System.IO.Directory.CreateDirectory(dbFilePath)
            End If

            If System.IO.File.Exists(dbFilePath & dbTbName & dbFileType) Then
                System.IO.File.Delete(dbFilePath & dbTbName & dbFileType)
            End If

            If dbFileType = ".mdb" Then
                Dim ADOXCatalog As New ADOX.Catalog

                Try
                    ADOXCatalog.Create(expConnStr)
                Catch ex As System.Runtime.InteropServices.COMException
                Catch ex As Exception
                    MessageBox.Show(ex.Source & ": " & ex.Message.ToString, "Chyba!")
                    Return False
                Finally
                    ADOXCatalog = Nothing
                End Try
            End If

            For Each col As DataColumn In srcDataTable.Columns
                If col.DataType Is GetType(DateTime) Then
                    oleDbColTp = OleDbType.VarWChar
                Else
                    If schemaTable IsNot Nothing Then
                        oleDbColTp = schemaTable.Rows(srcDataTable.Columns.IndexOf(col))(11)
                    Else
                        oleDbColTp = GetOleDbType(col.DataType)
                    End If
                End If

                createCmdStr &= "[" & col.ColumnName & "] " & GetSqlDbType(col.DataType).ToString

                If col.DataType Is GetType(String) Then 'OleDb dovoluje max dlzku 255 pre VarChar
                    If col.MaxLength > 255 Then
                        col.MaxLength = 255
                    End If

                    createCmdStr &= " (" & col.MaxLength & ")"
                End If

                'createCmdStr &= IIf(col.AllowDBNull, "", " NOT NULL")
                insertCmdStr &= "[" & col.ColumnName & "]"
                insertParams &= "?"

                param = insertCmd.Parameters.Add(paramPrefix & col.ColumnName, oleDbColTp, col.MaxLength, col.ColumnName)
                param.SourceVersion = DataRowVersion.Current

                If Array.IndexOf(srcDataTable.PrimaryKey, col) >= 0 Then
                    createCmdStr &= " PRIMARY KEY"
                ElseIf col.Unique Then
                    createCmdStr &= " UNIQUE"
                End If

                If srcDataTable.Columns.IndexOf(col) < srcDataTable.Columns.Count - 1 Then
                    createCmdStr &= ", "
                    insertCmdStr &= ", "
                    insertParams &= ", "
                Else
                    createCmdStr &= ")"
                    insertCmdStr &= ")"
                    insertParams &= ")"
                End If
            Next

            insertCmdStr &= insertParams
            createCmd.Connection = oleDbCon
            createCmd.CommandText = createCmdStr
            insertCmd.Connection = oleDbCon
            insertCmd.CommandText = insertCmdStr
            oleDbDa.InsertCommand = insertCmd

            For Each drow As DataRow In srcDataTable.Rows
                drow.AcceptChanges()
                drow.SetAdded()
            Next

            If oleDbCon.State <> ConnectionState.Closed Then
                oleDbCon.Close()
            End If

            oleDbCon.Open()
            'createCmd.ExecuteNonQuery()
            oleDbDa.Update(srcDataTable)
        Catch ex As Exception
            res = False
            MessageBox.Show(ex.Message.ToString, "Chyba!")
        Finally
            oleDbCon.Close()
        End Try

        Return res

    End Function


来源:https://stackoverflow.com/questions/9038691/exporting-data-into-excel-with-multiple-spreadsheets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!