2014-06-10 88 views
0

我有一個簡單的表單在Visual Studio(VB)中創建,它有一個數據gridview連接到MySQL中的一個表(託管在遠程服務器)。導出Gridview連接到MYSQL數據excel

我有下面的代碼導出網格視圖到Excel,但它需要很長時間才能導出(大約15分鐘)。

MySQL中的表格非常小​​(1000行和60列)。

是否有更好的方式將完整的MySQL表導出爲ex​​cel?

請幫助

CODE:

Dim xlApp As Microsoft.Office.Interop.Excel.Application 
     Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook 
     Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet 
     Dim misValue As Object = System.Reflection.Missing.Value 
     Dim i As Integer 
     Dim j As Integer 


     xlApp = New Microsoft.Office.Interop.Excel.Application 
     xlWorkBook = xlApp.Workbooks.Add(misValue) 
     xlWorkSheet = xlWorkBook.Sheets("sheet1") 
     For i = 0 To DataGridView1.Rows.Count - 1 
      For j = 0 To DataGridView1.Columns.Count - 1 
       For k As Integer = 1 To DataGridView1.Columns.Count 
        On Error Resume Next 
        xlWorkSheet.Cells(1, k) = DataGridView1.Columns(k - 1).HeaderText 
        xlWorkSheet.Cells(i + 2, j + 1) = DataGridView1(j, i).Value.ToString() 
       Next 
      Next 
     Next 
     xlWorkSheet.SaveAs("C:\Users\USERNAME\Desktop\vbexcel.xlsx") 
     xlWorkBook.Close() 

回答

0

有兩種方法可以做到這一點:

  1. 使用RDLC報告。
  2. 使用gridview導出。

你需要創建RDLC報告第一種方式,然後檢索數據到數據表中,然後調用此代碼:

Public Shared Sub ExportGridViewToExcelGridView(ByVal Filename As String, ByRef gvr As GridView, ByRef currentPage As Page) 

    Dim HtmlForm As System.Web.UI.HtmlControls.HtmlForm = New System.Web.UI.HtmlControls.HtmlForm() 
    currentPage.Controls.Add(HtmlForm) 
    HtmlForm.Controls.Add(gvr) 

    currentPage.Response.Clear() 
    currentPage.Response.Buffer = True 
    currentPage.Response.AddHeader("Content-Disposition", "attachment; filename=" & Filename) 
    currentPage.Response.ContentType = "application/vnd.ms-excel" 
    currentPage.Response.ContentEncoding = System.Text.Encoding.UTF8 
    currentPage.Response.Charset = "" 
    currentPage.EnableViewState = False 
    Using strwriter As New StringWriter 
     Dim htmlwrt As HtmlTextWriter = New HtmlTextWriter(strwriter) 
     HtmlForm.RenderControl(htmlwrt) 
     htmlwrt.Flush() 
     currentPage.Response.Write(strwriter.ToString) 
     currentPage.Response.End() 
    End Using 
End Sub 

確保:

Dim MyDataSource As ReportDataSource = New ReportDataSource("ReportDataSet", MyDataTable) 
     reportviewer1.LocalReport.ReportPath = Server.MapPath("MyrdlcReportPath") 
     rvSmartCardsIssues.LocalReport.EnableExternalImages = True 
     rvSmartCardsIssues.LocalReport.DataSources.Clear() 
     rvSmartCardsIssues.LocalReport.DataSources.Add(MyDataSource) 
     rvSmartCardsIssues.LocalReport.Refresh() 

     Dim warnings As Warning() = Nothing 
     Dim streamids As String() = Nothing 
     Dim mimeType As String = Nothing 
     Dim encoding As String = Nothing 
     Dim extension As String = Nothing 
     Dim bytes As Byte() 
     bytes = rvSmartCardsIssues.LocalReport.Render("Excel", Nothing, mimeType, encoding, extension, streamids, warnings) 
     HttpContext.Current.Response.Buffer = True 
     HttpContext.Current.Response.Clear() 
     HttpContext.Current.Response.ContentType = mimeType 
     HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=ExportedFileName.xls") 
     HttpContext.Current.Response.BinaryWrite(bytes) 
     HttpContext.Current.Response.Flush() 
     HttpContext.Current.Response.End() 

,基於此功能第二種方法當您使用第二種方法將gridview分頁屬性設置爲false時。