简介

Aspose.Cells是一款功能强大的 Excel 文档处理和转换控件,不依赖 Microsoft Excel 环境,支持所有 Excel 格式类型的操作。

下载 Aspose.Cells.dll

获取Excel数据

Workbook workbook = new Workbook("E:\\test.xlsx");
Cells cells = workbook.Worksheets[].Cells;
for (int i = ; i < cells.MaxDataRow + ; i++)
{
for (int j = ; j < cells.MaxDataColumn + ; j++)
{
string s = cells[i, j].StringValue.Trim();
//一行行的读取数据,插入数据库的代码也可以在这里写
}
}

返回DataTable数据

Workbook workbook = new Workbook("E:\\test.xlsx");
Cells cells = workbook.Worksheets[].Cells;
System.Data.DataTable dataTable1 = cells.ExportDataTable(, , cells.MaxDataRow, cells.MaxColumn);//没有标题
System.Data.DataTable dataTable2 = cells.ExportDataTable(, , cells.MaxDataRow + , cells.MaxColumn, true);//有标题

无标题

有标题

使用小结

生成Excel

/// <summary>
/// DataTable数据导出Excel
/// </summary>
/// <param name="data"></param>
/// <param name="filepath"></param>
public static void DataTableExport(DataTable data, string filepath)
{
try
{
//Workbook book = new Workbook("E:\\test.xlsx"); //打开工作簿
Workbook book = new Workbook(); //创建工作簿
Worksheet sheet = book.Worksheets[]; //创建工作表
Cells cells = sheet.Cells; //单元格
//创建样式
Aspose.Cells.Style style = book.Styles[book.Styles.Add()];
style.Borders[Aspose.Cells.BorderType.LeftBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //应用边界线 左边界线
style.Borders[Aspose.Cells.BorderType.RightBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //应用边界线 右边界线
style.Borders[Aspose.Cells.BorderType.TopBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //应用边界线 上边界线
style.Borders[Aspose.Cells.BorderType.BottomBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //应用边界线 下边界线
style.HorizontalAlignment = TextAlignmentType.Center; //单元格内容的水平对齐方式文字居中
style.Font.Name = "宋体"; //字体
style1.Font.IsBold = true; //设置粗体
style.Font.Size = ; //设置字体大小
//style.ForegroundColor = System.Drawing.Color.FromArgb(153, 204, 0); //背景色
//style.Pattern = Aspose.Cells.BackgroundType.Solid; //背景样式
//style.IsTextWrapped = true; //单元格内容自动换行 //表格填充数据
int Colnum = data.Columns.Count;//表格列数
int Rownum = data.Rows.Count;//表格行数
//生成行 列名行
for (int i = ; i < Colnum; i++)
{
cells[, i].PutValue(data.Columns[i].ColumnName); //添加表头
cells[, i].SetStyle(style); //添加样式
//cells.SetColumnWidth(i, data.Columns[i].ColumnName.Length * 2 + 1.5); //自定义列宽
//cells.SetRowHeight(0, 30); //自定义高
}
//生成数据行
for (int i = ; i < Rownum; i++)
{
for (int k = ; k < Colnum; k++)
{
cells[ + i, k].PutValue(data.Rows[i][k].ToString()); //添加数据
cells[ + i, k].SetStyle(style); //添加样式
}
cells[ + i, ].Formula = "=B" + ( + i) + "+C" + ( + i);//给单元格设置计算公式,计算班级总人数
}
sheet.AutoFitColumns(); //自适应宽
book.Save(filepath); //保存
GC.Collect();
}
catch (Exception e)
{
logger.Error("生成excel出错:" + e.Message);
}
}

调用Excel(例子)

public void DownExcel()
{
//创建DataTable
DataTable dt = new DataTable("Table_AX");
dt.Columns.Add("班级名称", System.Type.GetType("System.String"));
dt.Columns.Add("男生人数", System.Type.GetType("System.String"));
dt.Columns.Add("女生人数", System.Type.GetType("System.String"));
dt.Columns.Add("今日请假", System.Type.GetType("System.String"));
dt.Columns.Add("今日迟到", System.Type.GetType("System.String"));
Random ran = new Random();
for (int i = ; i < ; i++)
{
DataRow dr = dt.NewRow();
dr["班级名称"] = "软件技术" + i + "班";
dr["男生人数"] = ran.Next(, );
dr["女生人数"] = ran.Next(, );
dr["今日请假"] = ran.Next(, );
dr["今日迟到"] = ran.Next(, );
dt.Rows.Add(dr);
}
//地址
string fileName = "班级概况.xls";
string filePath = AppDomain.CurrentDomain.BaseDirectory.Replace("\\", "/") + "GxContent/data/student/" + fileName + "";
//生成Excel
ExcelHelper.DataTableExport(dt, filePath);
//下载
if (System.IO.File.Exists(filePath))
{
try
{
System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
if (fileInfo.Exists == true)
{
//每次读取文件,只读取1M,这样可以缓解服务器的压力
const long ChunkSize = ;
byte[] buffer = new byte[ChunkSize]; Response.Clear();
//获取文件
System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);
//获取下载的文件总大小
long dataLengthToRead = iStream.Length;
Response.ContentType = "application/octet-stream";
//通知浏览器下载文件而不是打开
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
using (iStream)//解决文件占用问题,using 外 iStream.Dispose() 无法释放文件
{
while (dataLengthToRead > && Response.IsClientConnected)
{
int lengthRead = iStream.Read(buffer, , Convert.ToInt32(ChunkSize));//读取的大小
Response.OutputStream.Write(buffer, , lengthRead);
Response.Flush();
dataLengthToRead = dataLengthToRead - lengthRead;
}
iStream.Dispose();
iStream.Close();
}
Response.End();
}
}
catch
{
Response.Write("<script>alert('文件未占用或文件未生成,请稍后重试!');window.close();</script>");
}
}
else
{
Response.Write("<script>alert('文件还未生成完,请稍后重试!');window.close();</script>");
}
}

效果


相关文章:C# 读写Excel的一些方法,Aspose.Cells.dll

C# Aspose.Cells.dll Excel操作总结的更多相关文章

  1. C# WinForm 导出导入Excel/Doc 完整实例教程[使用Aspose.Cells.dll]

    [csharp] view plain copy 1.添加引用: Aspose.Cells.dll(我们就叫工具包吧,可以从网上下载.关于它的操作我在“Aspose.Cells操作说明 中文版 下载 ...

  2. C# WinForm使用Aspose.Cells.dll 导出导入Excel/Doc 完整实例教程

    1.添加引用: Aspose.Cells.dll(我们就叫工具包吧,可以从网上下载.关于它的操作我在“Aspose.Cells操作说明 中文版 下载 Aspose C# 导出Excel 实例”一文中的 ...

  3. Aspose.Cells.dll引用导入导出Excel

    Aspose.Cells 导入导出EXCEL 文章出处:http://hi.baidu.com/leilongbing/item/c11467e1819e5417595dd8c1 修改样式       ...

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

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

  5. C# 读写Excel的一些方法,Aspose.Cells.dll

    需求:现有2个Excel,一个7000,一个20W,7000在20W是完全存在的.现要分离20W的,拆分成19W3和7000. 条件:两个Excel都有“登录名”,然后用“登录名”去关联2个Excel ...

  6. C# 利用Aspose.Cells .dll将本地excel文档转化成pdf(完美破解版 无水印 无中文乱码)

    Aspose.Cells .dll下载  http://pan.baidu.com/s/1slRENLF并引用 C#代码 using System; using System.Collections. ...

  7. Aspose.Cells.dll操作execl

    附件:Aspose.Cells.dll 1.创建execl(不需要服务器或者客户端安装office) public void DCExexl(DataTable dt) {  Workbook wb ...

  8. 使用Aspose.Cells读取Excel

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

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

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

随机推荐

  1. css冲刺

    CSS知识点及面试题 1.一般与文本相关的属性都可以继承text-/font-/line- 2.background属性1)background-repeat:repeat/repeat-x/repe ...

  2. TOML简介 (转) TOML的由来

    TOML的由来 配置文件的使用由来已久,从.ini.XML.JSON.YAML再到TOML,语言的表达能力越来越强,同时书写便捷性也在不断提升. TOML是前GitHub CEO, Tom Prest ...

  3. Python3 与 C# 并发编程之~ Net篇

    NetCore并发编程 示例代码:https://github.com/lotapp/BaseCode/tree/master/netcore/4_Concurrency 先简单说下概念(其实之前也有 ...

  4. 国际化之Android设备支持的语种

    昨天发了关于iOS支持的语种,文章最后也补了安卓支持语种列表.但最后发现安卓设备支持跟它列的有出入,我重新完全手工整理了一遍. 我将对应的语种在安卓的语言列表里的显示,也全部逐一列出来了,方便大家到时 ...

  5. 性能优化 Profiler MAT 内存泄漏 堆转储 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  6. 9 月份 GitHub 上最火的 JavaScript 开源项目!

    推荐 GitHub 上9 月份最受欢迎的 10 个 JavaScript 开源项目,在这些项目中,你有在用或用过哪些呢? 1.基于 Promise 的 HTTP 客户端 Axios https://g ...

  7. 《闲聊 ASP.NET Core》系列直播清单

    [闲聊 ASP.NET Core]第一期:项目与应用结构 [闲聊 ASP.NET Core]第二期:Web Host 初始化与生命周期事件 [闲聊ASP.NET Core]第三期:应用程序配置 [闲聊 ...

  8. freenode configuration sasl authentication in weechat

    转自:https://www.weechat.org/files/doc/stable/weechat_user.en.html#irc_sasl_authentication SASL authen ...

  9. Android Studio打包过程和应用安装过程

    三个部分,检查项目和读取基本配置,Gradle Build,Apk Install和LaunchActivity. 应用安装到手机,会复制APK安装包到data/app目录下,解压并扫描安装包,把de ...

  10. SNF快速开发平台成长史V4.5-Spring.Net.Framework-SNF软件开发机器人

    SNF快速开发平台成长史 SNF框架CS\BS 视频教程 https://pan.baidu.com/s/1dFegFKX SNF开发机器人教程:链接:https://pan.baidu.com/s/ ...