Does someone know how I can export a gridview in ASP.net to Excel??
有人知道我如何在ASP.net中导出gridview的Excel吗?
I use this code but I can't access my gridview because it's null
我使用这个代码,但是我不能访问gridview,因为它是空的。
protected void BtnExport_Click(object sender, EventArgs e)
{
DataTable dtOriginal = new DataTable();
dtOriginal = (DataTable)gvRapporten.DataSource; //Return Table consisting data
//Create Tempory Table
DataTable dtTemp = new DataTable();
//Creating Header Row
dtTemp.Columns.Add("<b>Melder</b>");
dtTemp.Columns.Add("<b>Onderwerp</b>");
dtTemp.Columns.Add("<b>Oplosser</b>");
dtTemp.Columns.Add("<b>Niveau 2</b>");
DataRow drAddItem;
for (int i = 0; i < dtOriginal.Rows.Count; i++)
{
drAddItem = dtTemp.NewRow();
drAddItem[0] = dtOriginal.Rows[i][0].ToString();//Melder
drAddItem[1] = dtOriginal.Rows[i][1].ToString();//Onderwerp
drAddItem[2] = dtOriginal.Rows[i][2].ToString();//Oplosser
drAddItem[3] = dtOriginal.Rows[i][3].ToString();//Niveau 2
dtTemp.Rows.Add(drAddItem);
}
//Temp Grid
DataGrid dg = new DataGrid();
dg.DataSource = dtTemp;
dg.DataBind();
ExportToExcel("Report.xls", dg);
dg = null;
dg.Dispose();
}
private void ExportToExcel(string strFileName, DataGrid dg)
{
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=" + strFileName);
Response.ContentType = "application/excel";
System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
dg.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
3 个解决方案
#1
2
can you try this ?
你能试试这个吗?
System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
Response.AddHeader("content-disposition", "attachment; filename=" + strFileName);
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
dg.RenderControl(htw);
Response.Write(sw.ToString());
Response.Flush();
Response.End();
#2
0
You code simply generates a Html-Table(that is what ASP.Net renders a GridView/DataGrid to) and nothing else. Excel can handle this and display it like a Table but it is not really a xls
- or csv
-file.
代码只生成一个html表(即ASP)。Net呈现一个GridView/DataGrid,而不是其他。Excel可以像表一样处理它,但它不是真正的xls-或csv文件。
The DataSource-property of a GridView is always null/nothing after postback. You should generate the DataSource again before you export it to excel.
在回发后,GridView的数据源属性总是空的。在将数据源导出为excel之前,您应该再次生成数据源。
This code converts any datatable to a csv-String:
此代码将任何datatable转换为csv-String:
C#
c#
public static string tableToCsv(DataTable DT)
{
System.Text.StringBuilder csv = new System.Text.StringBuilder();
System.Text.StringBuilder rowData = new System.Text.StringBuilder();
DataRow DR = null;
DataColumn DC = null;
foreach (DataColumn DC_loopVariable in DT.Columns) {
DC = DC_loopVariable;
rowData.Length = 0;
rowData.Append((rowData.Length > 0 ? ";" : "").ToString());
rowData.Append(DC.ColumnName);
}
csv.Append(rowData.ToString()).Append(Strings.Chr(13));
foreach (DataRow DR_loopVariable in DT.Rows) {
DR = DR_loopVariable;
rowData.Length = 0;
foreach (DataColumn DC_loopVariable in DT.Columns) {
DC = DC_loopVariable;
rowData.Append((rowData.Length > 0 ? ";" : "").ToString());
rowData.Append(DR[DC.ColumnName].ToString());
}
csv.Append(rowData.ToString()).Append(Strings.Chr(13));
}
return csv.ToString();
}
VB
VB
Public Shared Function tableToCsv(ByVal DT As DataTable) As String
Dim csv As New System.Text.StringBuilder
Dim rowData As New System.Text.StringBuilder
Dim DR As DataRow
Dim DC As DataColumn
For Each DC In DT.Columns
rowData.Length = 0
rowData.Append(IIf(rowData.Length > 0, ";", "").ToString)
rowData.Append(DC.ColumnName)
Next
csv.Append(rowData.ToString).Append(Chr(13))
For Each DR In DT.Rows
rowData.Length = 0
For Each DC In DT.Columns
rowData.Append(IIf(rowData.Length > 0, ";", "").ToString)
rowData.Append(DR.Item(DC.ColumnName).ToString)
Next
csv.Append(rowData.ToString).Append(Chr(13))
Next
Return csv.ToString
End Function
#3
-1
Dim dt As DataTable = ViewState("FinalState")
Dim GV As New GridView
GV.DataSource = dt ' UpdateDataTable(ViewState("FinalTable"))
GV.DataBind()
Response.Clear()
Response.Buffer = True
Response.AddHeader("content-disposition", "attachment;filename=ProspectsBank.xls")
Response.Charset = ""
Response.ContentType = "application/vnd.ms-excel"
Dim sw As New StringWriter()
Dim hw As New HtmlTextWriter(sw)
For i As Integer = 0 To GV.Rows.Count - 1
'Apply text style to each Row
GV.Rows(i).Attributes.Add("class", "textmode")
Next
GV.RenderControl(hw)
Dim style As String = "<style> .textmode { mso-number-format:\@; } </style>"
Response.Write(style)
Response.Output.Write(sw.ToString())
clsLog.SaveLogValue(Request.ServerVariables("REMOTE_ADDR"), "SearchAudienceForMagazine.aspx", "User Export Data From Our Databank in EXCEL format. Current they Exported " & dt.Rows.Count & " Data.", Session("User_TypeId"), Session("User_Name"))
Response.Flush()
Response.End()
#1
2
can you try this ?
你能试试这个吗?
System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
Response.AddHeader("content-disposition", "attachment; filename=" + strFileName);
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
dg.RenderControl(htw);
Response.Write(sw.ToString());
Response.Flush();
Response.End();
#2
0
You code simply generates a Html-Table(that is what ASP.Net renders a GridView/DataGrid to) and nothing else. Excel can handle this and display it like a Table but it is not really a xls
- or csv
-file.
代码只生成一个html表(即ASP)。Net呈现一个GridView/DataGrid,而不是其他。Excel可以像表一样处理它,但它不是真正的xls-或csv文件。
The DataSource-property of a GridView is always null/nothing after postback. You should generate the DataSource again before you export it to excel.
在回发后,GridView的数据源属性总是空的。在将数据源导出为excel之前,您应该再次生成数据源。
This code converts any datatable to a csv-String:
此代码将任何datatable转换为csv-String:
C#
c#
public static string tableToCsv(DataTable DT)
{
System.Text.StringBuilder csv = new System.Text.StringBuilder();
System.Text.StringBuilder rowData = new System.Text.StringBuilder();
DataRow DR = null;
DataColumn DC = null;
foreach (DataColumn DC_loopVariable in DT.Columns) {
DC = DC_loopVariable;
rowData.Length = 0;
rowData.Append((rowData.Length > 0 ? ";" : "").ToString());
rowData.Append(DC.ColumnName);
}
csv.Append(rowData.ToString()).Append(Strings.Chr(13));
foreach (DataRow DR_loopVariable in DT.Rows) {
DR = DR_loopVariable;
rowData.Length = 0;
foreach (DataColumn DC_loopVariable in DT.Columns) {
DC = DC_loopVariable;
rowData.Append((rowData.Length > 0 ? ";" : "").ToString());
rowData.Append(DR[DC.ColumnName].ToString());
}
csv.Append(rowData.ToString()).Append(Strings.Chr(13));
}
return csv.ToString();
}
VB
VB
Public Shared Function tableToCsv(ByVal DT As DataTable) As String
Dim csv As New System.Text.StringBuilder
Dim rowData As New System.Text.StringBuilder
Dim DR As DataRow
Dim DC As DataColumn
For Each DC In DT.Columns
rowData.Length = 0
rowData.Append(IIf(rowData.Length > 0, ";", "").ToString)
rowData.Append(DC.ColumnName)
Next
csv.Append(rowData.ToString).Append(Chr(13))
For Each DR In DT.Rows
rowData.Length = 0
For Each DC In DT.Columns
rowData.Append(IIf(rowData.Length > 0, ";", "").ToString)
rowData.Append(DR.Item(DC.ColumnName).ToString)
Next
csv.Append(rowData.ToString).Append(Chr(13))
Next
Return csv.ToString
End Function
#3
-1
Dim dt As DataTable = ViewState("FinalState")
Dim GV As New GridView
GV.DataSource = dt ' UpdateDataTable(ViewState("FinalTable"))
GV.DataBind()
Response.Clear()
Response.Buffer = True
Response.AddHeader("content-disposition", "attachment;filename=ProspectsBank.xls")
Response.Charset = ""
Response.ContentType = "application/vnd.ms-excel"
Dim sw As New StringWriter()
Dim hw As New HtmlTextWriter(sw)
For i As Integer = 0 To GV.Rows.Count - 1
'Apply text style to each Row
GV.Rows(i).Attributes.Add("class", "textmode")
Next
GV.RenderControl(hw)
Dim style As String = "<style> .textmode { mso-number-format:\@; } </style>"
Response.Write(style)
Response.Output.Write(sw.ToString())
clsLog.SaveLogValue(Request.ServerVariables("REMOTE_ADDR"), "SearchAudienceForMagazine.aspx", "User Export Data From Our Databank in EXCEL format. Current they Exported " & dt.Rows.Count & " Data.", Session("User_TypeId"), Session("User_Name"))
Response.Flush()
Response.End()