All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.poi.hssf.usermodel.HSSFOptimiser Maven / Gradle / Ivy

Go to download

The Apache Commons Codec package contains simple encoder and decoders for various formats such as Base64 and Hexadecimal. In addition to these widely used encoders and decoders, the codec package also maintains a collection of phonetic encoding utilities.

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 org.apache.poi.hssf.record.ExtendedFormatRecord;
import org.apache.poi.hssf.record.FontRecord;
import org.apache.poi.hssf.record.StyleRecord;
import org.apache.poi.hssf.record.common.UnicodeString;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;

/**
 * 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 doneUnicodeStrings = new HashSet<>();
		for(int sheetNum=0; sheetNum= newPos.length) {
				   		continue;
				   }
				   HSSFCellStyle newStyle = workbook.getCellStyleAt(newPos[oldXf]);
				   cell.setCellStyle(newStyle);
			   }

			   // adjust row column style
			   short oldXf = ((HSSFRow) row).getRowRecord().getXFIndex();
			   // some documents contain invalid values here
			   if(oldXf >= newPos.length) {
				   continue;
			   }
			   HSSFCellStyle newStyle = workbook.getCellStyleAt(newPos[oldXf]);
			   row.setRowStyle(newStyle);
		   }

		   // adjust cell column style
		   for (int col = s.getSheet().getMinColumnIndex(); col <= s.getSheet().getMaxColumnIndex(); col++) {
			   short oldXf = s.getSheet().getXFIndexForColAt((short) col);
			   // some documents contain invalid values here
			   if(oldXf >= newPos.length) {
				   continue;
			   }
			   HSSFCellStyle newStyle = workbook.getCellStyleAt(newPos[oldXf]);
			   s.setDefaultColumnStyle(col, newStyle);
		   }
	   }
   }

	private static boolean isUserDefined(HSSFWorkbook workbook, int index) {
		StyleRecord styleRecord = workbook.getWorkbook().getStyleRecord(index);
		return styleRecord != null &&
				!styleRecord.isBuiltin() &&
				styleRecord.getName() != null;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy