private void btnFillTemplate_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(txtExcelTemplatePath.Text.Trim())) { MessageBox.Show("请选择Excel模板文件!", "提示"); return; } WorkbookParameterContainer workbookParameter = ParseTemplate.Parse(txtExcelTemplatePath.Text); workbookParameter.Save(Path.ChangeExtension(txtExcelTemplatePath.Text, ".xml")); }
/// <summary> /// 获取模板文件对应的模板格式配置XML文件路径(当不存在或较旧时,将会重新生成) /// </summary> /// <param name="templatePath"></param> /// <param name="newGenerate"></param> /// <returns></returns> public static string GetTemplateConfigFilePath(string templatePath, bool newGenerate = false) { string templateConfigFilePath = Path.ChangeExtension(templatePath, ".xml"); if (newGenerate || !File.Exists(templateConfigFilePath) || File.GetLastWriteTime(templatePath) > File.GetLastWriteTime(templateConfigFilePath)) { WorkbookParameterContainer workbookParameter = ParseTemplate.Parse(templatePath); workbookParameter.Save(templateConfigFilePath); } return(templateConfigFilePath); }
private void btnFillTemplate_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(txtExcelTemplatePath.Text.Trim())) { MessageBox.Show("请选择Excel模板文件!", "提示"); return; } string xmlFilePath = Path.ChangeExtension(txtExcelTemplatePath.Text, ".xml"); try { WorkbookParameterContainer workbookParameter = ParseTemplate.Parse(txtExcelTemplatePath.Text); workbookParameter.Save(xmlFilePath); } catch (Exception ex) { MessageBox.Show("生成失败" + ex.Message); return; } MessageBox.Show("生成成功:" + xmlFilePath); }
public static WorkbookParameterContainer Parse(string templatePath) { var workbookParameterContainer = new WorkbookParameterContainer(); string templateConfig = Path.ChangeExtension(templatePath, ".xml"); if (File.Exists(templateConfig)) { workbookParameterContainer.Load(templateConfig); return(workbookParameterContainer); } IWorkbook workbook = NPOIHelper.LoadWorkbook(templatePath); ISheet sheet = workbook.GetSheetAt(0); sheetName = sheet.SheetName; workbookParameterContainer[sheetName] = new SheetParameterContainer { SheetName = sheetName }; foreach (IRow row in sheet) { foreach (ICell cell in row.Cells) { if (cell.CellType.Equals(CellType.String)) { MatchCollection matches = new Regex(@"(?<=\$\[)([\w]*)(?=\])").Matches(cell.StringCellValue); foreach (Match match in matches) { workbookParameterContainer[sheet.SheetName][match.Value] = new Parameter { Name = match.Value, RowIndex = cell.RowIndex, ColumnIndex = cell.ColumnIndex }; } } } } workbookParameterContainer.Save(templateConfig); return(workbookParameterContainer); }