aspose将datatable导出2

     public static bool ExportExcelWithAspose(System.Data.DataTable dt, string path)
 2         {
 3             bool succeed = false;
 4             if (dt != null)
 5             {
 6                 try
 7                 {
 8                     Aspose.Cells.License li = new Aspose.Cells.License();
 9                     string lic = Resources.License;
10                     Stream s = new MemoryStream(ASCIIEncoding.Default.GetBytes(lic));
11                     li.SetLicense(s);
12 
13                     Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();
14                     Aspose.Cells.Worksheet cellSheet = workbook.Worksheets[0];
15 
16                     cellSheet.Name = dt.TableName;
17 
18                     int rowIndex = 0;
19                     int colIndex = 0;
20                     int colCount = dt.Columns.Count;
21                     int rowCount = dt.Rows.Count;
22 
23                     //列名的处理
24                     for (int i = 0; i < colCount; i++)
25                     {
26                         cellSheet.Cells[rowIndex, colIndex].PutValue(dt.Columns[i].ColumnName);
27                         cellSheet.Cells[rowIndex, colIndex].Style.Font.IsBold = true;
28                         cellSheet.Cells[rowIndex, colIndex].Style.Font.Name = "宋体";
29                         colIndex++;
30                     }
31 
32                     Aspose.Cells.Style style = workbook.Styles[workbook.Styles.Add()];
33                     style.Font.Name = "Arial";
34                     style.Font.Size = 10;
35                     Aspose.Cells.StyleFlag styleFlag = new Aspose.Cells.StyleFlag();
36                     cellSheet.Cells.ApplyStyle(style, styleFlag);
37 
38                     rowIndex++;
39 
40                     for (int i = 0; i < rowCount; i++)
41                     {
42                         colIndex = 0;
43                         for (int j = 0; j < colCount; j++)
44                         {
45                             cellSheet.Cells[rowIndex, colIndex].PutValue(dt.Rows[i][j].ToString());
46                             colIndex++;
47                         }
48                         rowIndex++;
49                     }
50                     cellSheet.AutoFitColumns();
51 
52                     path = Path.GetFullPath(path);
53                     workbook.Save(path);
54                     succeed = true;
55                 }
56                 catch (Exception ex)
57                 {
58                     succeed = false;
59                 }
60             }
61 
62             return succeed;
63         }

转载于:https://www.cnblogs.com/JQlin-c/archive/2012/04/19/2456854.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值