C# 将数据表导出都EXCEL 中
将数据表中的数据倒出来,导出到EXCEL中,这里介绍一个方法,前台一个button按钮,点击生成EXCEL表;一个table用于呈现数据,这里就不写前台的html代码了,直接写后台的方法
/// <summary>
/// 导出Excel button点击
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button1_Click(object sender, EventArgs e)
{
//导出报表
Response.Clear();
Response.Buffer = true;
Response.Charset = "utf-8";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.DateTime.Now.ToString("yyyyMMdd_hhmm") + ".xls");//以时间命名
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");//设置输出流为简体中文
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
this.EnableViewState = false;
string str = OutReport();//生成数据表
Response.Write(str.Replace(" ", ""));//将生成的数据表写入输出流
Response.End();
}
private string OutReport()
{
DTMODEL.Bm.bm bmmodel = new DTMODEL.Bm.bm();
Bm_Dal bm_dal = new Bm_Dal();
if (this.cxName.Value != "")
{
bmmodel.Name = this.cxName.Value;
}
else
{
bmmodel.Name = "";
}
if (this.cxPhone.Value != "")
{
bmmodel.Phone = this.cxPhone.Value;
}
else
}
bmmodel.Phone = "";
}
DataTable dt = bm_dal.Bm_GetList1(bmmodel);//要导出的数据表
StringBuilder strb = new StringBuilder();
strb.Append("<table width='700' border='0' align='center' cellpadding='0' cellspacing='2'>");
strb.Append(" <tr>");
strb.Append(" <td height='25' align='center' style='font-size:14px;font-weight:bold;text-align:center;'>超级运动会报名表</td>");//表头
strb.Append(" </tr>");
strb.Append(" <tr>");
strb.Append(" <td><table width='100%' border='0' cellspacing='2' cellpadding='0'>");
strb.Append(" <tr>");
strb.Append(" <td><table width='100%' border='1' cellpadding='0' cellspacing='0' bordercolor='#000000'>");
strb.Append(" <tr>");
strb.Append(" <td rowspan='2' align='center'>姓名</td>");//列名
strb.Append(" <td rowspan='2' align='center'>性别</td>");
strb.Append(" <td rowspan='2' align='center'>年龄</td>");
strb.Append(" <td rowspan='2' align='center'>职业<br />");
strb.Append(" <td rowspan='2' align='center'>联系电话</td>");
strb.Append(" <td rowspan='2' align='center'>家庭住址</td>");
strb.Append(" <td rowspan='2' align='center'>才艺与特长</td>");
strb.Append(" <td rowspan='2' align='center'>加油口号<br />");
strb.Append(" <td rowspan='2' align='center'>出场方式<br />");
strb.Append(" <td rowspan='2' align='center'>秀出你的故事<br />");
strb.Append(" <td rowspan='2' align='center'>报名日期<br />");
strb.Append(" </tr>");
strb.Append(" <tr>");
foreach (DataRow dr in dt.Rows)//填充数据表
{
strb.Append(" <tr>");
strb.Append(" <td style='color:#FF0000' height='20'> " + dr["Name"].ToString()+ "</td>");
strb.Append(" <td style='color:#FF0000'> " + dr["Sex"].ToString() + "</td>");
strb.Append(" <td style='color:#FF0000'> " + dr["Age"].ToString() + "</td>");
strb.Append(" <td style='color:#FF0000'> " + dr["Work"].ToString() + "</td>");
strb.Append(" <td style='color:#FF0000'> " + dr["Phone"].ToString() + "</td>");
strb.Append(" <td style='color:#FF0000'> " + dr["Address"].ToString() + "</td>");
strb.Append(" <td style='color:#FF0000'> " + dr["Tcycy"].ToString() + "</td>");
strb.Append(" <td style='color:#FF0000'> " + dr["Jykh"].ToString() + "</td>");
strb.Append(" <td style='color:#FF0000'> " + dr["Ccfs"].ToString() + "</td>");
strb.Append(" <td style='color:#FF0000'> " + dr["Jj"].ToString() + "</td>");
strb.Append(" <td style='color:#FF0000'> " + dr["Bmrq"].ToString()+ "</td>");
strb.Append(" </tr>");
}
strb.Append(" </table></td>");
strb.Append(" </tr>");
strb.Append("</table>");
return strb.ToString();
// myReport.InnerHtml = strb.ToString();
}
呵呵 是不是很简单呢?最后贴张截图