怎么在C#中利用Aspose.Cells导出excel
今天就跟大家聊聊有关怎么在C#中利用Aspose.Cells导出excel,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
C#中winform使用spose.Cells导出excel的方法:
1.下载aspose.Cells.dll以及破解证书:下载地址
2.引用右键添加引用,点击浏览,找到下载的dll文件(最好复制到工程目录),选择Aspose.Cells引用
3.工程右键添加文件夹ASPOSE,并右键添加“现有项”aspose.Cells.dll以及破解证书。分别右键aspose.Cells.dll以及license.lic选择属性,始终复制到输出目录。
4.
添加using
usingAspose.Cells;
新建DataTable
DataTabledt1=newDataTable();
初始化表头:
dt1.Columns.Add(newDataColumn("表头1",typeof(string))); dt1.Columns.Add(newDataColumn("表头2",typeof(string))); dt1.Columns.Add(newDataColumn("表头3",typeof(string))); dt1.Columns.Add(newDataColumn("表头4",typeof(string)));
添加数据(可以放到循环体)
DataRowrowData=dt1.NewRow(); rowData["表头1"]="1" rowData["表头2"]="2"; rowData["表头3"]="3"; rowData["表头4"]="4"; dt1.Rows.Add(rowData);//新增一行数据
将DataTabel写入excel
ExportExcelWithAspose(dt1,"D:\\设备数据.xlsx");
函数实现:
publicstaticboolExportExcelWithAspose(System.Data.DataTabledata,stringfilepath) { try { if(data==null) { MessageBox.Show("数据为空"); returnfalse; } Aspose.Cells.Licenseli=newAspose.Cells.License(); li.SetLicense("ASPOSE/License.lic");//破解证书 Workbookbook=newWorkbook();//创建工作簿 Worksheetsheet=book.Worksheets[0];//创建工作表 Cellscells=sheet.Cells;//单元格 //创建样式 Aspose.Cells.Stylestyle=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=11;//设置字体大小 //style.ForegroundColor=System.Drawing.Color.FromArgb(153,204,0);//背景色 //style.Pattern=Aspose.Cells.BackgroundType.Solid; intColnum=data.Columns.Count;//表格列数 intRownum=data.Rows.Count;//表格行数 //生成行列名行 for(inti=0;i看完上述内容,你们对怎么在C#中利用Aspose.Cells导出excel有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注恰卡编程网行业资讯频道,感谢大家的支持。
推荐阅读
-
Web应用从零开始,初学者友好型开发教程
-
容器化最佳实践:Docker 与 Kubernetes 在微服务架构中的协同设计
-
AWS Cloud9 使用攻略:云端 IDE 如何无缝集成 Lambda 与 S3 服务?
-
Heroku vs AWS Elastic Beanstalk:快速部署 Web 应用的平台对比
-
Kubernetes 集群部署避坑:资源调度、服务发现与滚动更新策略
-
Docker 镜像优化指南:分层构建、瘦身技巧与多阶段编译实践
-
Postman 接口测试全流程:从 API 设计到自动化测试脚本编写
-
pytest 框架进阶:自定义 fixture、插件开发与持续集成集成方案
-
JUnit 5 新特性:参数化测试、扩展模型与微服务测试实践
-
Chrome DevTools 性能分析:FPS 监控、内存快照与网络请求优化指南