.Net平台下的第三方Excel类库对比

 

转自 http://topming.com/636.html

最近的一个项目中需要在DataTable和Excel之间做相互转换,Excel需是真正的xls,而不是CVS或者TVS或HTML写 法的xls。考虑到运行程序的机子上不一定专有Office,就没有用Excel程序,否则的话,装个程序还得装个office,那就又得考虑版权了,太 麻烦了。一共使用了三个不同免费的Library,分别是

myXls(http://sourceforge.net/projects/myxls/)、

Koogra(http://sourceforge.net/projects/koogra/)、

NPOI(http://npoi.codeplex.com/

三个的处理速度都非常的快,对比使用后发现这三者的功能并不一样:

myXls 这是一个免费开源的library,侧重于Excel的输出。可以设置到单个Cell,但读取功能很弱。

Koogra与myXls恰恰相反,是一个非常好用Excel读取类库,可是在测试过程中发现Koogra读不了myXls输出的XLS文件!不知道是不是自己没搞清两个类库的原因,总之觉得有点遗憾。

NPOI是.Net平台下的POI,目前稳定版是一个能够生成真正的Excel文件并实现读写的开源项目,项目地址是http://npoi.codeplex.com/。功能有输入输出,公式运算,单元格的高级样式等等,其中包含的类库有:

NPOI.Util 1.2.1 Basic assistant class library
NPOI.POIFS 1.2.1 OLE2 format read/write library
NPOI.DDF 1.2.1 Drawing format read/write library
NPOI.SS 1.2.1 Formula evaluation library
NPOI.HPSF 1.2.1 Summary Information and Document Summary Information read/write library
NPOI.HSSF 1.2.1 Excel BIFF format read/write library

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
//引用
using NPOI.HSSF.UserModel;
using NPOI.HPSF;
using NPOI.POIFS.FileSystem;
 
namespace OracleKity
{
    class DataTableExcel
    {
        public bool DataTableToExcel(System.Data.DataTable dtSource, string filePath)
        {
            try
            {
                //文档仅写入一个sheet
                //建立一个workbook
                HSSFWorkbook workbook = new HSSFWorkbook();
                System.Data.DataTable dt = dtSource;
                //建立sheet
                HSSFSheet sheet = workbook.CreateSheet("sheet1");
                //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
                HSSFCellStyle textStyle = workbook.CreateCellStyle();
                textStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
 
                //用column name 作为列名
                List columns = new List();
                for (int colIndex = 0; colIndex < dt.Columns.Count; colIndex++)
                {
                    string name = dt.Columns[colIndex].ColumnName;
                    HSSFCell cell = sheet.CreateRow(0).CreateCell(colIndex);
                    cell.SetCellValue(name);
                    cell.CellStyle = textStyle;
                    columns.Add(name);
                }
 
                //建立内容列
                for (int row = 0; row < dt.Rows.Count; row++)
                {
                    DataRow dr = dt.Rows[row];
                    for (int col = 0; col < columns.Count; col++)
                    {
                        string data = dr[columns[col]].ToString();
                        HSSFCell cell = sheet.CreateRow(row + 1).CreateCell(col);
                        cell.SetCellValue(data);
                        cell.CellStyle = textStyle;
                    }
                }
                //写Excel
                FileStream file = new FileStream(filePath, FileMode.OpenOrCreate);
                workbook.Write(file);
                file.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public System.Data.DataTable ReadExcelToDataTable(string filePath)
        {
            //打开要读取的Excel
            FileStream file = new FileStream(filePath, FileMode.Open);
            //读入Excel
            HSSFWorkbook workbook = new HSSFWorkbook(file);
            file.Close();
            HSSFSheet sheet = workbook.GetSheetAt(0);
            //建立一个新的table
            DataTable dtNew = new DataTable(); ;
            HSSFRow row = sheet.GetRow(0);
            //读取取第0列作为column name
            for (int columnIndex = 0; columnIndex < row.LastCellNum; columnIndex++)
            {
                DataColumn dc = new DataColumn(row.GetCell(columnIndex).ToString());
                dtNew.Columns.Add(dc);
            }
            int rowId = 1;
            //第一列以后为资料,一直读到最后一行
            while (rowId <= sheet.LastRowNum)
            {
                DataRow newRow = dtNew.NewRow();
                //读取所有column
                for (int colIndex = 0; colIndex < dtNew.Columns.Count; colIndex++)
                {
                    newRow[dtNew.Columns[colIndex]] = sheet.GetRow(rowId).GetCell(colIndex).ToString();
                }
                dtNew.Rows.Add(newRow);
                rowId++;
            }
            return dtNew;
 
        }
 
    }
}

如果正好要做EXCEL的输入输出,不妨考虑一下使用哪一个Library。

上一篇:《中国人工智能学会通讯》——4.22 让机器具有视觉注意功能


下一篇:我的博客pdf文档集合