public void DataBindTitleExcel(Page pPage, DataTable dt, string ExcelTitle, string strUserMsg)
{
HttpResponse response = pPage.Response;
if (dt.Rows.Count == 0)
{
response.Write( "<script>alert('对不起,没有可用于导出的数据!')</script>");
response.End();
}
response.ContentEncoding = Encoding.GetEncoding( "GB2312");
response.ContentType = "application/ms-excel";
response.AppendHeader( "Content-Disposition", "attachment;filename=Export.xls");
int count = dt.Columns.Count;
StringBuilder builder = new StringBuilder();
builder.Append( "<html><head>\n");
builder.Append( "<meta http-equiv=\"Content-Language\" content=\"zh-cn\">\n");
builder.Append( "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">\n");
builder.Append( "</head>\n");
builder.Append( "<table border=1>");
if (ExcelTitle != "")
{
string str = "<font size=4><b>" + ExcelTitle + "</b></font>";
if (strUserMsg != "")
{
str = str + "(" + strUserMsg + ")";
}
builder.Append( string.Concat( new object[] { "<tr><td colspan=", count, ">", str, "</td></tr>" }));
}
builder.Append( "<tr><td colspan=" + count + " valign=middle height=24>");
builder.Append( "查询时间:" + DateTime.Now.ToString( "G") + "</td></tr>");
builder.Append( "<tr>\n");
for ( int i = 0; i < count; i++)
{
if (dt.Columns[i].Caption.ToString().ToLower() != "id")
{
builder.Append( "<td bgcolor=#CCFFCC><b>" + dt.Columns[i].Caption.ToString() + "</b></td>\n");
}
}
foreach (DataRow row in dt.Rows)
{
builder.Append( "<tr>");
for ( int j = 0; j < count; j++)
{
if (dt.Columns[j].Caption.ToString().ToLower() != "id")
{
builder.Append( "<td style='vnd.ms-excel.numberformat:@'>" + row[j].ToString() + "</td>");
}
}
builder.Append( "</tr>\n");
}
builder.Append( "</table>\n");
response.Write(builder.ToString());
response.End();
}