日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

(转)koogra--Excel文件读取利器

發布時間:2023/11/27 生活经验 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 (转)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文件读取利器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。