日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

aspose.cells html excel导出,C#使用Aspose.Cells导出Excel简单实现

發布時間:2025/1/21 C# 85 豆豆
生活随笔 收集整理的這篇文章主要介紹了 aspose.cells html excel导出,C#使用Aspose.Cells导出Excel简单实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

首先,需要添加引用Aspose.Cells.dll,官網下載地址:http://downloads.aspose.com/cells/net

將DataTable導出Xlsx格式的文件下載(網頁輸出):

1 ///

2 /// 導出Excel表格

3 ///

4 /// 數據集合

5 /// 數據表頭

6 ///

7 public void ExportExcel(DataTable dt, string[] header)

8 {

9 Workbook wb = new Workbook(FileFormatType.Xlsx);

10 try

11 {

12 Worksheet sheet = wb.Worksheets[0];

13 sheet.Name = "MO上行查詢結果";

14 if (dt.Rows.Count <= 0)

15 {

16 System.Web.HttpContext.Current.Response.Write("");

17 return;

18 }

19 // 為單元格添加樣式

20 Aspose.Cells.Style style = wb.CreateStyle();

21 style.HorizontalAlignment = Aspose.Cells.TextAlignmentType.Center; //設置居中

22 style.Font.Size = 12;//文字大小

23 style.Font.IsBold = true;//粗體

24 style.HorizontalAlignment = TextAlignmentType.Center;//文字居中

25

26 int rowIndex = 0;

27 for (int i = 0; i < header.Length; i++)

28 {

29 sheet.Cells[rowIndex, i].PutValue(header[i]);

30 sheet.Cells[rowIndex, i].SetStyle(style);

31 sheet.Cells.SetColumnWidth(i, 20);//設置寬度

32 }

33 for (int i = 0; i < dt.Rows.Count; i++)//遍歷DataTable行

34 {

35 sheet.Cells[i + 1, 0].PutValue(dt.Rows[i]["SENDER"].ToString());

36 sheet.Cells[i + 1, 1].PutValue(dt.Rows[i]["SENDCONTENT"].ToString());

37 sheet.Cells[i + 1, 2].PutValue("");

38 sheet.Cells[i + 1, 3].PutValue(dt.Rows[i]["RECDATE"].ToString());

39 sheet.Cells[i + 1, 4].PutValue(dt.Rows[i]["sn"].ToString());

40 }

41 }

42 catch (Exception e)

43 {

44 System.Web.HttpContext.Current.Response.Write("");

45 }

46 #region 輸出到Excel

47 using (MemoryStream ms = new MemoryStream())

48 {

49

50 wb.Save(ms, new OoxmlSaveOptions(SaveFormat.Xlsx));//默認支持xls版,需要修改指定版本

51 System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xlsx", DateTime.Now.ToString("yyyyMMddHHmmssfff")));

52 System.Web.HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";

53 System.Web.HttpContext.Current.Response.BinaryWrite(ms.ToArray());

54 wb = null;

55 System.Web.HttpContext.Current.Response.End();

56 }

57 #endregion

58 }

Aspose.Cells.dll 下載地址:http://pan.baidu.com/s/1o8TRXDg

其它相關參考:

https://my.oschina.net/u/876556/blog/98801

http://www.cnblogs.com/top5/archive/2010/02/16/1668801.html

http://www.cnblogs.com/springyangwc/archive/2011/08/12/2136498.html

Aspose.Cells組件可以不依賴excel來導入導出excel文件:

導入:

public static System.Data.DataTable ReadExcel(String strFileName)

{

Workbook book = new Workbook();

book.Open(strFileName);

Worksheet sheet = book.Worksheets[0];

Cells cells = sheet.Cells;

return cells.ExportDataTableAsString(0, 0, cells.MaxDataRow + 1, cells.MaxDataColumn + 1, true);

}

導出:

private static void Export(IEnumerable data, HttpResponse response)

{

Workbook workbook = new Workbook();

Worksheet sheet = (Worksheet)workbook.Worksheets[0];

PropertyInfo[] ps = typeof(T).GetProperties();

var colIndex = "A";

foreach (var p in ps)

{

sheet.Cells[colIndex + 1].PutValue(p.Name);

int i = 2;

foreach (var d in data)

{

sheet.Cells[colIndex + i].PutValue(p.GetValue(d, null));

i++;

}

colIndex = ((char)(colIndex[0] + 1)).ToString();

}

response.Clear();

response.Buffer = true;

response.Charset = "utf-8";

response.AppendHeader("Content-Disposition", "attachment;filename=xxx.xls");

response.ContentEncoding = System.Text.Encoding.UTF8;

response.ContentType = "application/ms-excel";

response.BinaryWrite(workbook.SaveToStream().ToArray());

response.End();

}

非常簡單,好用!

————————————————

版權聲明:本文為CSDN博主「weiky626」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。

原文鏈接:https://blog.csdn.net/weiky626/article/details/7514637

總結

以上是生活随笔為你收集整理的aspose.cells html excel导出,C#使用Aspose.Cells导出Excel简单实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。