溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么在C#中利用Aspose.Cells導(dǎo)出excel

發(fā)布時(shí)間:2021-03-25 17:22:41 來源:億速云 閱讀:537 作者:Leah 欄目:開發(fā)技術(shù)

今天就跟大家聊聊有關(guān)怎么在C#中利用Aspose.Cells導(dǎo)出excel,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

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

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

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

怎么在C#中利用Aspose.Cells導(dǎo)出excel

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

怎么在C#中利用Aspose.Cells導(dǎo)出excel

怎么在C#中利用Aspose.Cells導(dǎo)出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)));

添加數(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;
  }

看完上述內(nèi)容,你們對(duì)怎么在C#中利用Aspose.Cells導(dǎo)出excel有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI