重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

怎么在C#中利用Aspose.Cells导出excel-创新互联

今天就跟大家聊聊有关怎么在C#中利用Aspose.Cells导出excel,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

专注于为中小企业提供做网站、成都做网站服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业武昌免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上1000+企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

C#中winform使用spose.Cells导出excel的方法:


1.下载aspose.Cells.dll以及破解证书:下载地址

2.引用右键添加引用,点击浏览,找到下载的dll文件(好复制到工程目录),选择Aspose.Cells引用

怎么在C#中利用Aspose.Cells导出excel

3.工程右键添加文件夹ASPOSE,并右键添加“现有项”aspose.Cells.dll以及破解证书。分别右键aspose.Cells.dll以及license.lic选择属性,始终复制到输出目录。

怎么在C#中利用Aspose.Cells导出excel

怎么在C#中利用Aspose.Cells导出excel

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-创新互联
浏览路径:http://cqcxhl.cn/article/spjij.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP