/// <summary> /// 给指定Cell设置值 /// </summary> /// <param name="cell">Excel单元格</param> /// <param name="value">值对象</param> /// <returns></returns> public static bool SetCellValue(ICell cell, Object value) { if (cell != null && value != null) { if (value.GetType() == typeof(DateTime) || value.GetType() == typeof(DateTime?)) { cell.SetCellValue((DateTime)value); } else if (value.GetType() == typeof(Boolean) || value.GetType() == typeof(Boolean?)) { cell.SetCellValue((Boolean)value); } else if (value.GetType() == typeof(IRichTextString)) { cell.SetCellValue((Boolean)value); } else if (NPOIUtil.NumberType(value.GetType())) { cell.SetCellValue(Double.Parse(value.ToString())); } else { cell.SetCellValue(value.ToString()); } return(true); } return(false); }
/// <summary> /// 初始化参数 /// </summary> private void init() { //FieldName convert to lower case DateTimeFields.ForEach(p => p = p.ToLower()); DoubleFields.ForEach(p => p = p.ToLower()); AllFields.ForEach(p => p = p.ToLower()); if (AllFields.Count == 0) { AllFields = this.GetDefaultFields(); } //自动识别 if (TypeDetectable) { foreach (String field in AllFields) { Type type = dt.Columns[field].DataType; if (typeof(DateTime) == type) { DateTimeFields.Add(field); } else if (NPOIUtil.NumberType(type)) { DoubleFields.Add(field); } } } }
/// <summary> /// 设置合并区域,并设置值 /// </summary> /// <param name="sheet">Excel的Sheet</param> /// <param name="rowStart">开始行</param> /// <param name="rowEnd">结束行</param> /// <param name="colStart">开始列</param> /// <param name="colEnd">结束列</param> /// <param name="obj">值对象</param> /// <returns>MergeRegion的索引</returns> public static int SetMergeRegion(ISheet sheet, int rowStart, int rowEnd, int colStart, int colEnd, Object obj = null) { try { int index = sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowStart, rowEnd, colStart, colEnd)); if (obj != null) { ICell cell = sheet.GetRow(rowStart).GetCell(colStart); NPOIUtil.SetCellValue(cell, obj); } return(index); } catch { return(-1); } }
private void init() { if (AllFields.Count == 0) { AllFields = GetDefaultFields(); } //自动识别 if (TypeDetectable) { foreach (String field in AllFields) { Type type = typeof(T).GetProperty(field).PropertyType; if (typeof(DateTime) == type || typeof(DateTime?) == type) { DateTimeFields.Add(field); } else if (NPOIUtil.NumberType(type)) { DoubleFields.Add(field); } } } }