C#和ASP.NET中怎样导出EXCEL不同版本(如下只能导出.XLS)我想可以选择导出.XLS和.XLSX,求高手指教!

ds=bll_sales.sp_dow(id,jop,mat);StringWritersw=newStringWriter();DataTableda=ds.Table... ds = bll_sales.sp_dow(id,jop,mat);
StringWriter sw = new StringWriter();
DataTable da = ds.Tables[0];
if (da.Rows.Count > 0)
{
sw.WriteLine("姓名\t性别\t年龄\t语文\t数学\t化学\t物理\t英语\t地理\t生物");
}
foreach (DataRow rrow in da.Rows)
{
sw.WriteLine(rrow["cus001"].ToString().Trim() + "\t" + rrow["mat001"].ToString().Trim() + "\t" + rrow["BQ03"].ToString().Trim() + "\t" + rrow["mat009"].ToString().Trim() + "\t" + rrow["dsrt4"].ToString().Trim() + "\t" + rrow["Sal04"].ToString().Trim() + "\t" + rrow["Sal05"].ToString().Trim() + "\t" + rrow["Sal06"].ToString().Trim() + "\t" + rrow["Sal07"].ToString().Trim() + "\t" + rrow["Sal08"].ToString().Trim());
}

HtmlTextWriter hw = new HtmlTextWriter(sw);
sw.Close();

Response.AppendHeader("content-disposition", "attachment;filename=\"" + System.Web.HttpUtility.UrlEncode("人员信息", System.Text.Encoding.UTF8) + ".xls");
Response.ContentType = "application/ms-excel";
Response.ContentEncoding = System.Text.Encoding.Default;
Response.Write(sw);
Response.End();
展开
 我来答
lxg13131313
2012-04-16 · 超过17用户采纳过TA的回答
知道答主
回答量:78
采纳率:0%
帮助的人:36.3万
展开全部
public static void DownData(DataView dvData, string fileName)
{
StringBuilder strBd = new StringBuilder(EXCELHTMLHEAD + "<table x:str><tr>");
int cols = dvData.Table.Columns.Count;
for (int j = 0; j < cols; j++)
strBd.Append("<td>" + dvData.Table.Columns[j].Caption + "</td>");
strBd.Append("</tr>");

for (int i = 0; i < dvData.Count; i++)
{
strBd.Append("<tr>");
for (int j = 0; j < cols; j++)
strBd.Append("<td>" + Lixiang.Common.HtmlToTxt(Convert.ToString(dvData[i][j])) + "</td>");
strBd.Append("</tr>");
}
strBd.Append("</table></body></html>");

DownData(strBd.ToString(), fileName);
}

public const string EXCELHTMLHEAD = @"<html xmlns:o='urn:schemas-microsoft-com:office:office'
xmlns:x='urn:schemas-microsoft-com:office:excel' xmlns='http://www.w3.org/TR/REC-html40'>
<head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet>
<x:Name>ExportData</x:Name><x:WorksheetOptions><x:Selected/></x:WorksheetOptions>
</x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]-->
</head><body>";
追问
能不能按照我的那种方法改下呢?
追答
public static void DownData(string strData, string fileName)
{
DownData(strData, fileName + ".xls", null);
}

public static void DownData(string strData, string fileName, Encoding code)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
HttpContext.Current.Response.ContentEncoding = (code == null ? Encoding.UTF8 : code);
HttpContext.Current.Response.AddHeader("Content-disposition", "attachment; filename="
+ HttpUtility.UrlEncode(fileName));
HttpContext.Current.Response.Write(strData);
HttpContext.Current.Response.End();
}

您那方法导出的其实是用tab分格的文本,不同版本的Excel都是认的,只要高兴,改个扩展名好了,但是有时又会有些问题,比如说有类似这样的编号12-1就会被识别成日期,身份证号会变成数字等
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式