Aspose.Cells简介及用到的几个方法
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之下,将是一个简单的事情。
//获取指定虚拟路径的物理路径
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;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了