org.apache.poi.hssf.usermodel.HSSFOptimiser Maven / Gradle / Ivy
The newest version!
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.usermodel;
import java.util.HashSet;
import java.util.Iterator;
import org.apache.poi.hssf.record.ExtendedFormatRecord;
import org.apache.poi.hssf.record.FontRecord;
import org.apache.poi.hssf.record.common.UnicodeString;
/**
* Excel can get cranky if you give it files containing too
* many (especially duplicate) objects, and this class can
* help to avoid those.
* In general, it's much better to make sure you don't
* duplicate the objects in your code, as this is likely
* to be much faster than creating lots and lots of
* excel objects+records, only to optimise them down to
* many fewer at a later stage.
* However, sometimes this is too hard / tricky to do, which
* is where the use of this class comes in.
*/
public class HSSFOptimiser {
/**
* Goes through the Workbook, optimising the fonts by
* removing duplicate ones.
* For now, only works on fonts used in {@link HSSFCellStyle}
* and {@link 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
* @param workbook The workbook in which to optimise the fonts
*/
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.getWorkbook().getNumberOfFontRecords()+1];
boolean[] zapRecords = new boolean[newPos.length];
for(int i=0; i