这是我在写程序的时候用到的方法,只是为了个人记录。
private void OutExecl(string strwhere)
{
string sql = "select * from db_table";
sql+="where "+strwhere;
sql+="order by ID desc";
DataSet ds = D_Common.DbHelperOleDb.Query(sql);//数据库操作方法
HttpResponse resp;
resp = Page.Response;
resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
resp.ContentType = "application/ms-excel";
resp.AddHeader("Content-Disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode("yymmddhhmmss", System.Text.Encoding.UTF8) + ".xls");
this.EnableViewState = false;
string colHeaders = "", Is_item = "";
int i = 0;
colHeaders += "1" + "\t";//“1”代表列表的头名称
colHeaders += "2" + "\t";
colHeaders += "3" + "\t";
colHeaders += "4" + "\t";
colHeaders += "4" + "\t\n";
resp.Write(colHeaders);
foreach (DataRow row in ds.Tables[0].Rows)
{
//在当前行中,逐列取得数据,数据之间以\t分割,结束时加回车符\n
for (i = 0; i < ds.Tables[0].Columns.Count; i++)
{
Is_item += row[i].ToString() + "\t";
}
Is_item += "\n";
resp.Write(Is_item);
Is_item = "";
}
Response.End();
}