导出数据到Excel通用的方法类,请应对需求自行修改。

资源下载列表

using System.Data;
using System.IO; namespace IM.Common.Tools
{
public class Export
{
public string Encoding = "UTF-8";
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response; public void EcportExcel(DataTable dt, string fileName)
{ if (dt != null)
{
StringWriter sw = new StringWriter();
CreateStringWriter(dt, ref sw);
sw.Close();
response.Clear();
response.Buffer = true;
response.Charset = Encoding;
//this.EnableViewState = false;
response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ".xls");
response.ContentType = "application/ms-excel"; //response.ContentEncoding = System.Text.Encoding.GetEncoding(Encoding)
response.ContentEncoding = System.Text.Encoding.UTF8;
response.Write(sw);
response.End();
} } private void CreateStringWriter(DataTable dt, ref StringWriter sw)
{
string sheetName = "sheetName"; 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>" + sheetName + "</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("<table>");
sw.WriteLine(" <tr>");
string[] columnArr = new string[dt.Columns.Count];
int i = ;
foreach (DataColumn columns in dt.Columns)
{ sw.WriteLine(" <td>" + columns.ColumnName + "</td>");
columnArr[i] = columns.ColumnName;
i++;
}
sw.WriteLine(" </tr>"); foreach (DataRow dr in dt.Rows)
{
sw.WriteLine(" <tr>");
foreach (string columnName in columnArr)
{
sw.WriteLine(" <td style='vnd.ms-excel.numberformat:@'>" + dr[columnName] + "</td>");
}
sw.WriteLine(" </tr>");
}
sw.WriteLine("</table>");
sw.WriteLine("</body>");
sw.WriteLine("</html>");
}
}
}

最新文章

  1. JAVA Thread线程异常监控
  2. Fedora javac 命令提示 [javac: 未找到命令...]
  3. Android 友盟分享躺过的几个坑,大坑,坑爹啊
  4. html textarea 获取换行
  5. 关于java.lang.reflect.InvocationTargetException
  6. c++随机数生成
  7. 李洪强漫谈iOS开发[C语言-006]-程序的描述方式
  8. MultiSet
  9. go与json
  10. c - 计算1到20的阶乘
  11. cp命令的实现
  12. Git 忽略特殊文件的功能
  13. UWP--页面传值
  14. Spring MVC 项目搭建 -2- 添加service ,dao,junit
  15. Activiti常见问题解决
  16. Jmeter + Ant 测试环境搭建 及解决问题: the &lt;jmeter&gt; type doesn&#39;t support nested text data
  17. Linux lvs-NAT模式配置详解
  18. SQL Server - 四种排序, ROW_NUMBER() /RANK() /DENSE_RANK() /ntile() over()
  19. Android获取手机号码
  20. .net core 生成

热门文章

  1. UITabBarController自定义一
  2. 微信小应用vs progressive-web-apps
  3. 限制窗口拉伸范围——WM_GETMINMAXINFO
  4. SGU 147.Black-white king
  5. 78 Subsets(求子集Medium)
  6. websphere内存溢出,手动导出was的phd和javacore文件
  7. jquery easy ui 学习 (6) basic validatebox
  8. 禁用ios7 手势滑动返回功能
  9. get set
  10. JAVA抽象类,接口,多态,抽象方法,一次列举