Пример #1
0
        /// <summary>
        /// Clones all the style information from another
        /// HSSFCellStyle, onto this one. This
        /// HSSFCellStyle will then have all the same
        /// properties as the source, but the two may
        /// be edited independently.
        /// Any stylings on this HSSFCellStyle will be lost!
        /// The source HSSFCellStyle could be from another
        /// HSSFWorkbook if you like. This allows you to
        /// copy styles from one HSSFWorkbook to another.
        /// </summary>
        /// <param name="source">The source.</param>
        public void CloneStyleFrom(HSSFCellStyle source)
        {
            // First we need to clone the extended format
            //  record
            _format.CloneStyleFrom(source._format);

            // Handle matching things if we cross workbooks
            if (_workbook != source._workbook)
            {
                lastDateFormat           = short.MinValue;
                lastFormats              = null;
                getDataFormatStringCache = null;
                // Then we need to clone the format string,
                //  and update the format record for this
                short fmt = (short)_workbook.CreateFormat(
                    source.GetDataFormatString()
                    );
                this.DataFormat = (fmt);

                // Finally we need to clone the font,
                //  and update the format record for this
                FontRecord fr = _workbook.CreateNewFont();
                fr.CloneStyleFrom(
                    source._workbook.GetFontRecordAt(
                        source.FontIndex
                        )
                    );

                HSSFFont font = new HSSFFont(
                    (short)_workbook.GetFontIndex(fr), fr
                    );
                this.SetFont(font);
            }
        }
Пример #2
0
        public void TestCloneOnto()
        {
            FontRecord base1 = new FontRecord(TestcaseRecordInputStream.Create(0x31, data));

            FontRecord other = new FontRecord();

            other.CloneStyleFrom(base1);

            byte[] recordBytes = other.Serialize();
            Assert.AreEqual(recordBytes.Length - 4, data.Length);
            for (int i = 0; i < data.Length; i++)
            {
                Assert.AreEqual(data[i], recordBytes[i + 4], "At offset " + i);
            }
        }
Пример #3
0
        public static void CopyCell(HSSFCell oldCell, HSSFCell newCell, IDictionary <Int32, HSSFCellStyle> styleMap, Dictionary <short, short> paletteMap, Boolean keepFormulas)
        {
            if (styleMap != null)
            {
                if (oldCell.CellStyle != null)
                {
                    if (oldCell.Sheet.Workbook == newCell.Sheet.Workbook)
                    {
                        newCell.CellStyle = oldCell.CellStyle;
                    }
                    else
                    {
                        int styleHashCode = oldCell.CellStyle.GetHashCode();
                        if (styleMap.ContainsKey(styleHashCode))
                        {
                            newCell.CellStyle = styleMap[styleHashCode];
                        }
                        else
                        {
                            HSSFCellStyle newCellStyle = (HSSFCellStyle)newCell.Sheet.Workbook.CreateCellStyle();
                            newCellStyle.CloneStyleFrom(oldCell.CellStyle);
                            RemapCellStyle(newCellStyle, paletteMap); //Clone copies as-is, we need to remap colors manually
                            newCell.CellStyle = newCellStyle;
                            //Clone of cell style always clones the font. This makes my life easier
                            IFont theFont = newCellStyle.GetFont(newCell.Sheet.Workbook);
                            if (theFont.Color > 0 && paletteMap.ContainsKey(theFont.Color))
                            {
                                theFont.Color = paletteMap[theFont.Color]; //Remap font color
                            }
                            styleMap.Add(styleHashCode, newCellStyle);
                        }
                    }
                }
                else
                {
                    newCell.CellStyle = null;
                }
            }
            switch (oldCell.CellType)
            {
            case CellType.String:
                HSSFRichTextString rts = oldCell.RichStringCellValue as HSSFRichTextString;
                newCell.SetCellValue(rts);
                if (rts != null)
                {
                    for (int j = 0; j < rts.NumFormattingRuns; j++)
                    {
                        short fontIndex  = rts.GetFontOfFormattingRun(j);
                        int   startIndex = rts.GetIndexOfFormattingRun(j);
                        int   endIndex   = 0;
                        if (j + 1 == rts.NumFormattingRuns)
                        {
                            endIndex = rts.Length;
                        }
                        else
                        {
                            endIndex = rts.GetIndexOfFormattingRun(j + 1);
                        }
                        FontRecord fr = newCell.BoundWorkbook.CreateNewFont();
                        fr.CloneStyleFrom(oldCell.BoundWorkbook.GetFontRecordAt(fontIndex));
                        HSSFFont font = new HSSFFont((short)(newCell.BoundWorkbook.GetFontIndex(fr)), fr);
                        newCell.RichStringCellValue.ApplyFont(startIndex, endIndex, font);
                    }
                }
                break;

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

            case CellType.Blank:
                newCell.SetCellType(CellType.Blank);
                break;

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

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

            case CellType.Formula:
                if (keepFormulas)
                {
                    newCell.SetCellType(CellType.Formula);
                    newCell.CellFormula = oldCell.CellFormula;
                }
                else
                {
                    try
                    {
                        newCell.SetCellType(CellType.Numeric);
                        newCell.SetCellValue(oldCell.NumericCellValue);
                    }
                    catch (Exception ex)
                    {
                        newCell.SetCellType(CellType.String);
                        newCell.SetCellValue(oldCell.ToString());
                    }
                }
                break;

            default:
                break;
            }
        }