/// <summary> /// 加载数据表数据 /// </summary> public void LoadData() { string tmppath = "./Bytes/" + TableName; string path = $"{System.IO.Path.GetFullPath(tmppath)}.bytes"; byte[] buffer = IOUtil.GetFileBuffer(path); if (buffer == null) { System.Console.WriteLine($"{path} 不存在"); return; } // 解压 buffer = ZlibHelper.DecompressBytes(buffer); // 解密 buffer = SecurityUtil.Xor(buffer); using (BinaryStream bs = new BinaryStream(buffer)) { LoadList(bs); } }
/// <summary> /// 生成 bytes 数据文件 /// </summary> private void CreateData(string pathIn, ISheet sheet) { using (BinaryStream bs = new BinaryStream()) { int rows = sheet.LastRowNum + 1; int cols = sheet.GetRow(sheet.LastRowNum).LastCellNum; // 计算表格实际行数 int realRows = 0; for (int i = 0; i < rows; i++) { IRow row = sheet.GetRow(i); if (row == null) { continue; } realRows++; } if (realRows < 3) { Console.WriteLine($"{pathIn} 异常。"); return; } bs.WriteInt(realRows - 3); //实际的数据条数 bs.WriteInt(cols); string[,] tableHeadArr = new string[cols, 3]; for (int i = 0; i < rows; i++) { IRow row = sheet.GetRow(i); if (row == null) { continue; } for (int j = 0; j < cols; j++) { ICell cell = row.GetCell(j, MissingCellPolicy.CREATE_NULL_AS_BLANK); if (i < 3) { tableHeadArr[j, i] = cell.ToString(); } else { string type = tableHeadArr[j, 1]; switch (type.ToLower()) { case "int": bs.WriteInt(cell.ToInt()); break; case "long": bs.WriteLong(cell.ToLong()); break; case "short": bs.WriteShort(cell.ToShort()); break; case "float": bs.WriteFloat(cell.ToFloat()); break; case "byte": bs.WriteByte(cell.ToByte()); break; case "bool": bs.WriteBool(cell.ToBool()); break; case "double": bs.WriteDouble(cell.ToDouble()); break; default: bs.WriteUTF8String(cell.ToString()); break; } } } } // xor加密 byte[] buffer = SecurityUtil.Xor(bs.ToArray()); // zlib压缩 buffer = ZlibHelper.CompressBytes(buffer); // 写入文件 string tableName = Path.GetFileNameWithoutExtension(pathIn); using (FileStream fs = new FileStream($"{PathOut}/Bytes/{tableName}.bytes", FileMode.Create)) { fs.Write(buffer, 0, buffer.Length); Console.WriteLine($"生成 {tableName}.bytes 完毕 ..."); } } }