Exemplo n.º 1
0
        /// <summary>
        /// CopyCell
        /// </summary>
        /// <param name="oldCell"></param>
        /// <param name="newCell"></param>
        public ICell CopyCell(ICell oldCell, ICell newCell)
        {
            // Copy style from old cell and apply to new cell
            var newCellStyle = Wk.CreateCellStyle();

            newCellStyle.CloneStyleFrom(oldCell.CellStyle);;
            newCell.CellStyle = newCellStyle;

            // If there is a cell comment, copy
            if (newCell.CellComment != null)
            {
                newCell.CellComment = oldCell.CellComment;
            }

            // If there is a cell hyperlink, copy
            if (oldCell.Hyperlink != null)
            {
                newCell.Hyperlink = oldCell.Hyperlink;
            }

            // Set the cell data type
            newCell.SetCellType(oldCell.CellType);

            switch (oldCell.CellType)
            {
            case CellType.Blank:
                newCell.SetCellValue(oldCell.StringCellValue);
                break;

            case CellType.Boolean:
                newCell.SetCellValue(oldCell.BooleanCellValue);
                break;

            case CellType.Error:
                newCell.SetCellErrorValue(oldCell.ErrorCellValue);
                break;

            case CellType.Formula:
                newCell.SetCellFormula(oldCell.CellFormula);
                break;

            case CellType.Numeric:
                newCell.SetCellValue(oldCell.NumericCellValue);
                break;

            case CellType.String:
                newCell.SetCellValue(oldCell.RichStringCellValue);
                break;

            case CellType.Unknown:
                newCell.SetCellValue(oldCell.StringCellValue);
                break;
            }

            return(newCell);
        }
Exemplo n.º 2
0
        private void InitializeWorkbook()
        {
            if (!FileName.IsNullOrEmpty())
            {
                Wk = WorkbookFactory.Create(FileName);

                FontOne                    = new HSSFFont(NPOI.HSSF.Util.HSSFColor.Blue.Index, new NPOI.HSSF.Record.FontRecord());
                FontOne.FontName           = "宋体";
                FontOne.FontHeightInPoints = 11;

                CellStyleOne            = Wk.CreateCellStyle();
                CellStyleOne.DataFormat = HSSFDataFormat.GetBuiltinFormat("0");

                CellStyleTwo                = Wk.CreateCellStyle();
                CellStyleTwo.DataFormat     = HSSFDataFormat.GetBuiltinFormat("0.00%");
                CellStyleTwo.TopBorderColor = NPOI.HSSF.Util.HSSFColor.Blue.Index;
                // CellStyleTwo.SetFont(FontOne);
            }
        }