最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

C#使用Aspose.Cells导出excel

来源:动视网 责编:小采 时间:2020-11-27 22:34:41
文档

C#使用Aspose.Cells导出excel

C#使用Aspose.Cells导出excel:C# winform导出excel可以使用 Microsoft.Office.Interop.Excel.dll或者Aspose.Cells.dll以及其他方法。Microsoft.Office.Interop.Excel.dll导出速度慢,不适用于数据量大情况。Aspose.Cells.dll到处速度很快。由于Aspose.Cells.
推荐度:
导读C#使用Aspose.Cells导出excel:C# winform导出excel可以使用 Microsoft.Office.Interop.Excel.dll或者Aspose.Cells.dll以及其他方法。Microsoft.Office.Interop.Excel.dll导出速度慢,不适用于数据量大情况。Aspose.Cells.dll到处速度很快。由于Aspose.Cells.


C# winform导出excel可以使用 Microsoft.Office.Interop.Excel.dll或者Aspose.Cells.dll以及其他方法。Microsoft.Office.Interop.Excel.dll导出速度慢,不适用于数据量大情况。Aspose.Cells.dll到处速度很快。由于Aspose.Cells.dll本身收费,所以需要加载破解证书。

Aspose.Cells简介:Aspose.Cells是一款功能强大的Excel文档处理和转换控件,开发人员和客户电脑无需安装Microsoft Excel也能在应用程序中实现类似Excel的强大数据管理功能,支持所有Excel格式类型的操作,在没有Microsoft Excel的环境下,用户也可为其应用程序嵌入类似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

using Aspose.Cells; 

新建DataTable

DataTable dt1 = new DataTable();

初始化表头:

dt1.Columns.Add(new DataColumn("表头1", typeof(string)));
dt1.Columns.Add(new DataColumn("表头2", typeof(string)));
dt1.Columns.Add(new DataColumn("表头3", typeof(string)));
dt1.Columns.Add(new DataColumn("表头4", typeof(string)));

添加数据(可以放到循环体)

DataRow rowData = dt1.NewRow();
rowData["表头1"] = "1"
rowData["表头2"] = "2";
rowData["表头3"] = "3";
rowData["表头4"] = "4";
dt1.Rows.Add(rowData);//新增一行数据

将DataTabel写入excel

ExportExcelWithAspose(dt1, "D:\\设备数据.xlsx");

函数实现:

public static bool ExportExcelWithAspose(System.Data.DataTable data, string filepath)
 {
 try
 {
 if (data == null)
 {
 MessageBox.Show("数据为空");
 return false;
 }
 Aspose.Cells.License li = new Aspose.Cells.License();
 li.SetLicense("ASPOSE/License.lic");//破解证书
 
 Workbook book = new Workbook(); //创建工作簿
 Worksheet sheet = book.Worksheets[0]; //创建工作表
 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 = 11; //设置字体大小
 //style.ForegroundColor = System.Drawing.Color.FromArgb(153, 204, 0); //背景色
 //style.Pattern = Aspose.Cells.BackgroundType.Solid; 
 
 int Colnum = data.Columns.Count;//表格列数 
 int Rownum = data.Rows.Count;//表格行数 
 //生成行 列名行 
 for (int i = 0; i < Colnum; i++)
 {
 cells[0, i].PutValue(data.Columns[i].ColumnName); //添加表头
 cells[0, i].SetStyle(style); //添加样式
 }
 //生成数据行 
 for (int i = 0; i < Rownum; i++)
 {
 for (int k = 0; k < Colnum; k++)
 {
 cells[1 + i, k].PutValue(data.Rows[i][k].ToString()); //添加数据
 cells[1 + i, k].SetStyle(style); //添加样式
 }
 }
 sheet.AutoFitColumns(); //自适应宽
 book.Save(filepath); //保存
 MessageBox.Show("Excel成功保存到D盘!!!");
 GC.Collect();
 }
 catch (Exception e)
 {
 return false;
 }
 
 return true;
 }

文档

C#使用Aspose.Cells导出excel

C#使用Aspose.Cells导出excel:C# winform导出excel可以使用 Microsoft.Office.Interop.Excel.dll或者Aspose.Cells.dll以及其他方法。Microsoft.Office.Interop.Excel.dll导出速度慢,不适用于数据量大情况。Aspose.Cells.dll到处速度很快。由于Aspose.Cells.
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top