将 Gridview 导出到 Excel:无法导出显示的正确数据
Posted
技术标签:
【中文标题】将 Gridview 导出到 Excel:无法导出显示的正确数据【英文标题】:Export Gridview to Excel: Cannot export the correct data displayed 【发布时间】:2012-05-20 02:01:47 【问题描述】:我需要将我的 gridview 数据导出到 Excel。这没有问题。 一切正常 问题是我可以将过滤器应用于网格中显示的数据。当我只想导出那些数据时(一旦应用过滤器),它会导出过滤器之前网格中先前包含的所有数据。
我希望我足够清楚......
这是我正在使用的代码:
public void ExportGridToExcel(GridView grdGridView, string fileName)
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
htmlTextWriter hw = new HtmlTextWriter(sw);
grdGridView.AllowPaging = false;
grdGridView.DataBind();
//Change the Header Row back to white color
grdGridView.HeaderRow.Style.Add("background-color", "#FFFFFF");
// Apply style to Individual Cells
grdGridView.HeaderRow.Cells[0].Style.Add("background-color", "green");
grdGridView.HeaderRow.Cells[1].Style.Add("background-color", "green");
grdGridView.HeaderRow.Cells[2].Style.Add("background-color", "green");
grdGridView.HeaderRow.Cells[3].Style.Add("background-color", "green");
grdGridView.HeaderRow.Cells[4].Style.Add("background-color", "green");
grdGridView.HeaderRow.Cells[5].Style.Add("background-color", "green");
grdGridView.HeaderRow.Cells[6].Style.Add("background-color", "green");
for (int i = 0; i < grdGridView.Rows.Count; i++)
GridViewRow row = grdGridView.Rows[i];
// Change Color back to white
row.BackColor = System.Drawing.Color.White;
// Apply text style to each Row
row.Attributes.Add("class", "textmode");
// Apply style to Individual Cells of Alternating Row
if (i % 2 != 0)
row.Cells[0].Style.Add("background-color", "#C2D69B");
row.Cells[1].Style.Add("background-color", "#C2D69B");
row.Cells[2].Style.Add("background-color", "#C2D69B");
row.Cells[3].Style.Add("background-color", "#C2D69B");
row.Cells[4].Style.Add("background-color", "#C2D69B");
row.Cells[5].Style.Add("background-color", "#C2D69B");
row.Cells[6].Style.Add("background-color", "#C2D69B");
grdGridView.RenderControl(hw);
// style to format numbers to string
string style = @"<style> .textmode mso-number-format:\@; </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
我如何应用过滤器(一切正常)
public void refreshGridFactures()
DateTime? debutFacture = null;
DateTime? FinFacture = null;
DateTime? DebutReglement = null;
DateTime? FinReglement = null;
int? NumeroFacture = 0;
if (PickerDateDebut.SelectedDate != null) debutFacture = PickerDateDebut.SelectedDate;
if (PickerDateFin.SelectedDate != null) FinFacture = PickerDateFin.SelectedDate;
if (PickerDebutReglement.SelectedDate != null) DebutReglement = PickerDebutReglement.SelectedDate;
if (PickerFinReglement.SelectedDate != null) FinReglement = PickerFinReglement.SelectedDate;
int IdEntreprise = Convert.ToInt32(ddlEntreprises.SelectedValue);
string Intitule = txtIntitule.Text;
if (txtNumeroFacture.Text != "") NumeroFacture = Convert.ToInt32(txtNumeroFacture.Text);
List<DBAccess.Facturation> listFacturation = DBAccess.DAOFacturation.GetFactures(IdEntreprise, debutFacture, FinFacture, DebutReglement, FinReglement, Intitule, NumeroFacture);
GridFactures.DataSource = listFacturation;
GridFactures.DataBind();
double total = Math.Round(listFacturation.Sum(a => a.MontantTTC), 2);
double totalHT = Math.Round(listFacturation.Sum(a => a.MontantHT), 2);
lblTotalTTC.Text = "Montant total TTC : " + total;
lblTotalHT.Text = "Montant total HT : " + totalHT;
感谢您的帮助
【问题讨论】:
实际上您正在导出为 HTML,因为grdGridView.RenderControl(hw)
将创建一个表格 ;-)
【参考方案1】:
在您的GridViewRow row = grdGridView.Rows[i];
之后添加一个检查以查看该行是否可见
if(!row.Visible) continue;
【讨论】:
以上是关于将 Gridview 导出到 Excel:无法导出显示的正确数据的主要内容,如果未能解决你的问题,请参考以下文章
把GridView1的数据导出到excel的sheet1把GridView2的数据导出到excel的sheet2.
将 dataGridView 数据导出到 excel 时出现错误
用asp.net 把GridView中的所有数据导出到Excel中没问题,根据条件查询的信息导出到Excel中 怎么整???