Aspose.Cells简介及用到的几个方法

发布时间 2023-04-26 14:37:12作者: yinghualeihenmei

https://baike.baidu.com/item/Aspose.Cells/6880387?fr=aladdin

https://www.cnblogs.com/chenwenbin/articles/7001719.html

官网:https://aspose.github.io/

简介:Aspose.Cells是一款功能强大的Excel文档处理和转换控件,开发人员和客户电脑无需安装Microsoft Excel也能在应用程序中实现类似Excel的强大数据管理功能,支持所有Excel格式类型的操作,在没有Microsoft Excel的环境下,用户也可为其应用程序嵌入类似Excel的强大数据管理功能。Aspose.Cells可以对每一个具体的数据,表格和格式进行管理,在各个层面导入图像,应用复杂的计算公式,并将应用程序中的表格保存为各种格式等。

如果你使用微软Excel的com组件,那么对于简单的操作还行,但是多余复杂的模板,那将是一个令人头疼的事。在Aspose.Cells之下,将是一个简单的事情。

1, 下载Aspose.Cells.dll 。在项目或者网站中添加引用Aspose.Cells.dll
Aspose.Cells.支持64位系统。可以很方便的操作Excel...在项目或者网站中添加引用 Aspose.Cells.dll
 
2、几个方法:
using Aspose.Cells;
 

//获取指定虚拟路径的物理路径
string path = HttpContext.Current.Server.MapPath("..//Excel//") + "License.lic";

//读取 License 文件
Stream stream = (Stream)File.OpenRead(path);

//注册 License
Aspose.Cells.License li = new Aspose.Cells.License();
li.SetLicense(stream);

//创建一个工作簿
Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();

//创建一个 sheet 表
Aspose.Cells.Worksheet worksheet = workbook.Worksheets[0];

//设置 sheet 表名称
worksheet.Name = fileName;

worksheet.Cells.SetRowHeight(0, 33);//设置高度

//获取第一行的每个单元格
cell = worksheet.Cells[rowIndex, colIndex];

//设置列名
cell.PutValue(dt_.Columns[i].ColumnName);
//合并列文字居中
worksheet.Cells.Merge(rowIndex, colIndex, 4, 1);
//设置字体
cell.Style.Font.Name = "等线";
//设置字体加粗
cell.Style.Font.IsBold = true;
//设置字体大小
cell.Style.Font.Size = 12;
//设置字体颜色
cell.Style.Font.Color = System.Drawing.Color.Black;

 

加边框:

cell = worksheet.Cells[x, y];
Aspose.Cells.Style style = cell.GetStyle();
style.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
style.Borders[BorderType.TopBorder].Color = Color.Black;
style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;
style.Borders[BorderType.BottomBorder].Color = Color.Black;
style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
style.Borders[BorderType.LeftBorder].Color = Color.Black;
style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
style.Borders[BorderType.RightBorder].Color = Color.Black;
style.HorizontalAlignment = Aspose.Cells.TextAlignmentType.Center;
cell.SetStyle(style);

 

//自动列宽
worksheet.AutoFitColumns();

 

 

//输出到浏览器下载。
byte[] bytes = workbook.SaveToStream().ToArray();
OutputClient(bytes, fileName);

public static void OutputClient(byte[] bytes, string fileName)
{
HttpContext.Current.Response.Buffer = true;

HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();

HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xls", DateTime.Now.ToString(fileName + "yyyy-MM-dd-HH-mm")));

HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = Encoding.GetEncoding("GB2312");

HttpContext.Current.Response.BinaryWrite(bytes);
HttpContext.Current.Response.Flush();

HttpContext.Current.Response.Close();
}

 

填充保存:

cellSheet.Cells.ImportDataTable(dt, false, 2, 0); //填充数据
cellSheet.Name = "Sheet1";
cellSheet.Workbook.Save(System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + ".xls", Aspose.Cells.SaveType.OpenInExcel, Aspose.Cells.FileFormatType.Default, Response);
cellSheet = null;