Aspose.Cells导出Excel(1)
利用Aspose.Cells导出excel
注意的问题
1、DataTable的处理
2、进行编码,便于中文名文件下载
3、别忘了Aspose.Cells.dll(可以自己在网上搜索)
- public static bool DataTableToExcel2(DataTable datatable, string filepath, out string error)
- {
- error = "";
- Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook();
- try
- {
- if (datatable == null)
- {
- error = "DataTableToExcel:datatable 为空";
- return false;
- }
- //为单元格添加样式
- Aspose.Cells.Style style = wb.Styles[wb.Styles.Add()];
- //设置居中
- style.HorizontalAlignment = Aspose.Cells.TextAlignmentType.Center;
- //设置背景颜色
- style.ForegroundColor = System.Drawing.Color.FromArgb(, , );
- style.Pattern = BackgroundType.Solid;
- style.Font.IsBold = true;
- int rowIndex = ;
- for (int i = ; i < datatable.Columns.Count; i++)
- {
- DataColumn col = datatable.Columns[i];
- string columnName = col.Caption ?? col.ColumnName;
- wb.Worksheets[].Cells[rowIndex, i].PutValue(columnName);
- wb.Worksheets[].Cells[rowIndex, i].SetStyle(style);
- }
- rowIndex++;
- foreach (DataRow row in datatable.Rows)
- {
- for (int i = ; i < datatable.Columns.Count; i++)
- {
- wb.Worksheets[].Cells[rowIndex, i].PutValue(row[i].ToString());
- }
- rowIndex++;
- }
- for (int k = ; k < datatable.Columns.Count; k++)
- {
- wb.Worksheets[].AutoFitColumn(k, , );
- }
- wb.Worksheets[].FreezePanes(, , , datatable.Columns.Count);
- wb.Save(filepath);
- return true;
- }
- catch (Exception e)
- {
- error = error + " DataTableToExcel: " + e.Message;
- return false;
- }
- }
- protected void btnExport_Click(object sender, EventArgs e)
- {//导出
- int ClassID = ;
- int.TryParse(hidClassID.Value, out ClassID);
- string error = "";
- string filepath = "";
- BLL.TUser bll_TUser = new BLL.TUser();
- BLL.TClass bll_Class = new BLL.TClass();
- Model.TClass model = (new BLL.TClass()).GetModel(ClassID);
- //处理DataTable
- DataTable dt = bll_TUser.GetListByClass(ClassID);
- DataTable dtNew = new DataTable();
- dtNew.Columns.Add("姓名", typeof(string));
- dtNew.Columns.Add("学号", typeof(string));
- dtNew.Columns.Add("性别", typeof(string));
- dtNew.Columns.Add("电话", typeof(string));
- if (dt != null && dt.Rows.Count > )
- {
- DataRow drNew = dtNew.NewRow();
- foreach (DataRow dr in dt.Rows)
- {
- //drNew = dtNew.NewRow();
- drNew["姓名"] = dr["UserName"];
- drNew["学号"] = dr["IDNO"];
- drNew["性别"] = dr["Sex"].ToString() == "" ? "男" : (dr["Sex"].ToString() == "" ? "女" : "");
- drNew["电话"] = dr["Phone"];
- dtNew.Rows.Add(drNew.ItemArray);
- }
- }
- if (model != null)
- {
- filepath = "/UploadFiles/ExportClass/";// + model.ClassName + ".xlsx";
- string filaname = model.ClassName + ".xlsx";
- string finalPath = MapPath("~" + filepath + filaname);
- //检查有该路径是否就创建
- if (!Directory.Exists(MapPath("~/UploadFiles/ExportClass/")))
- {
- Directory.CreateDirectory(MapPath("~/UploadFiles/ExportClass/"));
- }
- if (DataTableToExcel2(dtNew, finalPath, out error))
- {
- string SiteRoot = "http://" + Request.Url.Authority.ToString() + filepath + Uri.EscapeDataString(filaname); //进行编码,便于中文名文件下载
- //下载excel
- ClientScript.RegisterStartupScript(this.GetType(), "", ",<script type='text/javascript'>window.open('" + SiteRoot + "');</script>");
- }
- else
- {
- ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('提示', '" + error + "!');</script>");
- }
- }
- else
- {
- ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('提示', '班级不存在!');</script>");
- }
- }
Aspose.Cells导出Excel(1)的更多相关文章
- C#使用Aspose.Cells导出Excel简单实现
首先,需要添加引用Aspose.Cells.dll,官网下载地址:http://downloads.aspose.com/cells/net 将DataTable导出Xlsx格式的文件下载(网页输出) ...
- Aspose.Cells导出Excel(2)
DataTable dtTitle = ds.Tables[]; DataTable dtDetail = ds.Tables[]; int columns = dtTitle.Columns.Cou ...
- C#+Aspose.Cells 导出Excel及设置样式 (Webform/Winform)
在项目中用到,特此记录下来,Aspose.Cells 不依赖机器装没有装EXCEL都可以导出,很方便.具体可以参考其他 http://www.aspose.com/docs/display/cells ...
- C# 使用Aspose.Cells 导出Excel
今天在工作中碰到同事用了一种新型的方式导入excel,在此做个学习记录. 插件:Aspose.Cells 第一步:准备好导出的模板,例子: C#代码: #region 验证数据 if (model = ...
- aspose.Cells 导出Excel
aspose aspse.Cells可以操作Excel,且不依赖于系统环境. 使用模板,通过绑定输出数据源 这种适合于对格式没有特别要求的,直接绑定数据源即可.和数据绑定控件差不多. Workbook ...
- Aspose.Cells 导出 excel
Aspose.Cells.Workbook book = new Aspose.Cells.Workbook(); Aspose.Cells.Worksheet sheet = book.Worksh ...
- 使用Aspose.Cells读取Excel
最新更新请访问: http://denghejun.github.io Aspose.Cells读取Excel非常方便,以下是一个简单的实现读取和导出Excel的操作类: 以下是Aspose.Ce ...
- 报表中的Excel操作之Aspose.Cells(Excel模板)
原文:报表中的Excel操作之Aspose.Cells(Excel模板) 本篇中将简单记录下Aspose.Cells这个强大的Excel操作组件.这个组件的强大之处,就不多说,对于我们的报表总是会有导 ...
- 怎么使用Aspose.Cells读取excel 转化为Datatable
说明:vs2012 asp.net mvc4 c# 使用Aspose.Cells 读取Excel 转化为Datatable 1.HTML前端代码 <%@ Page Language=" ...
随机推荐
- 【Machine Learning】机器学习及其基础概念简介
机器学习及其基础概念简介 作者:白宁超 2016年12月23日21:24:51 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现的深入理解.本系列文章是作者结 ...
- WebGIS项目中利用mysql控制点库进行千万条数据坐标转换时的分表分区优化方案
文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1. 背景 项目中有1000万条历史案卷,为某地方坐标系数据,我们的真实 ...
- 开发者最爱的Firebug停止更新和维护
近日,Firebug团队在其官网上宣布,Firebug将不再继续开发和维护,并邀请大家使用Firefox的内置开发工具. Firebug最初是2006年1月由Joe Hewitt编写, ...
- 数塔问题(DP算法)自底向上计算最大值
Input 输入数据首先包括一个整数C,表示测试实例的个数,每个测试实例的第一行是一个整数N(1 <= N <= 100),表示数塔的高度,接下来用N行数字表示数塔,其中第i行有个i个整数 ...
- CSS学习笔记
CSS学习笔记 2016年12月15日整理 CSS基础 Chapter1 在console输入escape("宋体") ENTER 就会出现unicode编码 显示"%u ...
- CSS三个定位——常规、浮动、绝对定位
.dage { width: 868px; background: #5B8C75; border: 10px solid #A08C5A; margin-top: -125px; margin-le ...
- SharePoint 2013管理中心里【管理服务器上的服务】不见了
打开管理中心,准备配置Managed Metadata Service,发现"管理服务器上的服务"不见了 那我自己拼url直接访问:http://xxxx/_admin/Serve ...
- 【centos7常用技巧】RPM打包
一.RPM打包的目的 1.当目标机中不存在编译环境时,可以先在本地环境中编译打包,然后直接在目标机中用rpm -ivh *.rpm安装即可. 2.当需要在目标机中安装多个软件或者增加多个文件时,可以将 ...
- Merge Sorted Array
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...
- ASP.NET MVC 使用 Petapoco 微型ORM框架+NpgSql驱动连接 PostgreSQL数据库
前段时间在园子里看到了小蝶惊鸿 发布的有关绿色版的Linux.NET——“Jws.Mono”.由于我对.Net程序跑在Linux上非常感兴趣,自己也看了一些有关mono的资料,但是一直没有时间抽出时间 ...