菜鸟求助web导出到excel的问题
我从网上找了个例子
public static string ExportTable(DataSet ds)
{
string data = "";
//data = ds.DataSetName + "\n";
foreach (DataTable tb in ds.Tables)
{
//data += tb.TableName + "\n";
data += "<table cellspacing=\"0\" cellpadding=\"5\" rules=\"all\" border=\"1\">";
//写出列名
data += "<tr style=\"font-weight: bold; white-space: nowrap;\">";
foreach (DataColumn column in tb.Columns)
{
data += "<td>" + column.ColumnName + "</td>";
}
data += "</tr>";
//写出数据
foreach (DataRow row in tb.Rows)
{
data += "<tr>";
foreach (DataColumn column in tb.Columns)
{
if (column.ColumnName.Equals("证件编号") || column.ColumnName.Equals("报名编号"))
data += "<td style=\"vnd.ms-excel.numberformat:@\">" + row[column].ToString() + "</td>";
else
data += "<td>" + row[column].ToString() + "</td>";
}
data += "</tr>";
}
data += "</table>";
}
return data;
}
public static void ExportDsToXls(Page page, string sql)
{
ExportDsToXls(page, "FileName", sql);
}
public static void ExportDsToXls(Page page, string fileName, string sql)
{
//DataSet ds = ol.GetDataSet(sql);
DataSet ds = OleDbDB.ReturnDataSet(sql);
if (ds != null) ExportDsToXls(page, fileName, ds);
}
public static void ExportDsToXls(Page page, DataSet ds)
{
ExportDsToXls(page, "FileName", ds);
}
public static void ExportDsToXls(Page page, string fileName, DataSet ds)
{
page.Response.Clear();
page.Response.Buffer = true;
page.Response.Charset = "GB2312";
//page.Response.Charset = "UTF-8";
page.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName + System.DateTime.Now.ToString("_yyMMdd_hhmm") + ".xls");
page.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
page.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
page.EnableViewState = false;
page.Response.Write(ExportTable(ds));
page.Response.End();
}
然后调用的时候
_sql ='...'
excel.ExportDsToXls(this.Page, "excel", _sql);
为什么什么反应也没有...
------解决方案--------------------没数据的方法你加上空格啊
------解决方案--------------------