C# dt导出excel 生成多个sheet!
小米粥 2013-02-19 02:55:47 问题是这样的。我从数据库里查询到几条数据,返回一个datatable。通过循行,生成一个<table></table>的html格式的字符串。
然后通过下面:
System.Web.HttpContext curContext = System.Web.HttpContext.Current;
StringWriter sw = new StringWriter();
sw.WriteLine("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
sw.WriteLine("<head>");
sw.WriteLine("<!--[if gte mso 9]>");
sw.WriteLine("<xml>");
sw.WriteLine(" <x:ExcelWorkbook>");
sw.WriteLine(" <x:ExcelWorksheets>");
sw.WriteLine(" <x:ExcelWorksheet>");
sw.WriteLine(" <x:Name>数据报表</x:Name>");
sw.WriteLine(" <x:WorksheetOptions>");
sw.WriteLine(" <x:Print>");
sw.WriteLine(" <x:ValidPrinterInfo />");
sw.WriteLine(" </x:Print>");
sw.WriteLine(" </x:WorksheetOptions>");
sw.WriteLine(" </x:ExcelWorksheet>");
sw.WriteLine(" </x:ExcelWorksheets>");
sw.WriteLine("</x:ExcelWorkbook>");
sw.WriteLine("</xml>");
sw.WriteLine("<![endif]-->");
sw.WriteLine("</head>");
sw.WriteLine("<body>");
sw.WriteLine(sb.ToString());
sw.WriteLine("</body>");
sw.WriteLine("</html>");
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.AppendHeader("content-disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, Encoding.UTF8).ToString() + ".xls");
curContext.Response.Write("<meta http-equiv=Content-Type content=text/html;charset=gb2312>");
curContext.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
// 返回客户端
curContext.Response.Write(sw);
sw.Close();
sb 就是上面拼接的字符串变量。
现在想实现一条数据生成一个sheet,就是一个excel包含多个sheet。该如何在此基础上修改?