Koogra 一套讀取Excel 的類別庫,雖然作者很久沒更新了,但它簡單好用讀取快速,是我讀取excel時最佳選擇,但今天碰到了一個問題,當 sheet's name 用中文命名時,用程式讀取sheet's name會變亂碼,還好他是開放原始碼,花了點時間看看,做了底下修改,至於為什麼我知道這樣改? 我是參考 MyXls 的原始碼,是另一套還在持續發展中有點bug但同時可寫/讀excel檔的類別庫。
將 \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));