public static StreamWriter OpenCsvFile(string msg_name, bool isNew) { string path = ParseManager.GetInstance().config.csvPath + msg_name + ".csv"; StreamWriter file; try { var encoding = new UTF8Encoding(true); var append = isNew ? false : true; file = new StreamWriter(path, append, encoding); } catch (Exception e) { Console.WriteLine("OpenCsvFile failed:msg_name=" + msg_name + ",error=" + e.Message); return(null); } return(file); }
public virtual bool Init() { try { var cfg = ParseManager.GetInstance().config; engine = Python.CreateEngine(); var paths = engine.GetSearchPaths(); //paths.Add(@"D:\Program Files (x86)\IronPython 2.7\Lib"); string pythonPath = ParseManager.GetInstance().GetPythonPath(); paths.Add(pythonPath + "Lib"); paths.Add(pythonPath); engine.SetSearchPaths(paths); scope = engine.CreateScope(); script = engine.CreateScriptSourceFromFile(pythonPath + scriptName); script.Execute(scope); return(true); } catch (Exception e) { Console.WriteLine("PythonScript error:message=" + e.Message); } return(true); }
public static void ParseExcel(string[] args) { if (args.Length >= 1) { var path = args[0]; ParseConfig tempConfig = null; try { tempConfig = ReadConfig(path); } catch (Exception e) { Console.WriteLine("读取配置发生错误:path=" + path + ",error=" + e.Message); return; } if (tempConfig == null) { Console.WriteLine("读取配置" + path + "失败"); return; } GetInstance().config = tempConfig; } var config = GetInstance().config; List <string> excels = new List <string>(); if (args.Length >= 2) { //指定了excel excels.Add(args[1]); } else if (args.Length >= 1) { var dir = new DirectoryInfo(config.excelPath); foreach (var file in dir.GetFiles()) { if (file.Extension == ".xlsx" && !config.ignoreFiles.Contains(file.Name)) { excels.Add(file.FullName); } } } else { excels.Add("test_python_config.xlsx"); } //第一步:生成proto数据 foreach (var fileName in excels) { if (!ExcelParseManager.ParseExcel(fileName, true)) { Console.WriteLine("parse excel proto failed: " + fileName); return; } else { Console.WriteLine("parse excel proto sucess: " + fileName); } } //第二步:生成proto文件 if (!ParseManager.GetInstance().GenerateProto()) { Console.WriteLine("生成proto文件失败"); return; } //第三步:解析数据 foreach (var fileName in excels) { if (!ExcelParseManager.ParseExcel(fileName, false)) { Console.WriteLine("parse excel data failed: " + fileName); return; } else { Console.WriteLine("parse excel data sucess: " + fileName); } } }
public static bool ParseExcel(string path, bool generalProto) { string fileName = System.IO.Path.GetFileNameWithoutExtension(path).ToLower(); var workbook = new Workbook(path); var sh = workbook.Worksheets["数据表"]; if (sh == null) { Console.WriteLine("发现错误,文件", path + "找不到数据表"); return(false); } int rowCount = sh.Cells.MaxDataRow; if (rowCount < 2) { Console.WriteLine("发现错误,文件", path + "不足3行"); return(false); } if (generalProto) { var descList = GetLineData(sh, 0); var nameList = GetLineData(sh, 1); var typeList = GetLineData(sh, 2); if (descList.Count <= 0) { Console.WriteLine("发现错误,文件", path + "空的配置"); return(false); } var tempFile = OpenCsvFile(fileName, true); if (null == tempFile) { Console.WriteLine("发现错误,OpenCsvFile失败,msg_name=" + fileName); return(false); } WriteCsvLine(tempFile, descList); WriteCsvLine(tempFile, nameList); WriteCsvLine(tempFile, typeList); tempFile.Close(); if (!ParseManager.GetInstance().BeginMessage(fileName, path, descList, nameList, typeList)) { return(false); } return(true); } if (!ParseManager.GetInstance().ImportPath(fileName)) { return(false); } var file = OpenCsvFile(fileName, false); if (null == file) { Console.WriteLine("发现错误,OpenCsvFile失败,msg_name=" + fileName); return(false); } for (int row = 3; row <= sh.Cells.MaxDataRow; ++row) { var lineData = GetLineData(sh, row); WriteCsvLine(file, lineData); if (!ParseManager.GetInstance().AddLineData(row + 1, lineData)) { return(false); } } file.Close(); if (!ParseManager.GetInstance().EndMessage()) { return(false); } return(true); }