static bool AddCellUnit(int columnIndex, ref int exportColumnIndex, SheetConfig Sheetcfg, Cell cell, WorkbookPart workbookPart, List<string> stylesList, List<CellUnit> cellUnitList) { if (columnIndex >= Sheetcfg.ColumnConfigData.Count) goto Error; var columnConfig = Sheetcfg.ColumnConfigData[columnIndex]; if (!columnConfig.Export) return true; var dataType = columnConfig.FieldType; string cellValue = cell == null ? "" : GetCellValue(cell, workbookPart, stylesList); CellUnit cellUnit = null; switch (dataType) { case DesignerDataType.Int: { int intValue = 0; if (!int.TryParse(cellValue, out intValue) && !string.IsNullOrEmpty(cellValue)) goto Error; cellUnit = new CellUnitIntValue(exportColumnIndex, intValue); } break; case DesignerDataType.Float: { float floatValue = 0.0f; if (!float.TryParse(cellValue, out floatValue) && !string.IsNullOrEmpty(cellValue)) goto Error; cellUnit = new CellUnitFloatValue(exportColumnIndex, floatValue); } break; case DesignerDataType.String: { cellUnit = new CellUnitStringValue(exportColumnIndex, cellValue); } break; case DesignerDataType.Bool: { decimal decimalValue = 0; cellUnit = new CellUnitBoolValue(exportColumnIndex, decimal.TryParse(cellValue, out decimalValue) && decimalValue != 0); } break; } if (cellUnit.IsNeedWrite()) cellUnitList.Add(cellUnit); ++exportColumnIndex; return true; Error: string excelFileName = ""; if (!dicOutputFileName.TryGetValue(Sheetcfg.Name, out excelFileName)) excelFileName = ""; Console.Error.WriteLine(string.Format(Resources.ExcelConfigFileInValid, excelFileName, Sheetcfg.Name)); return false; }
public void InitData(Excel.Workbook workbook, Excel.Worksheet worksheet, CustomTaskPane TaskPane, SheetConfig ShtCfg = null) { this.Wb = workbook; this.ws = worksheet; this.myTaskPane = TaskPane; ((DocEvents_Event)ws).Activate += ws_Activate; this.ws.Deactivate += ws_Deactivate; this.ws.Change += ws_Change; if (ShtCfg != null) { foreach (var col in ShtCfg.ColumnConfigData) { if (col != null) { dataGridView1.Rows.Add(col.FieldName, col.FieldType, col.LimitMin, col.LimitMax, col.Export, col.Primarykey); } } } else { ShtCfg = new SheetConfig(); ShtCfg.Name = this.ws.Name; } this.SheetCfg = ShtCfg; this.ws_Change(this.ws.Cells[1, 1]); // 此时可能存在不合法数据(例如列名重复或列与配置不符),所以要按单元格内容变化的逻辑处理一次 }
internal void SetSheetConfig(SheetConfig sheetConfig, ExcelCodeConfig excelCodeConfig) { this.excelCodeConfig = excelCodeConfig; this.sheetConfig = sheetConfig; this.dataGridView1.Rows.Clear(); if (sheetConfig != null) { textBox_ClassName.NameInUndoDescription = string.Format(Properties.Resources.TextBox_ClassNam_NameInUndoDescription, ExcelConfigName, SheetName); textBox_ClassName.Text = sheetConfig.ClassName; textBox_ClassName.EnableUndo = true; dataGridView1.NameInUndoDescription = string.Format(Properties.Resources.DataGridView_NameInUndoDescription, ExcelConfigName, SheetName); foreach (var columnConfig in sheetConfig.ColumnConfigData) { if (columnConfig.Export) { int rowIndex = dataGridView1.Rows.Add(columnConfig.FieldName, columnConfig.CodeName); dataGridView1.Rows[rowIndex].Tag = columnConfig; } } dataGridView1.EnableUndo = true; } }