利用Aspose.Cells导出excel

注意的问题

1、DataTable的处理

2、进行编码,便于中文名文件下载

3、别忘了Aspose.Cells.dll(可以自己在网上搜索)

  1. public static bool DataTableToExcel2(DataTable datatable, string filepath, out string error)
  2. {
  3. error = "";
  4. Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook();
  5.  
  6. try
  7. {
  8. if (datatable == null)
  9. {
  10. error = "DataTableToExcel:datatable 为空";
  11. return false;
  12. }
  13.  
  14. //为单元格添加样式
  15. Aspose.Cells.Style style = wb.Styles[wb.Styles.Add()];
  16. //设置居中
  17. style.HorizontalAlignment = Aspose.Cells.TextAlignmentType.Center;
  18. //设置背景颜色
  19. style.ForegroundColor = System.Drawing.Color.FromArgb(, , );
  20. style.Pattern = BackgroundType.Solid;
  21. style.Font.IsBold = true;
  22.  
  23. int rowIndex = ;
  24. for (int i = ; i < datatable.Columns.Count; i++)
  25. {
  26. DataColumn col = datatable.Columns[i];
  27. string columnName = col.Caption ?? col.ColumnName;
  28. wb.Worksheets[].Cells[rowIndex, i].PutValue(columnName);
  29. wb.Worksheets[].Cells[rowIndex, i].SetStyle(style);
  30. }
  31. rowIndex++;
  32.  
  33. foreach (DataRow row in datatable.Rows)
  34. {
  35. for (int i = ; i < datatable.Columns.Count; i++)
  36. {
  37. wb.Worksheets[].Cells[rowIndex, i].PutValue(row[i].ToString());
  38. }
  39. rowIndex++;
  40. }
  41.  
  42. for (int k = ; k < datatable.Columns.Count; k++)
  43. {
  44. wb.Worksheets[].AutoFitColumn(k, , );
  45. }
  46. wb.Worksheets[].FreezePanes(, , , datatable.Columns.Count);
  47. wb.Save(filepath);
  48. return true;
  49. }
  50. catch (Exception e)
  51. {
  52. error = error + " DataTableToExcel: " + e.Message;
  53. return false;
  54. }
  55.  
  56. }
  57.  
  58. protected void btnExport_Click(object sender, EventArgs e)
  59. {//导出
  60. int ClassID = ;
  61. int.TryParse(hidClassID.Value, out ClassID);
  62. string error = "";
  63. string filepath = "";
  64. BLL.TUser bll_TUser = new BLL.TUser();
  65. BLL.TClass bll_Class = new BLL.TClass();
  66. Model.TClass model = (new BLL.TClass()).GetModel(ClassID);
  67.  
  68. //处理DataTable
  69. DataTable dt = bll_TUser.GetListByClass(ClassID);
  70. DataTable dtNew = new DataTable();
  71. dtNew.Columns.Add("姓名", typeof(string));
  72. dtNew.Columns.Add("学号", typeof(string));
  73. dtNew.Columns.Add("性别", typeof(string));
  74. dtNew.Columns.Add("电话", typeof(string));
  75. if (dt != null && dt.Rows.Count > )
  76. {
  77. DataRow drNew = dtNew.NewRow();
  78.  
  79. foreach (DataRow dr in dt.Rows)
  80. {
  81. //drNew = dtNew.NewRow();
  82. drNew["姓名"] = dr["UserName"];
  83. drNew["学号"] = dr["IDNO"];
  84. drNew["性别"] = dr["Sex"].ToString() == "" ? "男" : (dr["Sex"].ToString() == "" ? "女" : "");
  85. drNew["电话"] = dr["Phone"];
  86. dtNew.Rows.Add(drNew.ItemArray);
  87. }
  88. }
  89.  
  90. if (model != null)
  91. {
  92. filepath = "/UploadFiles/ExportClass/";// + model.ClassName + ".xlsx";
  93. string filaname = model.ClassName + ".xlsx";
  94. string finalPath = MapPath("~" + filepath + filaname);
  95. //检查有该路径是否就创建
  96. if (!Directory.Exists(MapPath("~/UploadFiles/ExportClass/")))
  97. {
  98. Directory.CreateDirectory(MapPath("~/UploadFiles/ExportClass/"));
  99. }
  100. if (DataTableToExcel2(dtNew, finalPath, out error))
  101. {
  102. string SiteRoot = "http://" + Request.Url.Authority.ToString() + filepath + Uri.EscapeDataString(filaname); //进行编码,便于中文名文件下载
  103. //下载excel
  104. ClientScript.RegisterStartupScript(this.GetType(), "", ",<script type='text/javascript'>window.open('" + SiteRoot + "');</script>");
  105. }
  106. else
  107. {
  108. ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('提示', '" + error + "!');</script>");
  109. }
  110. }
  111. else
  112. {
  113. ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('提示', '班级不存在!');</script>");
  114. }
  115. }

Aspose.Cells导出Excel(1)的更多相关文章

  1. C#使用Aspose.Cells导出Excel简单实现

    首先,需要添加引用Aspose.Cells.dll,官网下载地址:http://downloads.aspose.com/cells/net 将DataTable导出Xlsx格式的文件下载(网页输出) ...

  2. Aspose.Cells导出Excel(2)

    DataTable dtTitle = ds.Tables[]; DataTable dtDetail = ds.Tables[]; int columns = dtTitle.Columns.Cou ...

  3. C#+Aspose.Cells 导出Excel及设置样式 (Webform/Winform)

    在项目中用到,特此记录下来,Aspose.Cells 不依赖机器装没有装EXCEL都可以导出,很方便.具体可以参考其他 http://www.aspose.com/docs/display/cells ...

  4. C# 使用Aspose.Cells 导出Excel

    今天在工作中碰到同事用了一种新型的方式导入excel,在此做个学习记录. 插件:Aspose.Cells 第一步:准备好导出的模板,例子: C#代码: #region 验证数据 if (model = ...

  5. aspose.Cells 导出Excel

    aspose aspse.Cells可以操作Excel,且不依赖于系统环境. 使用模板,通过绑定输出数据源 这种适合于对格式没有特别要求的,直接绑定数据源即可.和数据绑定控件差不多. Workbook ...

  6. Aspose.Cells 导出 excel

    Aspose.Cells.Workbook book = new Aspose.Cells.Workbook(); Aspose.Cells.Worksheet sheet = book.Worksh ...

  7. 使用Aspose.Cells读取Excel

      最新更新请访问: http://denghejun.github.io Aspose.Cells读取Excel非常方便,以下是一个简单的实现读取和导出Excel的操作类: 以下是Aspose.Ce ...

  8. 报表中的Excel操作之Aspose.Cells(Excel模板)

    原文:报表中的Excel操作之Aspose.Cells(Excel模板) 本篇中将简单记录下Aspose.Cells这个强大的Excel操作组件.这个组件的强大之处,就不多说,对于我们的报表总是会有导 ...

  9. 怎么使用Aspose.Cells读取excel 转化为Datatable

    说明:vs2012 asp.net mvc4 c# 使用Aspose.Cells 读取Excel 转化为Datatable 1.HTML前端代码 <%@ Page Language=" ...

随机推荐

  1. 【Machine Learning】机器学习及其基础概念简介

    机器学习及其基础概念简介 作者:白宁超 2016年12月23日21:24:51 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现的深入理解.本系列文章是作者结 ...

  2. WebGIS项目中利用mysql控制点库进行千万条数据坐标转换时的分表分区优化方案

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1. 背景 项目中有1000万条历史案卷,为某地方坐标系数据,我们的真实 ...

  3. 开发者最爱的Firebug停止更新和维护

        近日,Firebug团队在其官网上宣布,Firebug将不再继续开发和维护,并邀请大家使用Firefox的内置开发工具.     Firebug最初是2006年1月由Joe Hewitt编写, ...

  4. 数塔问题(DP算法)自底向上计算最大值

    Input 输入数据首先包括一个整数C,表示测试实例的个数,每个测试实例的第一行是一个整数N(1 <= N <= 100),表示数塔的高度,接下来用N行数字表示数塔,其中第i行有个i个整数 ...

  5. CSS学习笔记

    CSS学习笔记 2016年12月15日整理 CSS基础 Chapter1 在console输入escape("宋体") ENTER 就会出现unicode编码 显示"%u ...

  6. CSS三个定位——常规、浮动、绝对定位

    .dage { width: 868px; background: #5B8C75; border: 10px solid #A08C5A; margin-top: -125px; margin-le ...

  7. SharePoint 2013管理中心里【管理服务器上的服务】不见了

    打开管理中心,准备配置Managed Metadata Service,发现"管理服务器上的服务"不见了 那我自己拼url直接访问:http://xxxx/_admin/Serve ...

  8. 【centos7常用技巧】RPM打包

    一.RPM打包的目的 1.当目标机中不存在编译环境时,可以先在本地环境中编译打包,然后直接在目标机中用rpm -ivh *.rpm安装即可. 2.当需要在目标机中安装多个软件或者增加多个文件时,可以将 ...

  9. Merge Sorted Array

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...

  10. ASP.NET MVC 使用 Petapoco 微型ORM框架+NpgSql驱动连接 PostgreSQL数据库

    前段时间在园子里看到了小蝶惊鸿 发布的有关绿色版的Linux.NET——“Jws.Mono”.由于我对.Net程序跑在Linux上非常感兴趣,自己也看了一些有关mono的资料,但是一直没有时间抽出时间 ...