日期:2014-05-18 浏览次数:20485 次
protected void Button1_Click(object sender, EventArgs e)
{
s.AppendLine("<table style='width:800px' border=1>");
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox ck = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1");
if (ck.Checked)
{
//GridView1.Columns
s.AppendLine(" <tr>");
for (int j = 0; j < GridView1.Columns.Count; j++)
{
s.AppendFormat("<td>{0} </td>", ((Label)GridView1.Rows[i].Cells[j].FindControl("Label")).Text);
// 提示出错:System.NullReferenceException: 未将对象引用设置到对象的实例。
}
s.AppendLine(" </tr>");
}
}
s.Append("</table>");
Export("application/ms-excel", "excel.xls", s.ToString());
}
private void Export(string FileType, string FileName, string s)
{
Response.Charset = "UTF-8";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
Response.Write(s);
Response.End();
}
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox ck = GridView1.Rows[i].FindControl("CheckBox1") as CheckBox;
if (ck!=null&&ck.Checked)
{
s.AppendLine(" <tr>");
Label lbl=GridView1.Rows[i].FindControl("Label1") as Label;
if(lbl!=null)
{
s.AppendFormat(" <td>{0} </td>", lbl.Text);
}
lbl=GridView1.Rows[i].FindControl("Label2") as Label;
if(lbl!=null)
{
s.AppendFormat(" <td>{0} </td>", lbl.Text);
}
...
s.AppendLine(" </tr>");
}
}