public static void UpdateCellValue(ReportSheetTemplate tpl, CellRange cell, object value, string format) { UpdateCellValueCallTimes++; value = FormatValue(tpl, value, format); try { switch (format) { case TemplateFlags.FormatDate: case TemplateFlags.FormatMonth: case TemplateFlags.FormatTime: case TemplateFlags.FormatDateTime: case TemplateFlags.FormatTimeSpan: break; case TemplateFlags.FormatFen: case TemplateFlags.FormatNumber: double outValue; cell.NumberValue = double.TryParse(value == null ? string.Empty : value.ToString(), out outValue) ? outValue : 0.0; return; } } catch { // ignored } cell.Text = Convert.ToString(value); }
public static object FormatValue(ReportSheetTemplate tpl, object value, string format) { if (!string.IsNullOrEmpty(format) && (value != null)) { switch (format) { case TemplateFlags.FormatDate: return(ValueToDate(value)); case TemplateFlags.FormatMonth: return(ValueToMonth(value)); case TemplateFlags.FormatTime: return(ValueToTime(value)); case TemplateFlags.FormatDateTime: return(ValueToDateTime(value)); case TemplateFlags.FormatTimeSpan: return(ValueToTimeSpan(value, tpl)); case TemplateFlags.FormatFen: return(ValueToFen(value)); } } return(value); }
public void InitDynamicColumn(ReportSheetTemplate template) { DynamicColumn = FindDynamicColumn(); if (DynamicColumn != null) { DynamicColumn.Tpl = template; } }
private static object ValueToTimeSpan(object value, ReportSheetTemplate tpl) { int num; object obj2; DateTime time; if (((value == null) || !tpl.ParamMap.TryGetValue("time_span", out obj2)) || (!int.TryParse(obj2.ToString(), out num) || !DateTime.TryParseExact(value.ToString(), "yyyyMMddHHmmss", null, DateTimeStyles.None, out time))) { return(ValueToTime(value)); } var time2 = time.Add(new TimeSpan(0, 0, num, 0)); return(time.ToString("HH:mm") + "-" + time2.ToString("HH:mm")); }
/// <summary> /// 写Sheet表单元格 /// </summary> /// <param name="tpl"></param> /// <param name="holder"></param> /// <param name="currentCellRange"></param> /// <param name="table"></param> /// <param name="valueRowIndex"></param> public void WriteCell(ReportSheetTemplate tpl, GroupDataHolder holder, CellRange currentCellRange, DataTable table, int valueRowIndex) { if (UseR1C1Formula) { currentCellRange.FormulaR1C1 = "=" + TplTextContent; } else { var cellValue = GetValue(holder, table, valueRowIndex); if ((cellValue == null || cellValue == string.Empty) || (cellValue == DBNull.Value)) { cellValue = TplDefaultContent; } RangeHelper.UpdateCellValue(tpl, currentCellRange, cellValue, TplFormat); if (GroupAlign == GroupAlign.Vertical) { //纵向分组才更新最后分组值,水平分组在判断是否新增单元格时更新(before情况) LastGroupedValue = GetGroupValue(holder, table, valueRowIndex); } } }
public static ReportSheetTemplate ParseSheetTemplate(Worksheet sheet) { var template = new ReportSheetTemplate { Sheet = sheet }; var sheetIndex = sheet.Index + 1; for (var i = 1; i < TemplateFlags.IndexTemplateEndRow; i++) { var str = (string)RangeHelper.GetRange(sheet, 1, i, 1, 1).Value2; if (!string.IsNullOrEmpty(str)) { if (str.Equals(TemplateFlags.EmptyFields, StringComparison.CurrentCultureIgnoreCase)) { var emptyFieldStr = RangeHelper.GetRange(sheet, 2, i, 1, 1).Value2 as string; if (!string.IsNullOrEmpty(emptyFieldStr)) { template.EmptyFieldsDict = ParseKeyValuePair(emptyFieldStr); } } else { var dict = ParseKeyValuePair(str); template.AutoFit = dict.ContainsKey(TemplateFlags.BlockAutoFit) && dict[TemplateFlags.BlockAutoFit] == "true"; var namePrefix = string.Concat("S", sheetIndex, "."); //Todo:JoinAt 动态列合并 int joinat; var block = new TplBlock { StartParseColumnIndex = 2, StartParseRowIndex = i, ColumnsCount = int.Parse(dict[TemplateFlags.BlockColumnCount]), TplColumCount = int.Parse(dict[TemplateFlags.BlockColumnCount]), TplRowCount = int.Parse(dict[TemplateFlags.BlockRowCount]), Name = dict.ContainsKey(TemplateFlags.BlockName) ? dict[TemplateFlags.BlockName] : string.Concat(namePrefix + "block", template.BlockList.Count + 1), DataTableIndex = dict.ContainsKey(TemplateFlags.BlockTable) ? int.Parse(dict[TemplateFlags.BlockTable]) : -1, TplColumnTableIndex = dict.ContainsKey(TemplateFlags.BlockColumnTalbe) ? int.Parse(dict[TemplateFlags.BlockColumnTalbe]) : -1, CopyOnly = dict.ContainsKey(TemplateFlags.BlockCopyOnly) && dict[TemplateFlags.BlockCopyOnly] == "true", UpdateAllRow = dict.ContainsKey(TemplateFlags.BlockUpdateAllRow) && dict[TemplateFlags.BlockUpdateAllRow] == "true", Joinat = dict.ContainsKey(TemplateFlags.BlockJoinAt) && int.TryParse(dict[TemplateFlags.BlockJoinAt], out joinat) ? joinat : -1 }; block.TplRange = RangeHelper.GetRange(sheet, block.StartParseColumnIndex, block.StartParseRowIndex, block.ColumnsCount, block.TplRowCount); if (block.CopyOnly) { template.BlockList.Add(block); } else { for (var j = 0; j < block.TplRowCount; j++) { var startColumn = TemplateFlags.IndexLineContendStartColumn; var line = ParseLine(sheet, block, startColumn, j + block.StartParseRowIndex); line.SheetTemplate = template; line.StartColumnIndex = startColumn; line.InsertOption = GetLineInsertOption( RangeHelper.GetCell(sheet, TemplateFlags.IndexLineInsertOptionColumn, j + block.StartParseRowIndex).Value2 as string); line.TplCellCount = block.ColumnsCount; block.TplLineList.Add(line); } block.InitDynamicColumn(template); template.BlockList.Add(block); } } } } return(template); }