/// <summary> /// 设置单元格的值。 /// </summary> /// <param name="cell">单元格。</param> /// <param name="map">单元格映射数据。</param> /// <param name="value">设置的值。</param> private static void SetCellValue(ICell cell, CellDataMap map, object value) { if (value is int || value is short || value is double || value is decimal) { if (!TryFormatCellValue(cell, map, Convert.ToDouble(value))) { cell.SetCellValue(Convert.ToDouble(value)); } } else if (value is DateTime) { var date = Convert.ToDateTime(value); if (date.Year <= 1) { cell.SetCellValue(string.Empty); } else if (!TryFormatCellValue(cell, map, value)) { cell.SetCellValue(date); } } else { if (value == null) { cell.SetCellValue(string.Empty); } else if (!TryFormatCellValue(cell, map, value)) { SetCellString(cell, value.ToString()); } } }
/// <summary> /// 尝试通过格式化后设置单元格的值。 /// </summary> /// <param name="cell">单元格。</param> /// <param name="map">单元格映射数据。</param> /// <param name="value">设置的值。</param> /// <returns></returns> private static bool TryFormatCellValue(ICell cell, CellDataMap item, object value) { if (!string.IsNullOrEmpty(item.Format)) { cell.SetCellValue(item.Format.Replace("[" + item.Key + "]", value.ToStringSafely())); return(true); } return(false); }
/// <summary> /// 解析数据集 /// </summary> /// <param name="cell">Cell</param> /// <param name="set">Set</param> /// <param name="key">Text</param> /// <param name="format">格式。</param> /// <param name="r">起始行索引</param> /// <returns></returns> private static SheetDataSet ParseDataSet(ICell cell, SheetDataSet set, string key, string format, int r) { if (set == null) { set = new SheetDataSet(); set.StartRowIndex = r; } if (format == key) { format = string.Empty; } var ditem = new CellDataMap(key.Substring(1, key.Length - 2), format); set.Properties.Add(cell, ditem); return(set); }