(转)koogra--Excel文件读取利器
koogra是一個(gè).net平臺(tái)下開源的excel讀取程序,可以在開源社區(qū)下載它。使用它我們無需office就可以讀取excel文件。盡管這個(gè)程序已經(jīng)停止了更新,但是它還是很好用的。下面介紹怎么使用它。
下載到該程序的源代碼,編譯生成Net.SourceForge.Koogra.dll。在項(xiàng)目中引用該dll,using Net.SourceForge.Koogra.Excel;
Workbook wb = new Workbook(path);path是文件的物理路徑,這可以創(chuàng)建一個(gè)excel文件對象
Worksheet xSheet = xBook.Sheets[0];引用Workbook 的工作表
xBook.Sheets.GetByName(string)還可以通過這個(gè)方法獲取對Workbook 工作表的引用。
xBook.Sheets.Rows[i]對excel行的引用
xBook.Sheets.Rows[i].Cells[i]對單元格的引用
xSheet.Rows.FirstRow 首行的行號,從0開始
xSheet.Rows.LastRow?? 尾行的行號,對于中間有空行,可以用xSheet.Rows[i]==null判斷
Cells對應(yīng)的也有FirstCol,LastCol屬性,對于Cells為NUll的情況下不能使用Cells.Value
下面是一個(gè)例子:
?? /// <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;
?? }
更多的功能有待大家去發(fā)掘,呵呵~反正是C#寫的,源代碼也有,慢慢看吧。此外另一個(gè)開源的東東:myxls支持excel的讀寫,現(xiàn)在依然在更新,也是很不錯(cuò)的。
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.調(diào)整日期的默認(rèn)格式
讀取 excel 裏的日期資料,譬如 2007/3/5
用cell.FormattedValue() 會(huì)取得字串 "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的話-------------------
今天發(fā)現(xiàn)koogra已經(jīng)更新到3.1.1版。只有dll文件。不過可以用reflector反編譯得到源代碼。
項(xiàng)目地址:koogra
轉(zhuǎn)載于:https://www.cnblogs.com/codeyu/archive/2009/07/09/1520104.html
總結(jié)
以上是生活随笔為你收集整理的(转)koogra--Excel文件读取利器的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 绿宝石多少钱一克啊?
- 下一篇: 黄山风景区现在需要预约吗