/// <summary> /// 导入 sheet 一行数据 /// </summary> /// <param name="column">一行几个数据</param> /// <param name="twoRow">提供数据类型的行</param> /// <param name="row">数据行</param> public void ImportOneLineData(int column, IRow twoRow, IRow row) { string rowStr = string.Empty; int Line_Start = DataUtil.DataKeywordToTypeNum(DataKeyword.Line_Start); DataBinaryWriter.Write(Line_Start); for (int i = 1; i < column; i++) { ICell data = row.GetCell(i); string dataType = twoRow.GetCell(i).StringCellValue; switch (dataType) { case DataKeyword.Int: if (data.CellType == CellType.Numeric) { int dataInt = (int)data.NumericCellValue; DataBinaryWriter.Write(dataInt); } else { Console.WriteLine("Int 数据错误"); } break; case DataKeyword.Float: if (data.CellType == CellType.Numeric) { float dataFlo = (float)data.NumericCellValue; DataBinaryWriter.Write(dataFlo); } else { Console.WriteLine("Float 数据错误"); } break; case DataKeyword.Double: if (data.CellType == CellType.Numeric) { double dataDou = data.NumericCellValue; DataBinaryWriter.Write(dataDou); } else { Console.WriteLine("Double 数据错误"); } break; case DataKeyword.String: if (data.CellType == CellType.String) { string dataStr = data.StringCellValue; DataBinaryWriter.Write(dataStr); } else if (data.CellType == CellType.Numeric) { string dataStr = data.NumericCellValue.ToString(); DataBinaryWriter.Write(dataStr); } else { Console.WriteLine("String 数据错误"); } break; case DataKeyword.Bool: if (data.CellType == CellType.Boolean) { bool dataBool = data.BooleanCellValue; DataBinaryWriter.Write(dataBool); } else { Console.WriteLine("Double 数据错误"); } break; } } }
/// <summary> /// 导入 excel 文件的一个 sheet /// </summary> /// <param name="sheet"></param> void ImportOneSheetData(ISheet sheet) { int rowCount = sheet.LastRowNum;//总行数 if (rowCount > 0) { IRow firstRow = sheet.GetRow(0); //第一行 IRow twoRow = sheet.GetRow(1); //第二行 int cellCount = firstRow.LastCellNum; //列数 string sheetName = firstRow.GetCell(0).StringCellValue; //表名 Enum_TableData += tabs + sheetName + "," + newline; string sheetCsPath = CsPathDir + sheetName + ".cs"; DeleteFile(sheetCsPath); DeclarationDescriptionInterface(firstRow, twoRow, sheetCsPath); CreatDataFileStream(DataPathDir + sheetName + ".data"); DataBinaryWriter.Write(sheetName); //填充行 for (int i = 2; i <= rowCount; ++i) { IRow row = sheet.GetRow(i); if (row == null) { continue; } ImportOneLineData(cellCount, twoRow, row); string strr = String.Empty; for (int j = row.FirstCellNum; j < cellCount; ++j) { ICell cell = row.GetCell(j); if (cell == null) { } else { switch (cell.CellType) { case CellType.Unknown: Console.WriteLine("未知"); break; case CellType.Numeric: strr += cell.NumericCellValue + "_"; break; case CellType.String: strr += cell.StringCellValue + "_"; break; case CellType.Formula: Console.WriteLine("公示"); break; case CellType.Blank: Console.WriteLine("空单元格"); break; case CellType.Boolean: Console.WriteLine("布尔"); break; case CellType.Error: Console.WriteLine("Error"); continue; } } } Console.WriteLine(strr); } int Table_End = DataUtil.DataKeywordToTypeNum(DataKeyword.Table_End); DataBinaryWriter.Write(Table_End); CloseDataFileStream(); } }