Пример #1
0
        internal override ICellStyle SetCell(ICellStyle cellStyle)
        {
            IDataFormat format = Workbook.CreateDataFormat();

            cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("¥#,##0");
            return(cellStyle);
        }
        internal override ICellStyle SetCell(ICellStyle cellStyle)
        {
            IDataFormat format = Workbook.CreateDataFormat();

            cellStyle.DataFormat = format.GetFormat("yyyy/mm/dd");
            return(cellStyle);
        }
Пример #3
0
        private CellStyle GetCellStyleForFormat(Workbook workbook, string dataFormat)
        {
            if (!this.cellStyleCache.ContainsKey(dataFormat))
            {
                var style = workbook.CreateCellStyle();

                // check if this is a built-in format
                var builtinFormatId = HSSFDataFormat.GetBuiltinFormat(dataFormat);

                if (builtinFormatId != -1)
                {
                    style.DataFormat = builtinFormatId;
                }
                else
                {
                    // not a built-in format, so create a new one
                    var newDataFormat = workbook.CreateDataFormat();
                    style.DataFormat = newDataFormat.GetFormat(dataFormat);
                }

                this.cellStyleCache[dataFormat] = style;
            }

            return(this.cellStyleCache[dataFormat]);
        }
Пример #4
0
        protected CellStyle GetDateTimeStyle()
        {
            if (Parent != null)
            {
                var style = Parent.GetDateTimeStyle();
                if (style != null)
                {
                    return(style);
                }
            }

            if (_dateTimeStyle != null)
            {
                return(_dateTimeStyle);
            }

            _dateTimeStyle = Workbook.CreateCellStyle();
            var format = Workbook.CreateDataFormat();

            _dateTimeStyle.DataFormat      = format.GetFormat("DD.MM.YYYY");
            GetRootParent()._dateTimeStyle = _dateTimeStyle;
            return(_dateTimeStyle);
        }
Пример #5
0
        private ICellStyle GetColumnStyle(int index, string format)
        {
            if (ColumnStyles.ContainsKey(index))
            {
                return(ColumnStyles[index]);
            }

            var cellStyle = Workbook.CreateCellStyle();

            if (string.IsNullOrEmpty(format))
            {
                ColumnStyles.Add(index, cellStyle);
                return(cellStyle);
            }

            var cellFormat = Workbook.CreateDataFormat();

            cellStyle.DataFormat = cellFormat.GetFormat(format);

            ColumnStyles.Add(index, cellStyle);

            return(cellStyle);
        }
Пример #6
0
        public Cell AddCell(Object value, int columnIndex, Row row, TextStyle textStyle, short backgroundColor,
                            CellBorderType borderStyle, short borderColor)
        {
            Cell cell = row.GetCell(columnIndex) ?? row.CreateCell(columnIndex);

            CellStyle cellStyle = Styles[textStyle];

            if (value == null)
            {
                cell.SetCellValue("");
            }
            else if (value.GetType() == typeof(DateTime))
            {
                cell.SetCellValue((DateTime)value);
                cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("m/d/yy");
            }
            else if (value.GetType() == typeof(double) || value.GetType() == typeof(float))
            {
                cell.SetCellValue((double)value);

                DataFormat format = Workbook.CreateDataFormat();
                cellStyle.DataFormat = format.GetFormat("# ### ### ##0.00");
            }
            else if (value.GetType() == typeof(decimal))
            {
                var d = (decimal)value;
                cell.SetCellValue((double)d);

                DataFormat format = Workbook.CreateDataFormat();
                cellStyle.DataFormat = format.GetFormat("# ### ### ##0.00");
            }
            else if (value.GetType() == typeof(Int16) ||
                     value.GetType() == typeof(Int32) ||
                     value.GetType() == typeof(Int64) ||
                     value.GetType() == typeof(UInt16) ||
                     value.GetType() == typeof(UInt32) ||
                     value.GetType() == typeof(UInt64) ||
                     value.GetType() == typeof(Byte) ||
                     value.GetType() == typeof(SByte))
            {
                cell.SetCellValue((int)value);
                cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0");
            }
            else if (value.GetType() == typeof(bool))
            {
                cell.SetCellValue((bool)value);
            }
            else
            {
                cell.SetCellValue(value.ToString());
            }

            if (backgroundColor != short.MinValue)
            {
                cellStyle.FillForegroundColor = backgroundColor;
                cellStyle.FillPattern         = FillPatternType.SOLID_FOREGROUND;
            }

            if (borderColor != short.MinValue)
            {
                cellStyle.BorderBottom = borderStyle;
                cellStyle.BorderLeft   = borderStyle;
                cellStyle.BorderRight  = borderStyle;
                cellStyle.BorderTop    = borderStyle;

                cellStyle.BottomBorderColor = borderColor;
                cellStyle.LeftBorderColor   = borderColor;
                cellStyle.RightBorderColor  = borderColor;
                cellStyle.TopBorderColor    = borderColor;
            }

            cell.CellStyle = cellStyle;

            return(cell);
        }