简体   繁体   中英

Export DataSet to Multiple Excel Sheets and download into a zip file using asp.net C#

嗨,如何将DataSet导出到多个Excel工作表并将这些文件下载到C#asp.net中的zip文件中?

This has already been well covered on Stack Overflow. First of all, to create your Excel spreadsheets, check this previous post:

Next to zip the files, check these excellent answers:

If you want to automatically stream it back to the user, then try these answers:

Private Sub CopySheet() Dim cmd As OleDbCommand Dim dt As String Dim ds As DataSet = New DataSet() Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\EXCEL\\From.xls;Extended Properties=Excel 8.0" Dim oledbConn As OleDbConnection = New OleDbConnection(connString) Try oledbConn.Open() For i = 0 To 1 If i = 0 Then cmd = New OleDbCommand("SELECT * FROM [Sheet1$]", oledbConn) dt = "Sheet3" Else cmd = New OleDbCommand("SELECT * FROM [Sheet2$]", oledbConn) dt = "Sheet4" End If Dim oleda As OleDbDataAdapter = New OleDbDataAdapter() oleda.SelectCommand = cmd oleda.Fill(ds, dt) Next

        ExportDatasetToExcel(ds, "dd")
    Catch
    Finally
        oledbConn.Close()
    End Try
End Sub

Public Sub ExportDatasetToExcel(ByVal ds As DataSet, ByVal strExcelFile As String)

    Dim conn As New OleDbConnection(String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\EXCEL\To.xls;Extended Properties=Excel 8.0"))
    conn.Open()
    Dim strTableQ(ds.Tables.Count) As String
    Dim i As Integer = 0
    'making table query
    For i = 0 To ds.Tables.Count - 1
        strTableQ(i) = "CREATE TABLE [" & ds.Tables(i).TableName & "]("
        Dim j As Integer = 0
        For j = 0 To ds.Tables(i).Columns.Count - 1
            Dim dCol As DataColumn
            dCol = ds.Tables(i).Columns(j)
            strTableQ(i) &= " [" & dCol.ColumnName & "] varchar(255) , "
        Next
        strTableQ(i) = strTableQ(i).Substring(0, strTableQ(i).Length - 2)
        strTableQ(i) &= ")"
        Dim cmd As New OleDbCommand(strTableQ(i), conn)
        cmd.ExecuteNonQuery()
    Next
    'making insert query
    Dim strInsertQ(ds.Tables.Count - 1) As String
    For i = 0 To ds.Tables.Count - 1
        strInsertQ(i) = "Insert Into " & ds.Tables(i).TableName & " Values ("
        For k As Integer = 0 To ds.Tables(i).Columns.Count - 1
            strInsertQ(i) &= "@" & ds.Tables(i).Columns(k).ColumnName & " , "
        Next
        strInsertQ(i) = strInsertQ(i).Substring(0, strInsertQ(i).Length - 2)
        strInsertQ(i) &= ")"
    Next
    'Now inserting data
    For i = 0 To ds.Tables.Count - 1
        For j As Integer = 0 To ds.Tables(i).Rows.Count - 1
            Dim cmd As New OleDbCommand(strInsertQ(i), conn)
            For k As Integer = 0 To ds.Tables(i).Columns.Count - 1
                cmd.Parameters.AddWithValue("@" & ds.Tables(i).Columns(k).ColumnName.ToString(), ds.Tables(i).Rows(j)(k).ToString())
            Next
            cmd.ExecuteNonQuery()
            cmd.Parameters.Clear()
        Next
    Next

    conn.Close()
End Sub

NPoi用于导出, DotNetZip库用于压缩它。

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