(转)koogra--Excel文件读取利器
koogra是一个.net平台下开源的excel读取程序,可以在开源社区下载它。使用它我们无需office就可以读取excel文件。尽管这个程序已经停止了更新,但是它还是很好用的。下面介绍怎么使用它。
下载到该程序的源代码,编译生成Net.SourceForge.Koogra.dll。在项目中引用该dll,using Net.SourceForge.Koogra.Excel;
Workbook wb = new Workbook(path);path是文件的物理路径,这可以创建一个excel文件对象
Worksheet xSheet = xBook.Sheets[0];引用Workbook 的工作表
xBook.Sheets.GetByName(string)还可以通过这个方法获取对Workbook 工作表的引用。
xBook.Sheets.Rows[i]对excel行的引用
xBook.Sheets.Rows[i].Cells[i]对单元格的引用
xSheet.Rows.FirstRow 首行的行号,从0开始
xSheet.Rows.LastRow 尾行的行号,对于中间有空行,可以用xSheet.Rows[i]==null判断
Cells对应的也有FirstCol,LastCol属性,对于Cells为NUll的情况下不能使用Cells.Value
下面是一个例子:
/// <summary>
/// This method just exercises the excel workbook data.
/// </summary>
/// <param name="path">The path to the workbook.</param>
private Workbook DumpWorkbookToConsole(string path)
{
// print the path
Console.WriteLine(path);
// construct our workbook
Workbook wb = new Workbook(path);
// dump the worksheet data
foreach(Worksheet ws in wb.Sheets)
{
Console.Write("Sheet is ");
Console.Write(ws.Name);
Console.Write(" First row is: ");
Console.Write(ws.Rows.FirstRow);
Console.Write(" Last row is: ");
Console.WriteLine(ws.Rows.LastRow);
// dump cell data
for(int r = ws.Rows.FirstRow; r <= ws.Rows.LastRow; ++r)
{
Row row = ws.Rows[(ushort)r];
if(row != null)
{
Console.Write("Row: ");
Console.Write(r);
Console.Write(" First Col: ");
Console.Write(row.Cells.FirstCol);
Console.Write(" Last Col: ");
Console.WriteLine(row.Cells.LastCol);
for(int c = row.Cells.FirstCol; c <= row.Cells.LastCol; ++c)
{
Cell cell = row.Cells[(byte)c];
Console.Write("Col: ");
Console.Write(c);
if(cell != null)
{
Console.Write(" Value: ");
Console.Write(cell.Value);
Console.Write(" Formatted Value: ");
Console.WriteLine(cell.FormattedValue());
}
else
Console.WriteLine(" null");
}
}
}
}
return wb;
}
更多的功能有待大家去发掘,呵呵~反正是C#写的,源代码也有,慢慢看吧。此外另一个开源的东东:myxls支持excel的读写,现在依然在更新,也是很不错的。
koogra一些修正:
1. 修正中文工作表名乱码的BUG
將 \Excel\Records\BoundSheetRecord.cs 34~38行
ushort nameLen = reader.ReadUInt16();
StringBuilder nb = new StringBuilder(nameLen);
nb.Append(new string(reader.ReadChars(nameLen)));
_name = nb.ToString();
改成
ushort nameLen = (ushort)reader.ReadByte();
bool compressed = (reader.ReadByte() * 0x01) == 0;
if (!compressed) {
nameLen *= 2;
}
byte[] charBytes = reader.ReadBytes(nameLen);
if (compressed) {
//decompress
byte[] wideBytes = new byte[charBytes.Length * 2];
for (int i = 0; i < charBytes.Length; i++)
wideBytes[2 * i] = charBytes[i];
charBytes = wideBytes;
}
_name = new string(Encoding.Unicode.GetChars(charBytes));
2.调整日期的默认格式
讀取 excel 裏的日期資料,譬如 2007/3/5
用cell.FormattedValue() 會取得字串 "3/5/07"
那通常不是我想要的,所以我修改了原本的 Cell.cs
public string FormattedValue() {
...
...
// get the format string
string formatString = format.FormatValue;
+if (formatString == "M/D/YY") {
+ formatString = "yyyy/MM/dd";
+}
-----------------本bloger的话-------------------
今天发现koogra已经更新到3.1.1版。只有dll文件。不过可以用reflector反编译得到源代码。
项目地址:koogra
转载于:https://www.cnblogs.com/codeyu/archive/2009/07/09/1520104.html
(转)koogra--Excel文件读取利器相关推荐
- java struts2 excel上传_文件上传方法,使用Struts2,实现Excel文件读取并写入数据库技术...
文件上传方法,使用Struts2,实现Excel文件读取并写入数据库技术 如题:文件信息的批量导入-- 项目中经常会遇到客户的一些单表信息的数据批量导入,也就是提供定制Excel表,再把Excel表中 ...
- Python 办公效率化学习(自学)三.Excel文件读取
目录 Excel文件读取 Day1 一.步骤解析(以统计火龙果可乐为例) 二.具体操作 1.读取单个月份Excel表格数据 (1)首先学习Excel基本结构: (2)分析得出需要的步骤 : (3)前提 ...
- 超大Excel文件读取(支持50w+)(三)
超大Excel文件读取(支持50w+) 1 线上内存溢出问题演示 环境准备 准备一个大的excel文件(xlsx大小10M及以上) 将jvm的heap缩小到500m(JVM 参数 -Xmx500m)用 ...
- Python之Excel文件读取
今天研究了一下Python对Excel文件的读取,在此分享一下. 1.模块安装 想通过python对Excel文件进行处理,需要安装以下几个模块: pip install xlrd pip insta ...
- python实现Excel文件读取的程序(附源代码)
python实现Excel文件读取的程序 前一段时间帮一个朋友用python写了一个读Excel程序操作的程序,具体要求为:读取两个Excel文件,根据其中某个特征的特征值对这两个文件进行取交集操 ...
- C#Excel文件读取问题及解决办法
最近由于工作上用到了读取Excel操作,完成过程中遇到了各种各样的问题,最后为了以后读取Excel时不再如此的麻烦,特意做成了一个小组件,方便日后使用.现在总结一下过程中遇到的问题及相应的解决办法. ...
- kettle读取json文件并读取数据_Labview打开Excel文件读取数据
Labview有几种打开excel的方式,各有利弊. 大水怪出没请注意:Labview使用Excel处理数据(打开Excel)zhuanlan.zhihu.com 这一篇介绍了通过使用Active打 ...
- Excel 文件读取
/// <summary> /// 读取Excel数据 /// </summary> private void NewMethod( ...
- python文件处理系列(二):Excel文件读取库xlwings
一.xlwings概述 1.xlwings特点 xlwings能够非常方便的读写Excel文件中的数据,并且能够进行单元格格式的修改 可以和matplotlib以及pandas无缝连接 可以调用Exc ...
最新文章
- 35.2. Subversion 版本控制
- Castle IOC容器实践之EnterpriseLibrary Configuration Facility
- 净水器选购指南,如何挑选家用净水器
- Flutter:使用 CustomClipper 绘制 N 角星
- JAVA程序开发----网络编程
- 关于Unity3D中函数说明
- anaconda moviepy_Anaconda和PyCharm的详细安装步骤~小白专用,手把手教学
- TTButton 的正确使用的方法
- mysql2008安装失败_SQL Server 2008 安装失败问题总结
- 网络工程师十个常见面试问题
- Win32多语言IME应用程序编程接口(API)
- java用户注册信息校验
- 恒指赵鑫:06.13今日实盘喊单记录与小结
- 如何在Photoshop里抠头发丝
- Linux rpm 命令 【转】
- AI自动修复图片网站
- js 倒计时算法及定时器的应用
- PBX220评测报告
- 湖南发现外星智慧创造人类证据(图)
- 小米电视更换默认桌面和优化
热门文章
- mysql loop嵌套_MySQL中Nested-Loop Join算法小结
- java游戏脱逃_App Store 上的“冒险的逃离筏船”
- android 动画x轴旋转,Android Roate3dAnimation实现围绕y轴竖直方向或者绕x轴方向旋转的3d动画效果...
- 图像处理怎么学matlab,Matlab数字图像处理学习(1)-亮度变换
- shell脚本求和_【零基础学云计算】Shell编程之case语句与循环语句
- html5表单实例元素,HTML5新表单元素的图文实例-
- animation动画不生效_关于CSS3的animation使用的一些坑,需要注意下!
- c++中求字符串数组的min/max
- 训练LaneATT遇到CUDA_HOME环境变量问题
- Keras Lambda层