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.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;
  }

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持神馬文庫。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章