博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
aspose将datatable导出2
阅读量:4595 次
发布时间:2019-06-09

本文共 1634 字,大约阅读时间需要 5 分钟。

     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

你可能感兴趣的文章
优云软件助阵GOPS·2017全球运维大会北京站
查看>>
linux 装mysql的方法和步骤
查看>>
poj3667(线段树区间合并&区间查询)
查看>>
51nod1241(连续上升子序列)
查看>>
SqlSerch 查找不到数据
查看>>
集合相关概念
查看>>
Memcache 统计分析!
查看>>
(Python第四天)字符串
查看>>
个人介绍
查看>>
使用python动态特性时,让pycharm自动补全
查看>>
关于R软件的安装
查看>>
MySQL数据库免安装版配置
查看>>
你必知必会的SQL面试题
查看>>
html5 Canvas绘制时钟以及绘制运动的圆
查看>>
Unity3D热更新之LuaFramework篇[05]--Lua脚本调用c#以及如何在Lua中使用Dotween
查看>>
JavaScript空判断
查看>>
洛谷 P1439 【模板】最长公共子序列(DP,LIS?)
查看>>
python timeit
查看>>
Wireless Network 并查集
查看>>
51nod 1019 逆序数
查看>>