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=" ...
随机推荐
- Xamarin+Prism小试牛刀:定制跨平台Outlook邮箱应用
通过本文你将学会如下内容: 1,如何使用Xamarin开发跨平台(Windows,Android,iOS)应用. 2,如何使用微软的登录界面登入Microsoft账号. 3,如何使用Outlook邮箱 ...
- var和dynamic的区别
1.var 1.均是声明动态类型的变量. 2.在编译阶段已经确定类型,在初始化的时候必须提供初始化的值. 3.无法作为方法参数类型,也无法作为返回值类型. 2.dynamic 1.均是声明动态类型的变 ...
- [BootStrap] 富编辑器,基于wysihtml5
在我的周围,已经有很多人在使用BootStrap,但对于任何一个带留言.评论.提问.文章编辑功的网站,编辑器永远是重中之重,显然,早期的编辑器完全没考虑过BootStrap的出现,或皮肤跟网站不匹配, ...
- Windows API 设置窗口下控件Enable属性
参考页面: http://www.yuanjiaocheng.net/webapi/create-crud-api-1-put.html http://www.yuanjiaocheng.net/we ...
- spring maven pom.xml设置
spring pom.xml设置 <?xml version="1.0" encoding="UTF-8"?> <project xmlns= ...
- PHP static静态属性和静态方法
这里分析了php面向对象中static静态属性和静态方法的调用.关于它们的调用(能不能调用,怎么样调用),需要弄明白了他们在内存中存放位置,这样就非常容易理解了.静态属性.方法(包括静态与非静态)在内 ...
- BPM配置故事之案例14-数据字典与数据联动
小明遇到了点麻烦,他昨天又收到了行政主管发来的邮件,要求把出差申请单改由H3 BPM进行,表单如下 行政主管的出差申请表 小明对表单进行了调整,设计出了一份适合在系统中使用的表单,但在"出差 ...
- 在将 varchar 值 'xinpian' 转换成数据类型 int 时失败?
把int类型的值修改为varchar类型的值
- 编写一个通用的Makefile文件
1.1在这之前,我们需要了解程序的编译过程 a.预处理:检查语法错误,展开宏,包含头文件等 b.编译:*.c-->*.S c.汇编:*.S-->*.o d.链接:.o +库文件=*.exe ...
- windows系统路径环境变量
当前系统盘符%systemdrive%或%HOMEDRIVE%C:\ 当前系统目录%systemroot%或%Windir%C:\WINDOWS 当前用户文件夹%UserProfile%或%HOMEP ...