コード例 #1
0
        public HSSFCreationHelper(HSSFWorkbook wb)
        {
            workbook = wb;

            // Create the things we only ever need one of
            dataFormat = new HSSFDataFormat(workbook.Workbook);
        }
コード例 #2
0
ファイル: HSSFSheet.cs プロジェクト: missxiaohuang/Weekly
        //private static POILogger log = POILogFactory.GetLogger(typeof(HSSFSheet));

        /// <summary>
        /// Creates new HSSFSheet - called by HSSFWorkbook to create a _sheet from
        /// scratch. You should not be calling this from application code (its protected anyhow).
        /// </summary>
        /// <param name="workbook">The HSSF Workbook object associated with the _sheet.</param>
        /// <see cref="LF.Utils.NPOI.HSSF.UserModel.HSSFWorkbook.CreateSheet()"/>
        public HSSFSheet(HSSFWorkbook workbook)
        {
            _sheet = InternalSheet.CreateSheet();
            rows = new Dictionary<int, LF.Utils.NPOI.SS.UserModel.IRow>();
            this._workbook = workbook;
            this.book = workbook.Workbook;
        }
コード例 #3
0
ファイル: NPOIHelper.cs プロジェクト: missxiaohuang/Weekly
        private static ICellStyle GetHeadStyle(HSSFWorkbook workbook)
        {
            //表头样式
            var headStyle = workbook.CreateCellStyle();
            headStyle.Alignment = HorizontalAlignment.LEFT;//居中对齐
            //表头单元格背景色
            headStyle.FillForegroundColor = HSSFColor.LIGHT_GREEN.index;
            headStyle.FillPattern = FillPatternType.SOLID_FOREGROUND;
            //表头单元格边框
            headStyle.BorderTop = BorderStyle.THIN;
            headStyle.TopBorderColor = HSSFColor.BLACK.index;
            headStyle.BorderRight = BorderStyle.THIN;
            headStyle.RightBorderColor = HSSFColor.BLACK.index;
            headStyle.BorderBottom = BorderStyle.THIN;
            headStyle.BottomBorderColor = HSSFColor.BLACK.index;
            headStyle.BorderLeft = BorderStyle.THIN;
            headStyle.LeftBorderColor = HSSFColor.BLACK.index;
            //表头字体设置
            var font = workbook.CreateFont();
            font.FontHeightInPoints = 12;//字号
            font.Boldweight = 600;//加粗
            //font.Color = HSSFColor.WHITE.index;//颜色
            headStyle.SetFont(font);

            return headStyle;
        }
コード例 #4
0
 /**
  * Construct an escher graphics object.
  *
  * @param escherGroup           The escher Group to Write the graphics calls into.
  * @param workbook              The workbook we are using.
  * @param forecolor             The foreground color to use as default.
  * @param verticalPointsPerPixel    The font multiplier.  (See class description for information on how this works.).
  */
 public EscherGraphics(HSSFShapeGroup escherGroup, HSSFWorkbook workbook, Color forecolor, float verticalPointsPerPixel)
 {
     this.escherGroup = escherGroup;
     this.workbook = workbook;
     this.verticalPointsPerPixel = verticalPointsPerPixel;
     this.verticalPixelsPerPoint = 1 / verticalPointsPerPixel;
     this.font = new Font("Arial", 10);
     this.foreground = forecolor;
     //        background = backcolor;
 }
コード例 #5
0
        //[Obsolete]
        //public static void SetBorderLeft(LF.Utils.NPOI.SS.UserModel.CellBorderType border, Region region, HSSFSheet sheet,
        //        HSSFWorkbook workbook)
        //{
        //    SetBorderLeft(border, toCRA(region), sheet, workbook);
        //}
        /// <summary>
        /// Sets the left border for a region of cells by manipulating the cell style
        /// of the individual cells on the left
        /// </summary>
        /// <param name="border">The new border</param>
        /// <param name="region">The region that should have the border</param>
        /// <param name="sheet">The sheet that the region is on.</param>
        /// <param name="workbook">The workbook that the region is on.</param>
        public static void SetBorderLeft(LF.Utils.NPOI.SS.UserModel.BorderStyle border, CellRangeAddress region, HSSFSheet sheet,
                HSSFWorkbook workbook)
        {
            int rowStart = region.FirstRow;
            int rowEnd = region.LastRow;
            int column = region.FirstColumn;

            CellPropertySetter cps = new CellPropertySetter(workbook, HSSFCellUtil.BORDER_LEFT, (int)border);
            for (int i = rowStart; i <= rowEnd; i++)
            {
                cps.SetProperty(HSSFCellUtil.GetRow(i, sheet), column);
            }
        }
コード例 #6
0
 public CellEvaluationFrame(HSSFWorkbook workbook, HSSFSheet sheet, int srcRowNum, int srcColNum)
 {
     if (workbook == null)
     {
         throw new ArgumentException("workbook must not be null");
     }
     if (sheet == null)
     {
         throw new ArgumentException("sheet must not be null");
     }
     _workbook = workbook;
     _sheet = sheet;
     _srcRowNum = srcRowNum;
     _srcColNum = srcColNum;
 }
コード例 #7
0
        public HSSFAutoFilter(string formula,HSSFWorkbook workbook)
        {
            //this.workbook = workbook;

            Ptg[] ptgs = HSSFFormulaParser.Parse(formula, workbook);
            if (!(ptgs[0] is Area3DPtg))
                throw new ArgumentException("incorrect formula");

            Area3DPtg ptg = (Area3DPtg)ptgs[0];
            HSSFSheet sheet = (HSSFSheet)workbook.GetSheetAt(ptg.ExternSheetIndex);
            //look for the prior record
            int loc = sheet.Sheet.FindFirstRecordLocBySid(DefaultColWidthRecord.sid) ;
            CreateFilterModeRecord(sheet, loc+1);
            CreateAutoFilterInfoRecord(sheet, loc + 2,ptg);
            //look for "_FilterDatabase" NameRecord of the sheet
            NameRecord name = workbook.Workbook.GetSpecificBuiltinRecord(NameRecord.BUILTIN_FILTER_DB, ptg.ExternSheetIndex+1);
            if (name == null)
                name = workbook.Workbook.CreateBuiltInName(NameRecord.BUILTIN_FILTER_DB, ptg.ExternSheetIndex + 1);
            name.IsHiddenName = true;

            name.NameDefinition = ptgs;
        }
コード例 #8
0
 /**
  * Static method to convert an array of {@link Ptg}s in RPN order
  * to a human readable string format in infix mode.
  * @param book  used for defined names and 3D references
  * @param ptgs  must not be <c>null</c>
  * @return a human readable String
  */
 public static String ToFormulaString(HSSFWorkbook book, Ptg[] ptgs)
 {
     return FormulaRenderer.ToFormulaString(HSSFEvaluationWorkbook.Create(book), ptgs);
 }
コード例 #9
0
        /**
 * @param formula     the formula to parse
 * @param workbook    the parent workbook
 * @param formulaType a constant from {@link FormulaType}
 * @param sheetIndex  the 0-based index of the sheet this formula belongs to.
 * The sheet index is required to resolve sheet-level names. <code>-1</code> means that
 * the scope of the name will be ignored and  the parser will match named ranges only by name
 *
 * @return the parsed formula tokens
 */
        public static Ptg[] Parse(String formula, HSSFWorkbook workbook, FormulaType formulaType, int sheetIndex)
        {
            return FormulaParser.Parse(formula, CreateParsingWorkbook(workbook), formulaType, sheetIndex);
        }
コード例 #10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ExcelExtractor"/> class.
 /// </summary>
 /// <param name="wb">The wb.</param>
 public ExcelExtractor(HSSFWorkbook wb)
     : base(wb)
 {
     this.wb = wb;
     _formatter = new HSSFDataFormatter();
 }
コード例 #11
0
ファイル: HSSFCell.cs プロジェクト: missxiaohuang/Weekly
        /// <summary>
        /// Creates new Cell - Should only be called by HSSFRow.  This Creates a cell
        /// from scratch.
        /// </summary>
        /// <param name="book">Workbook record of the workbook containing this cell</param>
        /// <param name="sheet">Sheet record of the sheet containing this cell</param>
        /// <param name="row">the row of this cell</param>
        /// <param name="col">the column for this cell</param>
        /// <param name="type">CellType.NUMERIC, CellType.STRING, CellType.FORMULA, CellType.BLANK,
        /// CellType.BOOLEAN, CellType.ERROR</param>
        public HSSFCell(HSSFWorkbook book, HSSFSheet sheet, int row, short col,
                           CellType type)
        {
            CheckBounds(col);
            cellType = CellType.Unknown; // Force 'SetCellType' to Create a first Record
            stringValue = null;
            this.book = book;
            this.sheet = sheet;

            short xfindex = sheet.Sheet.GetXFIndexForColAt(col);
            SetCellType(type, false, row, col, xfindex);
        }
コード例 #12
0
 public CellPropertySetter(HSSFWorkbook workbook, String propertyName, int value)
 {
     _workbook = workbook;
     _propertyName = propertyName;
     _propertyValue = (short)value;
 }
コード例 #13
0
ファイル: HSSFRow.cs プロジェクト: missxiaohuang/Weekly
        /// <summary>
        /// Creates an HSSFRow from a low level RowRecord object.  Only HSSFSheet should do
        /// this.  HSSFSheet uses this when an existing file is Read in.
        /// </summary>
        /// <param name="book">low-level Workbook object containing the sheet that Contains this row</param>
        /// <param name="sheet"> low-level Sheet object that Contains this Row</param>
        /// <param name="record">the low level api object this row should represent</param>
        ///<see cref="LF.Utils.NPOI.HSSF.UserModel.HSSFSheet.CreateRow(int)"/>
        public HSSFRow(HSSFWorkbook book, HSSFSheet sheet, RowRecord record)
        {
            this.book = book;
            this.sheet = sheet;
            row = record;

            RowNum=(record.RowNumber);
             // Don't trust colIx boundaries as read by other apps
            // set the RowRecord empty for the moment
            record.SetEmpty();
            

        }
コード例 #14
0
ファイル: HSSFRow.cs プロジェクト: missxiaohuang/Weekly
        /// <summary>
        /// Creates new HSSFRow from scratch. Only HSSFSheet should do this.
        /// </summary>
        /// <param name="book">low-level Workbook object containing the sheet that Contains this row</param>
        /// <param name="sheet">low-level Sheet object that Contains this Row</param>
        /// <param name="rowNum">the row number of this row (0 based)</param>
        ///<see cref="LF.Utils.NPOI.HSSF.UserModel.HSSFSheet.CreateRow(int)"/>
        public HSSFRow(HSSFWorkbook book, HSSFSheet sheet, int rowNum):this(book, sheet, new RowRecord(rowNum))
        {

        }
コード例 #15
0
ファイル: NPOIHelper.cs プロジェクト: missxiaohuang/Weekly
        public static Stream ListToExcel(object list, Dictionary<string, string> titles, bool IsExportAllCol)
        {
            const int startIndex = 0;
            var fields = titles.Keys.ToList();
            var workbook = new HSSFWorkbook();
            var sheet = workbook.CreateSheet("sheet1");
            sheet.DefaultRowHeight = 200 * 20;
            var row = sheet.CreateRow(startIndex);

            var headStyle = GetHeadStyle(workbook);
            EachHelper.EachListHeader(list, (i, name, type) =>
            {
                if (!fields.Contains(name))
                {
                    if (IsExportAllCol)
                        fields.Add(name);
                    else
                        return;
                }
                var cellIndex = fields.IndexOf(name) + startIndex;
                var cell = row.CreateCell(cellIndex);
                cell.SetCellValue(titles.ContainsKey(name)?titles[name]:name);
                cell.CellStyle = headStyle;
                sheet.AutoSizeColumn(cellIndex);
            });
            
            EachHelper.EachListRow(list, (rowIndex, dataRow) => 
            {
                row = sheet.CreateRow(rowIndex + 1);
                EachHelper.EachObjectProperty(dataRow, (i, name, value) => 
                {
                    if (!fields.Contains(name))
                    {
                        if (IsExportAllCol)
                            fields.Add(name);
                        else
                            return;
                    }
                    var cellIndex = fields.IndexOf(name) + startIndex;
                    var dataStyle = GetDataStyle(workbook);
                    var cell = row.CreateCell(cellIndex);
                    cell.CellStyle = dataStyle;
                    switch ((value??string.Empty).GetType().Name.ToLower())
                    {
                        case "int32":
                        case "int64":
                        case "decimal":
                            dataStyle.Alignment = HorizontalAlignment.RIGHT;
                            cell.SetCellValue(ZConvert.To<double>(value,0));
                            break;
                        default:
                            cell.CellStyle.Alignment = HorizontalAlignment.LEFT;
                            cell.SetCellValue(ZConvert.ToString(value));
                            break;
                    }
                });
            });

            var ms = new MemoryStream();
            workbook.Write(ms);
            ms.Flush();
            ms.Position = 0;

            workbook = null;
            sheet = null;
            row = null;

            return ms;
        }
コード例 #16
0
ファイル: NPOIHelper.cs プロジェクト: missxiaohuang/Weekly
        private static ICellStyle GetDataStyle(HSSFWorkbook workbook)
        {
            //数据样式
            var dataStyle = workbook.CreateCellStyle();
            dataStyle.Alignment = HorizontalAlignment.LEFT;//左对齐
            //数据单元格的边框
            dataStyle.BorderTop = BorderStyle.THIN;
            dataStyle.TopBorderColor = HSSFColor.BLACK.index;
            dataStyle.BorderRight = BorderStyle.THIN;
            dataStyle.RightBorderColor = HSSFColor.BLACK.index;
            dataStyle.BorderBottom = BorderStyle.THIN;
            dataStyle.BottomBorderColor = HSSFColor.BLACK.index;
            dataStyle.BorderLeft = BorderStyle.THIN;
            dataStyle.LeftBorderColor = HSSFColor.BLACK.index;
            //数据的字体
            var datafont = workbook.CreateFont();
            datafont.FontHeightInPoints = 11;//字号
            dataStyle.SetFont(datafont);

            return dataStyle;
        }
コード例 #17
0
 /**
  * Constructs an escher graphics object.
  *
  * @param escherGroup           The escher Group to Write the graphics calls into.
  * @param workbook              The workbook we are using.
  * @param foreground            The foreground color to use as default.
  * @param verticalPointsPerPixel    The font multiplier.  (See class description for information on how this works.).
  * @param font                  The font to use.
  */
 EscherGraphics(HSSFShapeGroup escherGroup, HSSFWorkbook workbook, Color foreground, Font font, float verticalPointsPerPixel)
 {
     this.escherGroup = escherGroup;
     this.workbook = workbook;
     this.foreground = foreground;
     //        this.background = background;
     this.font = font;
     this.verticalPointsPerPixel = verticalPointsPerPixel;
     this.verticalPixelsPerPoint = 1 / verticalPointsPerPixel;
 }
コード例 #18
0
        //[Obsolete]
        //public static void SetRightBorderColor(short color, Region region, HSSFSheet sheet,
        //        HSSFWorkbook workbook)
        //{
        //    SetRightBorderColor(color, toCRA(region), sheet, workbook);
        //}
        /// <summary>
        /// Sets the rightBorderColor attribute of the HSSFRegionUtil object
        /// </summary>
        /// <param name="color">The color of the border</param>
        /// <param name="region">The region that should have the border</param>
        /// <param name="sheet">The workbook that the region is on.</param>
        /// <param name="workbook">The sheet that the region is on.</param>
        public static void SetRightBorderColor(int color, CellRangeAddress region, HSSFSheet sheet,
                HSSFWorkbook workbook)
        {
            int rowStart = region.FirstRow;
            int rowEnd = region.LastRow;
            int column = region.LastColumn;

            CellPropertySetter cps = new CellPropertySetter(workbook, HSSFCellUtil.RIGHT_BORDER_COLOR, color);
            for (int i = rowStart; i <= rowEnd; i++)
            {
                cps.SetProperty(HSSFCellUtil.GetRow(i, sheet), column);
            }
        }
コード例 #19
0
 /// <summary>
 /// Sets the topBorderColor attribute of the HSSFRegionUtil object
 /// </summary>
 /// <param name="color">The color of the border</param>
 /// <param name="region">The region that should have the border</param>
 /// <param name="sheet">The sheet that the region is on.</param>
 /// <param name="workbook">The workbook that the region is on.</param>
 public static void SetTopBorderColor(int color, CellRangeAddress region, HSSFSheet sheet,
         HSSFWorkbook workbook)
 {
     int colStart = region.FirstColumn;
     int colEnd = region.LastColumn;
     int rowIndex = region.FirstRow;
     CellPropertySetter cps = new CellPropertySetter(workbook, HSSFCellUtil.TOP_BORDER_COLOR, color);
     LF.Utils.NPOI.SS.UserModel.IRow row = HSSFCellUtil.GetRow(rowIndex, sheet);
     for (int i = colStart; i <= colEnd; i++)
     {
         cps.SetProperty(row, i);
     }
 }
コード例 #20
0
ファイル: HSSFName.cs プロジェクト: missxiaohuang/Weekly
 /* package */
 internal HSSFName(HSSFWorkbook book, NameRecord name):this(book, name, null)
 {
     
 }
コード例 #21
0
ファイル: HSSFCell.cs プロジェクト: missxiaohuang/Weekly
        /// <summary>
        /// Creates an Cell from a CellValueRecordInterface.  HSSFSheet uses this when
        /// reading in cells from an existing sheet.
        /// </summary>
        /// <param name="book">Workbook record of the workbook containing this cell</param>
        /// <param name="sheet">Sheet record of the sheet containing this cell</param>
        /// <param name="cval">the Cell Value Record we wish to represent</param>
        public HSSFCell(HSSFWorkbook book, HSSFSheet sheet, CellValueRecordInterface cval)
        {
            record = cval;
            cellType = DetermineType(cval);
            stringValue = null;
            this.book = book;
            this.sheet = sheet;
            switch (cellType)
            {
                case CellType.STRING:
                    stringValue = new HSSFRichTextString(book.Workbook, (LabelSSTRecord)cval);
                    break;

                case CellType.BLANK:
                    break;

                case CellType.FORMULA:
                    stringValue = new HSSFRichTextString(((FormulaRecordAggregate)cval).StringValue);
                    break;
            }
            ExtendedFormatRecord xf = book.Workbook.GetExFormatAt(cval.XFIndex);

            CellStyle = new HSSFCellStyle((short)cval.XFIndex, xf, book);
        }
コード例 #22
0
        /**
         * Notifies this evaluation tracker that the evaluation of the specified
         * cell Is complete. <p/>
         * 
         * Every successful call to <c>startEvaluate</c> must be followed by a
         * call to <c>endEvaluate</c> (recommended in a finally block) to enable
         * proper tracking of which cells are being evaluated at any point in time.<p/>
         * 
         * Assuming a well behaved client, parameters to this method would not be
         * required. However, they have been included to assert correct behaviour,
         * and form more meaningful error messages.
         */
        public void EndEvaluate(HSSFWorkbook workbook, HSSFSheet sheet, int srcRowNum, int srcColNum)
        {
            int nFrames = _evaluationFrames.Count;
            if (nFrames < 1)
            {
                throw new InvalidOperationException("Call to endEvaluate without matching call to startEvaluate");
            }

            nFrames--;
            CellEvaluationFrame cefExpected = (CellEvaluationFrame)_evaluationFrames[nFrames];
            CellEvaluationFrame cefActual = new CellEvaluationFrame(workbook, sheet, srcRowNum, srcColNum);
            if (!cefActual.Equals(cefExpected))
            {
                throw new Exception("Wrong cell specified. "
                        + "Corresponding startEvaluate() call was for cell {"
                        + cefExpected.FormatAsString() + "} this endEvaluate() call Is for cell {"
                        + cefActual.FormatAsString() + "}");
            }
            // else - no problems so pop current frame 
            _evaluationFrames.Remove(nFrames);
        }
コード例 #23
0
ファイル: HSSFName.cs プロジェクト: missxiaohuang/Weekly
 /// <summary>
 /// Creates new HSSFName   - called by HSSFWorkbook to Create a sheet from
 /// scratch.
 /// </summary>
 /// <param name="book">lowlevel Workbook object associated with the sheet.</param>
 /// <param name="name">the Name Record</param>
 /// <param name="comment"></param>
 internal HSSFName(HSSFWorkbook book, NameRecord name, NameCommentRecord comment)
 {
     this.book = book;
     this._definedNameRec = name;
     _commentRec = comment;
 }
コード例 #24
0
ファイル: HSSFOptimiser.cs プロジェクト: missxiaohuang/Weekly
        /// <summary>
        /// Goes through the Wokrbook, optimising the cell styles
        /// by removing duplicate ones.
        /// For best results, optimise the fonts via a call to
        /// OptimiseFonts(HSSFWorkbook) first
        /// </summary>
        /// <param name="workbook">The workbook in which to optimise the cell styles</param>
        public static void OptimiseCellStyles(HSSFWorkbook workbook)
        {
            // Where each style has ended up, and if we need to
            //  delete the record for it. Start off with no change
            short[] newPos =
                new short[workbook.Workbook.NumExFormats];
            bool[] zapRecords = new bool[newPos.Length];
            for (int i = 0; i < newPos.Length; i++)
            {
                newPos[i] = (short)i;
                zapRecords[i] = false;
            }

            // Get each style record, so we can do deletes
            //  without Getting confused
            ExtendedFormatRecord[] xfrs = new ExtendedFormatRecord[newPos.Length];
            for (int i = 0; i < newPos.Length; i++)
            {
                xfrs[i] = workbook.Workbook.GetExFormatAt(i);
            }

            // Loop over each style, seeing if it is the same
            //  as an earlier one. If it is, point users of the
            //  later duplicate copy to the earlier one, and 
            //  mark the later one as needing deleting
            // Only work on user added ones, which come after 20
            for (int i = 21; i < newPos.Length; i++)
            {
                // Check this one for being a duplicate
                //  of an earlier one
                int earlierDuplicate = -1;
                for (int j = 0; j < i && earlierDuplicate == -1; j++)
                {
                    ExtendedFormatRecord xfCheck = workbook.Workbook.GetExFormatAt(j);
                    if (xfCheck.Equals(xfrs[i]))
                    {
                        earlierDuplicate = j;
                    }
                }

                // If we got a duplicate, mark it as such
                if (earlierDuplicate != -1)
                {
                    newPos[i] = (short)earlierDuplicate;
                    zapRecords[i] = true;
                }
            }

            // Update the new positions based on
            //  deletes that have occurred between
            //  the start and them
            // Only work on user added ones, which come after 20
            for (int i = 21; i < newPos.Length; i++)
            {
                // Find the number deleted to that
                //  point, and adjust
                short preDeletePos = newPos[i];
                short newPosition = preDeletePos;
                for (int j = 0; j < preDeletePos; j++)
                {
                    if (zapRecords[j]) newPosition--;
                }

                // Update the new position
                newPos[i] = newPosition;
            }

            // Zap the un-needed user style records
            for (int i = 21; i < newPos.Length; i++)
            {
                if (zapRecords[i])
                {
                    workbook.Workbook.RemoveExFormatRecord(
                            xfrs[i]
                    );
                }
            }

            // Finally, update the cells to point at
            //  their new extended format records
            for (int sheetNum = 0; sheetNum < workbook.NumberOfSheets; sheetNum++)
            {
                HSSFSheet s = (HSSFSheet)workbook.GetSheetAt(sheetNum);
                IEnumerator rIt = s.GetRowEnumerator();
                while (rIt.MoveNext())
                {
                    HSSFRow row = (HSSFRow)rIt.Current;
                    IEnumerator cIt = row.GetEnumerator();
                    while (cIt.MoveNext())
                    {
                        ICell cell = (HSSFCell)cIt.Current;
                        short oldXf = ((HSSFCell)cell).CellValueRecord.XFIndex;
                        LF.Utils.NPOI.SS.UserModel.ICellStyle newStyle = workbook.GetCellStyleAt(
                                newPos[oldXf]
                        );
                        cell.CellStyle = (newStyle);
                    }
                }
            }
        }
コード例 #25
0
ファイル: HSSFCell.cs プロジェクト: missxiaohuang/Weekly
 /// <summary>
 /// Creates new Cell - Should only be called by HSSFRow.  This Creates a cell
 /// from scratch.
 /// When the cell is initially Created it is Set to CellType.BLANK. Cell types
 /// can be Changed/overwritten by calling SetCellValue with the appropriate
 /// type as a parameter although conversions from one type to another may be
 /// prohibited.
 /// </summary>
 /// <param name="book">Workbook record of the workbook containing this cell</param>
 /// <param name="sheet">Sheet record of the sheet containing this cell</param>
 /// <param name="row">the row of this cell</param>
 /// <param name="col">the column for this cell</param>
 public HSSFCell(HSSFWorkbook book, HSSFSheet sheet, int row, short col)
     : this(book, sheet, row, col, CellType.BLANK)
 {
 }
コード例 #26
0
ファイル: HSSFOptimiser.cs プロジェクト: missxiaohuang/Weekly
        /// <summary>
        /// Goes through the Workbook, optimising the fonts by
        /// removing duplicate ones.
        /// For now, only works on fonts used in HSSFCellStyle
        /// and HSSFRichTextString. Any other font uses
        /// (eg charts, pictures) may well end up broken!
        /// This can be a slow operation, especially if you have
        /// lots of cells, cell styles or rich text strings
        /// </summary>
        /// <param name="workbook">The workbook in which to optimise the fonts</param>
        public static void OptimiseFonts(HSSFWorkbook workbook)
        {
            // Where each font has ended up, and if we need to
            //  delete the record for it. Start off with no change
            short[] newPos =
                new short[workbook.Workbook.NumberOfFontRecords + 1];
            bool[] zapRecords = new bool[newPos.Length];
            for (int i = 0; i < newPos.Length; i++)
            {
                newPos[i] = (short)i;
                zapRecords[i] = false;
            }

            // Get each font record, so we can do deletes
            //  without Getting confused
            FontRecord[] frecs = new FontRecord[newPos.Length];
            for (int i = 0; i < newPos.Length; i++)
            {
                // There is no 4!
                if (i == 4) continue;

                frecs[i] = workbook.Workbook.GetFontRecordAt(i);
            }

            // Loop over each font, seeing if it is the same
            //  as an earlier one. If it is, point users of the
            //  later duplicate copy to the earlier one, and 
            //  mark the later one as needing deleting
            // Note - don't change built in fonts (those before 5)
            for (int i = 5; i < newPos.Length; i++)
            {
                // Check this one for being a duplicate
                //  of an earlier one
                int earlierDuplicate = -1;
                for (int j = 0; j < i && earlierDuplicate == -1; j++)
                {
                    if (j == 4) continue;

                    FontRecord frCheck = workbook.Workbook.GetFontRecordAt(j);
                    if (frCheck.SameProperties(frecs[i]))
                    {
                        earlierDuplicate = j;
                    }
                }

                // If we got a duplicate, mark it as such
                if (earlierDuplicate != -1)
                {
                    newPos[i] = (short)earlierDuplicate;
                    zapRecords[i] = true;
                }
            }

            // Update the new positions based on
            //  deletes that have occurred between
            //  the start and them
            // Only need to worry about user fonts
            for (int i = 5; i < newPos.Length; i++)
            {
                // Find the number deleted to that
                //  point, and adjust
                short preDeletePos = newPos[i];
                short newPosition = preDeletePos;
                for (int j = 0; j < preDeletePos; j++)
                {
                    if (zapRecords[j]) newPosition--;
                }

                // Update the new position
                newPos[i] = newPosition;
            }

            // Zap the un-needed user font records
            for (int i = 5; i < newPos.Length; i++)
            {
                if (zapRecords[i])
                {
                    workbook.Workbook.RemoveFontRecord(
                            frecs[i]
                    );
                }
            }

            // Tell HSSFWorkbook that it needs to
            //  re-start its HSSFFontCache
            workbook.ResetFontCache();

            // Update the cell styles to point at the 
            //  new locations of the fonts
            for (int i = 0; i < workbook.Workbook.NumExFormats; i++)
            {
                ExtendedFormatRecord xfr = workbook.Workbook.GetExFormatAt(i);
                xfr.FontIndex = (
                        newPos[xfr.FontIndex]
                );
            }

            // Update the rich text strings to point at
            //  the new locations of the fonts
            // Remember that one underlying unicode string
            //  may be shared by multiple RichTextStrings!
            ArrayList doneUnicodeStrings = new ArrayList();
            for (int sheetNum = 0; sheetNum < workbook.NumberOfSheets; sheetNum++)
            {
                LF.Utils.NPOI.SS.UserModel.ISheet s = workbook.GetSheetAt(sheetNum);
                IEnumerator rIt = s.GetRowEnumerator();
                while (rIt.MoveNext())
                {
                    HSSFRow row = (HSSFRow)rIt.Current;
                    IEnumerator cIt = row.GetEnumerator();
                    while (cIt.MoveNext())
                    {
                        ICell cell = (HSSFCell)cIt.Current;
                        if (cell.CellType == LF.Utils.NPOI.SS.UserModel.CellType.STRING)
                        {
                            HSSFRichTextString rtr = (HSSFRichTextString)cell.RichStringCellValue;
                            UnicodeString u = rtr.RawUnicodeString;

                            // Have we done this string already?
                            if (!doneUnicodeStrings.Contains(u))
                            {
                                // Update for each new position
                                for (short i = 5; i < newPos.Length; i++)
                                {
                                    if (i != newPos[i])
                                    {
                                        u.SwapFontUse(i, newPos[i]);
                                    }
                                }

                                // Mark as done
                                doneUnicodeStrings.Add(u);
                            }
                        }
                    }
                }
            }
        }
コード例 #27
0
 public HSSFConditionalFormattingRule(HSSFWorkbook pWorkbook, CFRuleRecord pRuleRecord)
 {
     workbook = pWorkbook;
     cfRuleRecord = pRuleRecord;
 }
コード例 #28
0
 private static IFormulaParsingWorkbook CreateParsingWorkbook(HSSFWorkbook book)
 {
     return HSSFEvaluationWorkbook.Create(book);
 }
コード例 #29
0
 private HSSFEvaluationWorkbook(HSSFWorkbook book)
 {
     _uBook = book;
     _iBook = book.Workbook;
 }
コード例 #30
0
 /**
  * Convenience method for parsing cell formulas. see {@link #parse(String, HSSFWorkbook, int)}
  */
 public static Ptg[] Parse(String formula, HSSFWorkbook workbook)
 {
     return FormulaParser.Parse(formula, CreateParsingWorkbook(workbook));
 }