unity中读写二进制文件

微笑、不失礼 提交于 2020-03-08 06:01:03

都半年没写博客了,自从上个项目开始,感觉时间好紧迫。在这感慨下[自从用了unity之后,代码架构能力越来越差。unity的一切对象都是component,要恶补coding]。

废话不说了。今天记录下用NPOI将excel写入二进制。unity中读取二进制内容。   go......

NPOI 不用多介绍了,一年前用过一次做打印,都是不深入的使用。

 

首先是规定好策划配置表

前四行为策划/程序对应表,四行之后为可用数据。

//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);
  }

  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++)
              {

                IRow row = sheet.GetRow(j);
                //第一行:字段行 客户端取值对照
                //第二行:字段描述行 skip
                //第三行:数据类型行 [byte short int string boolean ...long]
                //第四行:中文描述 skip

                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。客户端还需规划规划。下次再来补这个方式的注意事项以及优缺点。困的不行了^_^

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!