将DataTable导出到Excel

我有一个小的Web应用程序,从jQuery的dateselect器创build两个数据表。 当然,如果它们在同一页面上,我可以将这些数据表导出为ex​​cel。

我已经改变了我的应用程序来渲染新的webforms数据表。

这里是我的代码导出到Excel中:

protected void ExportToExcel(object sender, EventArgs e) { Response.Clear(); Response.Buffer = true; Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls"); Response.Charset = ""; Response.ContentType = "application/vnd.ms-excel"; using (StringWriter sw = new StringWriter()) { HtmlTextWriter hw = new HtmlTextWriter(sw); //To Export all pages this.BindGrid1(TextDateFrom.Text, TextDateTo.Text); GridView2.HeaderRow.BackColor = Color.White; foreach (TableCell cell in GridView2.HeaderRow.Cells) { cell.BackColor = GridView2.HeaderStyle.BackColor; } foreach (GridViewRow row in GridView2.Rows) { row.BackColor = Color.White; foreach (TableCell cell in row.Cells) { if (row.RowIndex % 2 == 0) { cell.BackColor = GridView2.AlternatingRowStyle.BackColor; } else { cell.BackColor = GridView2.RowStyle.BackColor; } cell.CssClass = "textmode"; } } GridView2.RenderControl(hw); //style to format numbers to string string style = @"<style> .textmode { } </style>"; Response.Write(style); Response.Output.Write(sw.ToString()); Response.Flush(); Response.End(); } } 

这是我遇到麻烦的地方:

  this.BindGrid1(TextDateFrom.Text, TextDateTo.Text); 

当然,我的BindGrid1()是另一种forms。 要以我的新forms调用我的数据表,我创build了一个会话。

如果我有在数据表的网页表单上的代码:

  DataTable dt = (DataTable)Session["GridData"]; if (dt.Rows.Count > 0) { GridView1.DataSource = dt; GridView1.DataBind(); } 

我不确定如何调用这个。 导出所有页面。 我应该创build一个全局variables,从BindGrid1方法的string值,然后在新的页面上使用?

与您的任务相关,从DataTable dt到Excel的数据导出可以使用以下程序来实现,该程序使用Microsoft.Office.Interop.Excel对象库:

 /// <summary> /// export DataTable to Excel (C#) /// </summary> internal static void Export2Excel(DataTable dataTable) { object misValue = System.Reflection.Missing.Value; Microsoft.Office.Interop.Excel.Application _appExcel = null; Microsoft.Office.Interop.Excel.Workbook _excelWorkbook = null; Microsoft.Office.Interop.Excel.Worksheet _excelWorksheet = null; try { // excel app object _appExcel = new Microsoft.Office.Interop.Excel.Application(); // excel workbook object added to app _excelWorkbook = _appExcel.Workbooks.Add(misValue); _excelWorksheet = _appExcel.ActiveWorkbook.ActiveSheet as Microsoft.Office.Interop.Excel.Worksheet; // column names row (range obj) Microsoft.Office.Interop.Excel.Range _columnsNameRange; _columnsNameRange = _excelWorksheet.get_Range("A1", misValue).get_Resize(1, dataTable.Columns.Count); // column names array to be assigned to _columnNameRange string[] _arrColumnNames = new string[dataTable.Columns.Count]; for (int i = 0; i < dataTable.Columns.Count; i++) { // array of column names _arrColumnNames[i] = dataTable.Columns[i].ColumnName; } // assign array to column headers range, make 'em bold _columnsNameRange.set_Value(misValue, _arrColumnNames); _columnsNameRange.Font.Bold = true; // populate data content row by row for (int Idx = 0; Idx < dataTable.Rows.Count; Idx++) { _excelWorksheet.Range["A2"].Offset[Idx].Resize[1, dataTable.Columns.Count].Value = dataTable.Rows[Idx].ItemArray; } // Autofit all Columns in the range _columnsNameRange.Columns.EntireColumn.AutoFit(); } catch { throw; } } 

只是把dt作为一个参数。

希望这可能有帮助。