急救!gridview自定义表头不能导出为excel?
我做了一个页面。用一个gridview显示一张datatable的内容,并可另存为excel。由于需要,自定义了gridview的表头,在网页上显示是正常的。但将gridview导出为excel时,excel的表头却还是datatable的表头。请问怎么回事?相关代码如下:protected void Button3_Click(object sender, EventArgs e)
{
DataSet myds = new DataSet();
DataTable mytb = new DataTable();
mytb.Columns.Add("1",typeof(string));
mytb.Columns.Add("2", typeof(string));
mytb.Columns.Add("3", typeof(string));
mytb.Columns.Add("4", typeof(string));
mytb.Columns.Add("5", typeof(string));
mytb.Columns.Add("6", typeof(string));
mytb.Columns.Add("7", typeof(string));
mytb.Columns.Add("8", typeof(string));
DataRow row = mytb.NewRow();
row["1"] = "姓名";
row["2"] = "早";
row["3"] = "午";
row["4"] = "晚";
row["5"] = "早";
row["6"] = "午";
row["7"] = "晚";
row["8"] = "合计";
mytb.Rows.Add(row);
myds.Tables.Add(mytb);
GridView1.DataSource = mytb;
GridView1.DataBind();
GridView1.CellSpacing = 1;
GridView1.HeaderRow.Cells.Clear();
//这里自定义gridview表头,但是保存到excel还是原表样子。
GridView1.HeaderRow.Cells.Clear();
TableCell cell=new TableCell();
cell.Text = "</td><td colspan='3'>场景一</td><td colspan='3'>场景二</td><td>";
GridView1.HeaderRow.Cells.Add(cell);
}
public override void VerifyRenderingInServerForm(Control control) { }//加入这个,不然导出excel出错。
protected void Button4_Click(object sender, EventArgs e)//导出excel
{
System.Web.HttpContext curContext = System.Web.HttpContext.Current;
System.IO.StringWriter strWriter = null;
System.Web.UI.HtmlTextWriter htmlWriter = null;
Response.AddHeader("content-disposition", "attachment; filename=baobiao.xls");
curContext.Response.ContentType = "application/vnd.ms-excel"; curContext.Response.ContentEncoding = System.Text.Encoding.UTF7;
strWriter = new System.IO.StringWriter();
htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
GridView1.RenderControl(htmlWriter);
curContext.Response.Write(strWriter.ToString());
curContext.Response.End();
} --------------------编程问答-------------------- up --------------------编程问答-------------------- 帮顶了 --------------------编程问答-------------------- 顶一下 --------------------编程问答-------------------- 你看gridview的属性呢,试试看吧, --------------------编程问答-------------------- lz 改變的只是GridView的表頭名稱,而不是DataTable的ColumnName,
所以匯出的Excel的表頭沒有變化,建議lz修改DataTable的ColumnName,
這樣就可以了 --------------------编程问答-------------------- 这段放到绑定的前面试一下.
//这里自定义gridview表头,但是保存到excel还是原表样子。--------------------编程问答-------------------- 当然可以。我上周刚做了个例子 --------------------编程问答-------------------- to5楼:因为我的表头有合并单元格的,datatable里面没有找到相关的属性去设置这个...
GridView1.HeaderRow.Cells.Clear();
TableCell cell=new TableCell();
cell.Text = " </td> <td colspan='3'> 场景一 </td> <td colspan='3'> 场景二 </td> <td> ";
GridView1.HeaderRow.Cells.Add(cell);
to6楼:不行,如果没绑定数据,那样改表头会出错。最后显示的还是datatable的样子。
to7楼:能不能详细说说?
谢谢帮顶的弟兄们。 --------------------编程问答-------------------- protected void Button1_Click(object sender, EventArgs e)
{
if (GridView1.Rows.Count < 1)
{
Label5.Text = "没有数据!";
return;
}
StringWriter sw = new StringWriter();
sw.WriteLine("\t\t\t\t\t\t\t\t" + Label3.Text + "年度小区取暖费明细表");
sw.WriteLine("热源: " + "\t" + Label3.Text + "\t\t" + "单位性质:" + "\t" + Label2.Text + "\t\t\t\t\t\t\t\t\t" + "截止日期:" + "\t" + Label6.Text);
sw.WriteLine("\t\t\t\t\t\t" + "总体情况" + "\t\t\t" + "实供" + "\t\t\t" + "未用热" + "\t\t\t" + "实收" + "\t\t\t" + "欠费");
sw.WriteLine("序号" + "\t" + "热源" + "\t" + "热力站" + "\t" + "小区名称" + "\t" + "用户性质" + "\t" + "户数"
+ "\t" + "面积(m²)" + "\t" + "总额(元)" + "\t" + "户数" + "\t" + "面积(m²)" + "\t" + "总额(元)" + "\t" + "户数"
+ "\t" + "面积(m²)" + "\t" + "总额(元)" + "\t" + "户数" + "\t" + "面积(m²)" + "\t" + "总额(元)" + "\t"
+ "户数" + "\t" + "面积(m²)" + "\t" + "总额(元)" + "\t" + "备注");
for (int i = 0; i < GridView1.Rows.Count; i++)
{
string strInfo = "";
for (int j = 0; j < GridView1.Rows[i].Cells.Count; j++)
{
if (GridView1.Rows[i].Cells[j].Text == " ")
{
GridView1.Rows[i].Cells[j].Text = "";
}
if (j != 0)
{
strInfo = strInfo + "\t" + GridView1.Rows[i].Cells[j].Text;
}
else
{
strInfo = GridView1.Rows[i].Cells[j].Text;
}
}
sw.WriteLine(strInfo);
}
sw.WriteLine("制表时间: " + "\t" + LabelTime.Text + "\t\t" + "制表人: " + "\t" + LabelMaker.Text + "\t\t\t\t\t\t\t\t" + "审核人: " + "\t\t\t" + "负责人: ");
sw.Close();
Response.AddHeader("Content-Disposition", "attachment; filename=" + Label1.Text + "XQMXList.xls");
Response.ContentType = "application/ms-excel";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.Write(sw);
Response.End();
} --------------------编程问答-------------------- UP --------------------编程问答-------------------- DataTable的ColumnName可以修改,但是如何合并单元格呢?
补充:.NET技术 , ASP.NET