public bool init(EXCEL.Worksheet v_workSheet, IndexSheetData v_indexData, int v_rowBegin = 3) { indexData = v_indexData; sheetName = v_indexData.sheetName; EXCEL.Cells datas = v_workSheet.Cells; header = new _SheetHeader(datas, v_indexData.shildKeys, v_indexData.dataOffX, v_indexData.dataOffY); data = new List <CellValue[]>(); for (int row = v_indexData.dataOffY + v_rowBegin; row < 100000; row++) { if (datas[row, 0].Value == null) { break; } CellValue[] row_data = new CellValue[header.HeaderLen]; for (int colidx = 0; colidx < header.HeaderLen; colidx++) { int col = colidx + v_indexData.dataOffX; try { row_data[colidx] = header.checkData(col, datas[row, colidx]); } catch (Exception ex) { Debug.Error("在导出表\"{0}\" sheet \"{1}\" 单元格{2}{3}时发生了错误,单元格内容为{4},错误信息如下:\r\n{5}", v_workSheet.Workbook.FileName, v_indexData.sheetName, Tools.getColName(col), row + 1, datas[row, colidx].Value, ex.ToString()); return(false); } } //添加注释数据 if (header.Note != null) { CellValue noteCell = row_data[header.Note.Col]; if (noteCell is MissVal) { notes.Add(null); } else { notes.Add(noteCell.ToString()); } } else { notes.Add(null); } data.Add(row_data); } return(true); }
public void apposeReadExcel(string v_filePath) { Excel.Workbook book = new Excel.Workbook(v_filePath); Excel.Worksheet index_sheet = book.Worksheets["INDEX"]; if (index_sheet == null) { __old_readExcel(v_filePath); return; } //读取索引表 List <IndexSheetData> indexes = new List <IndexSheetData>(); SheetHeader index_header = new SheetHeader(); index_header.readHeader(index_sheet); Excel.Cells datas = index_sheet.Cells; for (int i = 1; i < 100; i++) { try { if (datas[i, 0].Value == null || string.IsNullOrEmpty(datas[i, 0].Value.ToString())) { break; } IndexSheetData the_index = new IndexSheetData(); the_index.init(datas, i, index_header); indexes.Add(the_index); } catch (Exception ex) { Debug.Error("{0}读取索引列,第{1}行时报错,报错信息如下\r\n{2}", Path.GetFileName(v_filePath), i + 2, ex.ToString()); return; } } Dictionary <string, ExcelToMapData>[] table_memo = new Dictionary <string, ExcelToMapData> [2]; Dictionary <string, ExportSheetBin>[] sheetBin_memo = new Dictionary <string, ExportSheetBin> [2]; string[] root_pathes = { Config.cliPath, Config.servPath }; int[] optCode = { 1, 2 }; for (int i = 0; i < table_memo.Length; i++) { table_memo[i] = new Dictionary <string, ExcelToMapData>(); sheetBin_memo[i] = new Dictionary <string, ExportSheetBin>(); } //根据索引表读取各sheet foreach (IndexSheetData curIndex in indexes) { if (!curIndex.isOpt) { continue; } Excel.Worksheet curSheet = book.Worksheets[curIndex.sheetName]; if (curSheet == null) { Debug.Error("{0}没有找到sheet[{1}]", Path.GetFileName(v_filePath), curIndex.sheetName); return; } ExportSheetBin sheetBin = new ExportSheetBin(); try { if (!sheetBin.init(curSheet, curIndex)) { return; } } catch (Exception ex) { Debug.Error("{0}_[{1}]导出基础数据时出现错误,错误信息为:\r\n{2}", Path.GetFileNameWithoutExtension(v_filePath), curIndex.sheetName, ex.ToString()); return; } //根据服务端的文件名,创建获取luamap string[] file_names = { curIndex.optCliFileName, curIndex.optSrvFileName }; for (int i = 0; i < table_memo.Length; i++)//客户端服务端各生成一遍 { if (string.IsNullOrEmpty(file_names[i])) { continue; } if (!table_memo[i].ContainsKey(file_names[i])) { ExcelMapData new_map = new ExcelMapData(); bool isDataPersistence = curIndex.isDataPersistence && (!string.IsNullOrEmpty(curIndex.optCliFileName)) && curIndex.optCliFileName.EndsWith(".lua"); ExcelToMapData new_data = new ExcelToMapData(new_map, isDataPersistence, Path.GetFileNameWithoutExtension(file_names[i])); table_memo[i].Add(file_names[i], new_data); } if (!sheetBin_memo[i].ContainsKey(file_names[i])) { sheetBin_memo[i].Add(file_names[i], sheetBin); } ExcelToMapData root_table = table_memo[i][file_names[i]]; root_table.add_sheetbin(sheetBin); //把表中的数据读取到lua map里 //应当把这里的逻辑改为,把表中数据读到一个map_data中,而后转到各语言的结构中 try { sheetBin.getExportMap(root_table._data, optCode[i]); } catch (Exception ex) { Debug.Error("在装载【{0}】数据到中间结构时发生错误,错误信息是{1}", curIndex.sheetName, ex.ToString()); } } } //这里应当写为,根据后缀名导出不同的语言 for (int i = 0; i < table_memo.Length; i++) { foreach (var cur_pair in table_memo[i]) { string opt_path = root_pathes[i] + cur_pair.Key; OptData optData = null; ExportSheetBin cur_sheetBin = sheetBin_memo[i][cur_pair.Key]; ELanguage optLanguage = cur_sheetBin.indexData.getOptLanguage(i); bool skip = false; switch (optLanguage) { case ELanguage.lua: optData = LuaExporter.getExportContent(cur_pair.Value, optCode[i]); break; case ELanguage.json: optData = JsonExporter.getExportContent(cur_pair.Value, optCode[i]); break; case ELanguage.xml: Debug.Error("xml导出未实现"); break; case ELanguage.none: skip = true; break; default: Debug.Error("未知导出语言"); break; } if (!skip) { if (optData.errList.Count > 0) { Debug.Error(string.Format("在导出{0}时发生错误:\r\n", cur_pair.Key) + optData.getErrInfo()); return; } File.WriteAllText(opt_path, optData.content); //todo 如果是Realease模式,把文件输出到一个临时文件夹,调用LUAC,把lua文件生成到配置的cli文件夹中。 } } } Debug.Info("{0}:导表完成~~~", Path.GetFileName(v_filePath)); }