private static void TableDataToBinary(ProtoFileConvertor convertor) { string path = srcFolderPath + "/" + convertor.SrcFileName; FileStream stream = File.Open(path, FileMode.Open, FileAccess.Read); IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream); DataSet result = excelReader.AsDataSet(); DataTable table = result.Tables[0]; int columns = table.Columns.Count; int rows = table.Rows.Count; TableEncoder encoder = new TableEncoder(); encoder.WriteInt(rows - 1); //先标记数据量,其中第一行作为标记是不应该算进去的 for (int i = 1; i < rows; i++) { for (int j = 0; j < columns; j++) { //第一行当用来当这一列的字段名称 string fieldName = table.Rows[0][j].ToString(); if (convertor.propertyTableMap.ContainsKey(fieldName)) { object data = table.Rows[i][j]; //Debug.Log(data.ToString()); string dataType = convertor.propertyTableMap[fieldName]; #region 根据字段类型Encode if (dataType == CommmonVarType.INT16) { encoder.WriteShort((short)Convert.ChangeType(data, typeof(short))); } else if (dataType == CommmonVarType.INT32) { encoder.WriteInt((int)Convert.ChangeType(data, typeof(int))); } else if (dataType == CommmonVarType.INT64) { encoder.WriteInt64((long)Convert.ChangeType(data, typeof(long))); } else if (dataType == CommmonVarType.INT8 || dataType == CommmonVarType.UINT8) { encoder.WriteByte((byte)Convert.ChangeType(data, typeof(byte))); } else if (dataType == CommmonVarType.STRING) { encoder.WriteString(data as string); } else if (dataType == CommmonVarType.UINT16) { encoder.WriteUShort((ushort)Convert.ChangeType(data, typeof(ushort))); } else if (dataType == CommmonVarType.UINT32) { encoder.WriteUInt((uint)Convert.ChangeType(data, typeof(uint))); } else if (dataType == CommmonVarType.UINT64) { encoder.WriteUInt64((ulong)Convert.ChangeType(data, typeof(ulong))); } else { Debug.LogError("未实现解析类型" + dataType); } #endregion } else { Debug.LogError("跳过"); } } } byte[] fileBytes = encoder.listBytes.ToArray(); string binPath = tableDataOutputPath + "/" + convertor.destClassName + ".kiss"; File.WriteAllBytes(binPath, fileBytes); }
private static void TableDataToBinary(ProtoFileConvertor convertor) { string path = srcFolderPath +"/"+ convertor.SrcFileName; FileStream stream = File.Open(path, FileMode.Open, FileAccess.Read); IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream); DataSet result = excelReader.AsDataSet(); DataTable table = result.Tables[0]; int columns = table.Columns.Count; int rows = table.Rows.Count; TableEncoder encoder=new TableEncoder(); encoder.WriteInt(rows-1); //先标记数据量,其中第一行作为标记是不应该算进去的 for (int i = 1; i < rows; i++) { for (int j = 0; j < columns; j++) { //第一行当用来当这一列的字段名称 string fieldName = table.Rows[0][j].ToString(); if (convertor.propertyTableMap.ContainsKey(fieldName)) { object data = table.Rows[i][j]; //Debug.Log(data.ToString()); string dataType = convertor.propertyTableMap[fieldName]; #region 根据字段类型Encode if (dataType == CommmonVarType.INT16) { encoder.WriteShort((short)Convert.ChangeType(data, typeof(short))); } else if (dataType == CommmonVarType.INT32) { encoder.WriteInt((int)Convert.ChangeType(data,typeof(int))); } else if (dataType == CommmonVarType.INT64) { encoder.WriteInt64((long)Convert.ChangeType(data, typeof(long))); } else if (dataType == CommmonVarType.INT8 || dataType == CommmonVarType.UINT8) { encoder.WriteByte((byte)Convert.ChangeType(data,typeof(byte))); } else if (dataType == CommmonVarType.STRING) { encoder.WriteString(data as string); } else if (dataType == CommmonVarType.UINT16) { encoder.WriteUShort((ushort)Convert.ChangeType(data, typeof(ushort))); } else if (dataType == CommmonVarType.UINT32) { encoder.WriteUInt((uint)Convert.ChangeType(data, typeof(uint))); } else if (dataType == CommmonVarType.UINT64) { encoder.WriteUInt64((ulong)Convert.ChangeType(data, typeof(ulong))); } else { Debug.LogError("未实现解析类型" + dataType); } #endregion } else { Debug.LogError("跳过"); } } } byte[] fileBytes = encoder.listBytes.ToArray(); string binPath = tableDataOutputPath + "/" + convertor.destClassName + ".kiss"; File.WriteAllBytes(binPath,fileBytes); }