最近的一个项目中需要在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
001 | using System; |
002 | using System.Collections.Generic; |
003 | using System.Linq; |
004 | using System.Text; |
005 | using System.IO; |
006 | using System.Data; |
007 | //引用 |
008 | using NPOI.HSSF.UserModel; |
009 | using NPOI.HPSF; |
010 | using NPOI.POIFS.FileSystem; |
011 |
012 | namespace OracleKity |
013 | { |
014 | class DataTableExcel |
015 | { |
016 | public bool DataTableToExcel(System.Data.DataTable dtSource, string filePath) |
017 | { |
018 | try |
019 | { |
020 | //文档仅写入一个sheet |
021 | //建立一个workbook |
022 | HSSFWorkbook workbook = new HSSFWorkbook(); |
023 | System.Data.DataTable dt = dtSource; |
024 | //建立sheet |
025 | HSSFSheet sheet = workbook.CreateSheet( "sheet1" ); |
026 | //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看 |
027 | HSSFCellStyle textStyle = workbook.CreateCellStyle(); |
028 | textStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat( "@" ); |
029 |
030 | //用column name 作为列名 |
031 | List< string > columns = new List< string >(); |
032 | for ( int colIndex = 0; colIndex < dt.Columns.Count; colIndex++) |
033 | { |
034 | string name = dt.Columns[colIndex].ColumnName; |
035 | HSSFCell cell = sheet.CreateRow(0).CreateCell(colIndex); |
036 | cell.SetCellValue(name); |
037 | cell.CellStyle = textStyle; |
038 | columns.Add(name); |
039 | } |
040 |
041 | //建立内容列 |
042 | for ( int row = 0; row < dt.Rows.Count; row++) |
043 | { |
044 | DataRow dr = dt.Rows[row]; |
045 | for ( int col = 0; col < columns.Count; col++) |
046 | { |
047 | string data = dr[columns[col]].ToString(); |
048 | HSSFCell cell = sheet.CreateRow(row + 1).CreateCell(col); |
049 | cell.SetCellValue(data); |
050 | cell.CellStyle = textStyle; |
051 | } |
052 | } |
053 | //写Excel |
054 | FileStream file = new FileStream(filePath, FileMode.OpenOrCreate); |
055 | workbook.Write(file); |
056 | file.Close(); |
057 | return true ; |
058 | } |
059 | catch |
060 | { |
061 | return false ; |
062 | } |
063 | } |
064 | public System.Data.DataTable ReadExcelToDataTable( string filePath) |
065 | { |
066 | //打开要读取的Excel |
067 | FileStream file = new FileStream(filePath, FileMode.Open); |
068 | //读入Excel |
069 | HSSFWorkbook workbook = new HSSFWorkbook(file); |
070 | file.Close(); |
071 | HSSFSheet sheet = workbook.GetSheetAt(0); |
072 | //建立一个新的table |
073 | DataTable dtNew = new DataTable(); ; |
074 | HSSFRow row = sheet.GetRow(0); |
075 | //读取取第0列作为column name |
076 | for ( int columnIndex = 0; columnIndex < row.LastCellNum; columnIndex++) |
077 | { |
078 | DataColumn dc = new DataColumn(row.GetCell(columnIndex).ToString()); |
079 | dtNew.Columns.Add(dc); |
080 | } |
081 | int rowId = 1; |
082 | //第一列以后为资料,一直读到最后一行 |
083 | while (rowId <= sheet.LastRowNum) |
084 | { |
085 | DataRow newRow = dtNew.NewRow(); |
086 | //读取所有column |
087 | for ( int colIndex = 0; colIndex < dtNew.Columns.Count; colIndex++) |
088 | { |
089 | newRow[dtNew.Columns[colIndex]] = sheet.GetRow(rowId).GetCell(colIndex).ToString(); |
090 | } |
091 | dtNew.Rows.Add(newRow); |
092 | rowId++; |
093 | } |
094 | return dtNew; |
095 |
096 | } |
097 |
098 | } |
099 | } |
100 | </ string ></ string > |
如果正好要做EXCEL的输入输出,不妨考虑一下使用哪一个Library。