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=" ...
随机推荐
- 【NLP】Python NLTK处理原始文本
Python NLTK 处理原始文本 作者:白宁超 2016年11月8日22:45:44 摘要:NLTK是由宾夕法尼亚大学计算机和信息科学使用python语言实现的一种自然语言工具包,其收集的大量公开 ...
- 【定有惊喜】android程序员如何做自己的API接口?php与android的良好交互(附环境搭建),让前端数据动起来~
一.写在前面 web开发有前端和后端之分,其实android还是有前端和后端之分.android开发就相当于手机app的前端,一般都是php+android或者jsp+android开发.androi ...
- xss和sql注入原理学习
8.4 Web跨站脚本攻击 8.4.1 跨站脚本攻击的原理(1) 跨站脚本在英文中称为Cross-Site Scripting,缩写为CSS.但是,由于层叠样式表 (Cascading Style ...
- Android中实现APP文本内容的分享发送与接收方法简述
谨记(指定选择器Intent.createChooser()) 开始今天的内容前,先闲聊一下: (1)突然有一天头脑风暴,对很多问题有了新的看法和见解,迫不及待的想要分享给大家,文档已经写好了,我需要 ...
- git远程库GitHub
首先,注册一个GitHub(github.com)帐号,免费获得Git远程仓库 由于本地Git仓库和GitHub仓库之间的传输是通过SSH加密的,所以,需要一点设置: 第1步:创建SSH Key.在用 ...
- 烂泥:VMWare Workation双网卡配置IP地址
本文由ilanniweb提供友情赞助,首发于烂泥行天下 想要获得更多的文章,可以关注我的微信ilanniweb 前几天给一个客户做远程项目实施,客户那边的服务器是Windows OS的,我们这边的业务 ...
- WebAPI 2参数绑定方法
简单类型参数 Example 1: Sending a simple parameter in the Url [RoutePrefix("api/values")] public ...
- 2016年中国微信小程序专题研究报告
2016年12月29日,全球领先的移动互联网第三方数据挖掘和分析机构iiMedia Research(艾媒咨询)权威首发<2016年中国微信小程序专题研究报告>. 报告显示,82.6%手机 ...
- Harmonic Number(调和级数+欧拉常数)
题意:求f(n)=1/1+1/2+1/3+1/4-1/n (1 ≤ n ≤ 108).,精确到10-8 (原题在文末) 知识点: 调和级数(即f(n))至今没有一个完全正确的公式, ...
- Spring MVC注解开发入门
注解式开发初步 常用的两个注解: @Controller:是SpringMVC中最常用的注解,它可以帮助定义当前类为一个Spring管理的bean,同时指定该类是一个控制器,可以用来接受请求.标识当前 ...