/// <summary> /// 获取工作簿,读取表头信息, /// </summary> /// <param name="Excel文件路径"></param> private void InitSheetInfo(ExcelScriptExportSetting exportSetting) { string excelFilePath = exportSetting.TaregetExcel; var readRule = exportSetting.readRule; using (var fs = new FileStream(excelFilePath, FileMode.Open, FileAccess.ReadWrite)) { var workBook = new XSSFWorkbook(fs); var firstSheet = workBook.GetSheetAt(0); // 约定只有第一个工作簿为有效导出源 SheetInfo = new ExcelSheetInfo(firstSheet); var chineseCommentRow = firstSheet.GetRow(readRule.ChineseCommentIndex); var englishCommentRow = firstSheet.GetRow(readRule.EnglishCommentIndex); var fieldDefineRow = firstSheet.GetRow(readRule.StrcutDefineIndex); //字段数 var fieldCount = chineseCommentRow.PhysicalNumberOfCells; for (var index = 0; index < fieldCount; index++) { try { var chineseComment = chineseCommentRow.GetCell(index).ToString(); var englishComment = englishCommentRow.GetCell(index).ToString(); var fieldDefineStr = fieldDefineRow.GetCell(index).ToString(); //字段类型 var fieldType = GetFieldDefine(fieldDefineStr); //字段备注 var fieldClassDesc = TryGetClassFieldDesc(fieldDefineStr); //分隔符 var arraySplit = TryGetArraySplit(fieldDefineStr); var fieldInfo = new ExcelFieldInfo( englishComment, chineseComment, fieldType, arraySplit, index, SheetInfo.EnglishId, fieldClassDesc); SheetInfo.AddFieldInfo(fieldInfo); } catch (Exception e) { throw new Exception($"在解析Excel文件{excelFilePath}的第{index}列时发生异常," + $"异常信息为{e.Message}!"); } } } }
public string CreateScript(ExcelScriptExportSetting exportSetting) { this.exportSetting = exportSetting; InitSheetInfo(exportSetting); if (projectInfo != null && !string.IsNullOrEmpty(projectInfo.ProjectScriptingDefines)) { Appender.AppendPrecomplie(projectInfo.ProjectScriptingDefines.Split(',')); } AppendScript(); if (projectInfo != null && !string.IsNullOrEmpty(projectInfo.ProjectScriptingDefines)) { Appender.AppendLine("#endif"); } var content = Appender.ToString(); IOUtility.WriteAllText($"{exportSetting.ExportDir}/{ScriptName}.cs", content); Dispose(); return(ScriptName); }