asp.net 导出excel文件
asp.net 导出excel文件
以下是代码片段:
public void SettleOrderToExcel(HttpResponse resp, string fileName, string columnNames, IList mSOrder)
{
//设置编码和附件格式
resp.AddHeader("content-disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + ".xls");
resp.ContentType = "application nd.ms-excel";
resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//这里如果写成UTF-8会出现中文乱码
resp.Charset = "GB2312";
string colHeaders = "", ls_item = "";
int count = 0;
IList liCol = GetCloumnNames(columnNames);
//在当前行中,逐列获得数据,数据之间以\t分割,结束时加回车符\n
foreach (MixedLot_SettleOrder msoItem in mSOrder)
{
foreach (ColumnOrName cn in liCol)
{
if (count == 0)
{ colHeaders += cn.ColumnText + "\t"; }
switch (cn.ColumnValue)
{
case "Mso_ID": ls_item += msoItem.Mso_ID + "\t"; break;
case "Mu_ID": ls_item += msoItem.Mu_ID + "\t"; break;
case "Mu_UserName": ls_item += msoItem.Mu_UserName + "\t"; break;
case "Mu_ERPUserID": ls_item += msoItem.Mu_ERPUserID + "\t"; break;
case "Mu_B2CUserID": ls_item += msoItem.Mu_B2CUserID + "\t"; break;
case "Mu_CreditLimit": ls_item += msoItem.Mu_CreditLimit + "\t"; break;
case "Mu_Bnlance": ls_item += msoItem.Mu_Bnlance + "\t"; break;
case "Mu_LettlementType": ls_item += msoItem.Mu_LettlementType + "\t"; break;
case "Msl_ID": ls_item += msoItem.Msl_ID + "\t"; break;
case "Mso_OrderID": ls_item += msoItem.Mso_OrderID + "\t"; break;
case "Mso_OrderTotal": ls_item += msoItem.Mso_OrderTotal + "\t"; break;
case "Mso_PayState": ls_item += LSI.Settle.GetPayState(msoItem.Mso_PayState.ToString()) + "\t"; break;
case "Mso_PayTime": ls_item += msoItem.Mso_PayTime + "\t"; break;
case "Mso_OperateUserID": ls_item += msoItem.Mso_OperateUserID + "\t"; break;
case "Mso_OperateTime": ls_item += msoItem.Mso_OperateTime + "\t"; break;
case "Mso_Note": ls_item += msoItem.Mso_Note + "\t"; break;
case "Mso_AdjustMoney": ls_item += msoItem.Mso_AdjustMoney + "\t"; break;
default: break;
}
}
ls_item += "\n";
if (count == 0) { colHeaders += "\n"; }
count++;
}
//向HTTP输出流中写入取得的数据信息
resp.Write(colHeaders);
//当前行数据写入HTTP输出流,并且置空ls_item以便下行数据
resp.Write(ls_item);
//写缓冲区中的数据到HTTP头文件中
resp.End();
}
最后需要注意的是使用此方法是不能通过异步调用的
相关文章
关键词:asp.net,Excel,.NET,微软技术
责任编辑:失落的羽毛