都半年沒寫博客了,自從上個項目開始,感受時間好緊迫。在這感慨下[自從用了unity以後,代碼架構能力愈來愈差。unity的一切對象都是component,要惡補coding]。架構
廢話不說了。今天記錄下用NPOI將excel寫入二進制。unity中讀取二進制內容。 go......excel
NPOI 不用多介紹了,一年前用過一次作打印,都是不深刻的使用。component
首先是規定好策劃配置表對象
前四行爲策劃/程序對應表,四行以後爲可用數據。ip
//ReadExcel => WriteData ......文檔
/// <summary>
/// 讀取excel生出.gd二進制文件
/// </summary>
/// <param name="path">Excel文件路徑</param>
/// <returns></returns>
public bool WriteToFile(string path)
{
if (Directory.Exists(writeToPath) == false)
{
Directory.CreateDirectory(writeToPath);
}get
try
{
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
IWorkbook wk = new HSSFWorkbook(fs);
for (int i = 0; i < wk.NumberOfSheets; i++)
{
ISheet sheet = wk.GetSheetAt(i);
using (FileStream fsWrite = new FileStream(writeToPath + sheet.SheetName + ".gd", FileMode.Create, FileAccess.Write))
{
using (BinaryWriter bw = new BinaryWriter(fsWrite))
{
//Console.WriteLine(sheet.SheetName);
//Console.WriteLine(sheet.LastRowNum);
if (sheet.LastRowNum == 0)
{
GameEvtMgr.Debug(sheet.SheetName + "爲空,請檢查文檔內容"); //可刪除已生成的.gd文件
continue;
}
GameEvtMgr.Debug("表名稱:" + sheet.SheetName + " 共計:" + (sheet.LastRowNum + 1 - initLength) + "行");
bw.Write(sheet.LastRowNum + 1 - initLength); //寫入當前sheet中rows.length 默認sheet.LastRowNum + 1 -4(excel前四行); 博客
for (int j = 0; j <= sheet.LastRowNum; j++)
{string
IRow row = sheet.GetRow(j);
//第一行:字段行 客戶端取值對照
//第二行:字段描述行 skip
//第三行:數據類型行 [byte short int string boolean ...long]
//第四行:中文描述 skipit
IRow rows = sheet.GetRow(2); //取excel中第三行類型value,寫入值時參考
if (rows != null)
{
}
if (j >= 4)
{
if (row != null)
{
for (int z = 0; z < row.LastCellNum; z++)
{
//Console.WriteLine(row.FirstCellNum + " " + row.LastCellNum);
ICell cell = row.GetCell(z);
if (cell != null)
{
BWriteByType(bw, rows.GetCell(z).StringCellValue, cell); //以類型rows作類型寫入參考
}
}
}
}
}
}
}
}
}
}
catch (Exception ex)
{
//Console.WriteLine(ex.Message);
GameEvtMgr.Debug(ex.Message);
return false;
}
GameEvtMgr.Debug("End");
return true;
}
public void BWriteByType(BinaryWriter writer, string value, ICell cell)
{
switch (value)
{
case "byte":
writer.Write((byte)cell.NumericCellValue);
break;
case "short":
writer.Write((short)cell.NumericCellValue);
break;
case "int":
writer.Write((int)cell.NumericCellValue);
break;
case "string":
writer.Write(cell.ToString());
break;
case "bool":
writer.Write(cell.NumericCellValue == 1 ? true : false);
break;
case "long":
writer.Write((long)cell.NumericCellValue);
break;
default:
//throw new Exception("excel 類型錯誤");
GameEvtMgr.Debug("Excel類型有誤");
break;
}
}
//讀取數據 => 首先的寫一個類,字段都對應表中每一個字段名。方便客戶端調用數據
private void ReadBattle02Data()
{
Battle02 bt = new Battle02();
using (FileStream fs = new FileStream(writeToPath + "Battle02.gd", FileMode.Open, FileAccess.Read))
{
using (BinaryReader br = new BinaryReader(fs))
{
int count = br.ReadInt32();
for (int i = 0; i < count; i++)
{
bt.ID = br.ReadInt32();
bt.Audio = br.ReadString();
bt.Prefab = br.ReadString();
bt.IsOpen = br.ReadBoolean();
Console.WriteLine(bt.ID + " " + bt.Audio + " " + bt.Prefab + " " + bt.IsOpen);
}
}
}
}
class Battle02
{
private int id;
private string audio;
private string prefab;
private bool isOpen;
public int ID
{
get { return id; }
set { id = value; }
}
public string Audio
{
get { return audio; }
set { audio = value; }
}
public string Prefab
{
get { return prefab; }
set { prefab = value; }
}
public bool IsOpen
{
get { return isOpen; }
set { isOpen = value; }
}
}
ok 讀取都ok。客戶端還需規劃規劃。下次再來補這個方式的注意事項以及優缺點。困的不行了^_^