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

.Net代码志 ,2010-03-03 17:17 ,ming

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

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

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

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。

已经有3个人留下评论。

  1. 静脉曲张 说:

    来顶下博客,很喜欢你的博客风格,呵呵,很大气,很有感觉,努力吧。
    静脉曲张(www.zgxbg.com)

  2. xiaochong 说:

    网站很清新啊!最近一直在思考换个什么样的主题呢。

踩博就要像铲除敌人一样毫不留情!