日期:2014-05-17  浏览次数:20989 次

asp.net 导出excel 方法二

if (dtList == null || dtList.Rows.Count <= 0)// 把查询出来的数据放到dtlist (DataTable)里面
??????????? {
??????????????? return;
??????????? }
??????????? DataTable dt = dtList;
??????????? if (dt == null || dt.Rows.Count < 0)
??????????? {
??????????????? return;
??????????? }

?

//以上操作是没数据就不执行

??????????? StringBuilder sb = new StringBuilder();

??????????? sb.Append("<meta http-equiv='Content-Type' content='text/html; charset=gb2312' />");
??????????? sb.Append("<table width='100%' border='1'><tr style='font-weight:bold;'>");
??????????? sb.Append("<td align='center'>序号</td>");
??????????? sb.Append("<td align='center'>项目部</td>");
??????????? sb.Append("<td align='center'>监控年月</td>");
??????????? sb.Append("<td align='center'>项目总监</td>");
??????????? sb.Append("<td align='center'>本月监控项数</td>");
??????????? sb.Append("<td align='center'>本月不符合项数</td>");
??????????? sb.Append("<td align='center'>本月整改完成项数</td>");
??????????? sb.Append("<td align='center'>不符合事项描述</td></tr>");

?

//以上就是打印表头

?

?

??????????? double byjkxs=0.00;//这是定义几个总计要计算的变量
??????????? double bybfhxs = 0.00;
??????????? double byzgxs = 0.00;
??????????? for (int i = 0; i < dt.Rows.Count; i++)
??????????? {

//变量累加
??????????????? byjkxs += Convert.ToDouble(dt.Rows[i]["byjkxs"]);
??????????????? bybfhxs += Convert.ToDouble(dt.Rows[i]["bybfhxs"]);
??????????????? byzgxs += Convert.ToDouble(dt.Rows[i]["byzgxs"]);

?

//打印数据

??????????????? sb.Append("<tr>");
??????????????? sb.Append("<td ' align='center'>").Append((i + 1).ToString()).Append("</td>");
??????????????? sb.Append("<td? align='left'>").Append(dt.Rows[i]["NodeName"].ToString()).Append("</td>");
??????????????? sb.Append("<td? align='left'>").Append(dt.Rows[i]["jkny"].ToString().Replace("-", "—")).Append("</td>");
??????????????? sb.Append("<td? align='left'>").Append(dt.Rows[i]["xmzjname"].ToString()).Append("</td>");
??????????????? sb.Append("<td? align='left'>").Append(dt.Rows[i]["byjkxs"].ToString()).Append("</td>");
??????????????? sb.Append("<td? align='left'>").Append(dt.Rows[i]["bybfhxs"].ToString()).Append("</td>");
??????????????? sb.Append("<td? align='left'>").Append(dt.Rows[i]["byzgxs"].ToString()).Append("</td>");
??????????????? sb.Append("<td? align='left'>").Append(dt.Rows[i]["bfhsxmc"].ToString()).Append("</td>");
??????????????? sb.Append("</tr>");
??????????? }

//最后对后列 把总计给打印出来
??????????? sb.Append("<tr>");
??????????? sb.Append("<td ' align='center'>").Append("总计:").Append("</td>");
??????????? sb.Append("<td? align='left'>").Append("").Append("</td>");
??????????? sb.Append("<td? align='left'>").Append("").Append("</td>");
??????????? sb.Append("<td? align='left'>").Append("").Append("</td>");
??????????? sb.Append("<td? align='left'>").Append(byjkxs).Append("</td>");
??????????? sb.Append("<td? align='left'>").Append(bybfhxs).Append("</td>");
??????????? sb.Append("<td? align='left'>").Append(byzgxs).Append("</td>");
??????????? sb.Append("<td? align='left'>").Append("").Append("</td>");
??????????? sb.Append("</tr>");

??????????? sb.Append("</table>");
??????????? string strfilename = Guid.NewGuid() + ".xls";
??????????? FileStream fs = new FileStream(Path.Combine(xtPM.AttachmentManager.AttachmentDirectory, strfilename), FileMode.Create, FileAccess.Write);//参数:路径,创建,写
??????????? StreamWriter sw = new StreamWriter(fs, Encoding.Default);
??????????? sw.Write(sb);
???????????