欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#使用Aspose.Cells導(dǎo)出excel

 更新時(shí)間:2018年12月13日 15:30:15   作者:RobinChow_  
這篇文章主要為大家詳細(xì)介紹了C#使用Aspose.Cells導(dǎo)出excel,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

C# winform導(dǎo)出excel可以使用 Microsoft.Office.Interop.Excel.dll或者Aspose.Cells.dll以及其他方法。Microsoft.Office.Interop.Excel.dll導(dǎo)出速度慢,不適用于數(shù)據(jù)量大情況。Aspose.Cells.dll到處速度很快。由于Aspose.Cells.dll本身收費(fèi),所以需要加載破解證書。

Aspose.Cells簡(jiǎn)介:Aspose.Cells是一款功能強(qiáng)大的Excel文檔處理和轉(zhuǎn)換控件,開發(fā)人員和客戶電腦無(wú)需安裝Microsoft Excel也能在應(yīng)用程序中實(shí)現(xiàn)類似Excel的強(qiáng)大數(shù)據(jù)管理功能,支持所有Excel格式類型的操作,在沒有Microsoft Excel的環(huán)境下,用戶也可為其應(yīng)用程序嵌入類似Excel的強(qiáng)大數(shù)據(jù)管理功能。

C#中winform使用spose.Cells導(dǎo)出excel的方法:

1.下載aspose.Cells.dll以及破解證書:下載地址

2.引用右鍵添加引用,點(diǎn)擊瀏覽,找到下載的dll文件(最好復(fù)制到工程目錄),選擇Aspose.Cells引用

3.工程右鍵添加文件夾ASPOSE,并右鍵添加“現(xiàn)有項(xiàng)”aspose.Cells.dll以及破解證書。分別右鍵aspose.Cells.dll以及l(fā)icense.lic選擇屬性,始終復(fù)制到輸出目錄。

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)));

添加數(shù)據(jù)(可以放到循環(huán)體)

DataRow rowData = dt1.NewRow();
rowData["表頭1"] = "1"
rowData["表頭2"] = "2";
rowData["表頭3"] = "3";
rowData["表頭4"] = "4";
dt1.Rows.Add(rowData);//新增一行數(shù)據(jù)

將DataTabel寫入excel

ExportExcelWithAspose(dt1, "D:\\設(shè)備數(shù)據(jù).xlsx");

函數(shù)實(shí)現(xiàn):

public static bool ExportExcelWithAspose(System.Data.DataTable data, string filepath)
  {
   try
   {
    if (data == null)
    {
     MessageBox.Show("數(shù)據(jù)為空");
     return false;
    }
    Aspose.Cells.License li = new Aspose.Cells.License();
    li.SetLicense("ASPOSE/License.lic");//破解證書
 
    Workbook book = new Workbook(); //創(chuàng)建工作簿
    Worksheet sheet = book.Worksheets[0]; //創(chuàng)建工作表
    Cells cells = sheet.Cells; //單元格
           //創(chuàng)建樣式
    Aspose.Cells.Style style = book.Styles[book.Styles.Add()];
    style.Borders[Aspose.Cells.BorderType.LeftBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //應(yīng)用邊界線 左邊界線 
    style.Borders[Aspose.Cells.BorderType.RightBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //應(yīng)用邊界線 右邊界線 
    style.Borders[Aspose.Cells.BorderType.TopBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //應(yīng)用邊界線 上邊界線 
    style.Borders[Aspose.Cells.BorderType.BottomBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //應(yīng)用邊界線 下邊界線 
    style.HorizontalAlignment = TextAlignmentType.Center; //單元格內(nèi)容的水平對(duì)齊方式文字居中
    style.Font.Name = "宋體"; //字體
          //style1.Font.IsBold = true; //設(shè)置粗體
    style.Font.Size = 11; //設(shè)置字體大小
          //style.ForegroundColor = System.Drawing.Color.FromArgb(153, 204, 0); //背景色
          //style.Pattern = Aspose.Cells.BackgroundType.Solid; 
 
    int Colnum = data.Columns.Count;//表格列數(shù) 
    int Rownum = data.Rows.Count;//表格行數(shù) 
            //生成行 列名行 
    for (int i = 0; i < Colnum; i++)
    {
     cells[0, i].PutValue(data.Columns[i].ColumnName); //添加表頭
     cells[0, i].SetStyle(style); //添加樣式
    }
    //生成數(shù)據(jù)行 
    for (int i = 0; i < Rownum; i++)
    {
     for (int k = 0; k < Colnum; k++)
     {
      cells[1 + i, k].PutValue(data.Rows[i][k].ToString()); //添加數(shù)據(jù)
      cells[1 + i, k].SetStyle(style); //添加樣式
     }
    }
    sheet.AutoFitColumns(); //自適應(yīng)寬
    book.Save(filepath); //保存
    MessageBox.Show("Excel成功保存到D盤!??!");
    GC.Collect();
   }
   catch (Exception e)
   {
    return false;
   }
 
   return true;
  }

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論