简介

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. bootstrap常见的面试题

    1.  如果让一个元素在pc端显示而在手机端隐藏,下列选项正确的是(b). A. visible-xs-8  hidden-md B. visible-md-8 hidden-xs C. visibl ...

  2. JSAP103

    JSAP103 1.节点 1)定义:不是元素,节点是页面中的所有内容(标签,属性,文本),Node.它使得任何标签中的元素获取都十分方便 2)节点的相关属性 可以使用标签即元素点出来,可以使用属性节点 ...

  3. pygame 笔记-6 碰撞检测

    这一节学习碰撞检测,先看原理图: 2个矩形如果发生碰撞(即:图形有重叠区域),按上图的判断条件就能检测出来,如果是圆形,则稍微变通一下,用半径检测.如果是其它不规则图形,大多数游戏中,并不要求精确检测 ...

  4. ssh以root用户远程登录失败

    参考文献: http://blog.csdn.net/lichangzai/article/details/39379153 http://blog.csdn.net/yasi_xi/article/ ...

  5. securecrt通过ssh连接板子: 密钥交换失败,没有兼容的加密程序

    在用securecrt连接板子时遇到如下问题: 需要修改板子上的/etc/ssh/ssh_config和/etc/ssh/sshd_config. 修改/etc/ssh/ssh_config,取消下面 ...

  6. android应用程序中获取view的位置

    我们重点在获取view的y坐标,你懂的... 依次介绍以下四个方法: 1.getLocationInWindow int[] position = new int[2]; textview.getLo ...

  7. fashion datasets图像检索实践project

    Using Siamese Networks and Pre-Trained Convolutional Neural Networks (CNNs) for Fashion Similarity M ...

  8. iOS https请求 NSURLSessionDataTask

    // //  YKSHttpsRequest.m //  YKShareSdkDemo // //  Created by qingyun on 22/05/2017. //  Copyright © ...

  9. sort is deprecated, use sort_values(inplace=True) for INPLACE sorting

    排序是过时的,用sort_values(到位=真)为就地排序

  10. linux 通过nvm安装node

    官方介绍:https://github.com/creationix/nvm#installation PS:通常不要用root权限安装软件,因为线上任何服务部署都不允许用root,其他软件用root ...