Fixed the double spacing found in this file.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@352860 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Glen Stampoultzis 2002-09-29 23:50:35 +00:00
parent 26e4e16719
commit 43f234b354
1 changed files with 251 additions and 228 deletions

View File

@ -1,6 +1,4 @@
/* ====================================================================
/*
* ====================================================================
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
* *
* Copyright (c) 2002 The Apache Software Foundation. All rights * Copyright (c) 2002 The Apache Software Foundation. All rights
@ -53,20 +51,19 @@
* information on the Apache Software Foundation, please see * information on the Apache Software Foundation, please see
* <http://www.apache.org/>. * <http://www.apache.org/>.
*/ */
package org.apache.poi.hssf.usermodel.contrib; package org.apache.poi.hssf.usermodel.contrib;
import java.util.*;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.commons.beanutils.PropertyUtils; import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.exception.NestableException;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.exception.NestableException;
import org.apache.poi.hssf.usermodel.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/** /**
* Various utility functions that make working with a cells and rows easier. The various * Various utility functions that make working with a cells and rows easier. The various
* methods that deal with style's allow you to create your HSSFCellStyles as you need them. * methods that deal with style's allow you to create your HSSFCellStyles as you need them.
@ -76,10 +73,11 @@ import org.apache.commons.lang.StringUtils;
* can be supported. * can be supported.
* *
*@author Eric Pugh epugh@upstate.com *@author Eric Pugh epugh@upstate.com
*@created July 29, 2002
*/ */
public class HSSFCellUtil { public class HSSFCellUtil
{
private static HashMap unicodeMappings = new HashMap(); private static HashMap unicodeMappings = new HashMap();
@ -90,14 +88,18 @@ public class HSSFCellUtil {
*@param sheet The sheet that the row is part of. *@param sheet The sheet that the row is part of.
*@return The row indicated by the rowCounter *@return The row indicated by the rowCounter
*/ */
public static HSSFRow getRow( int rowCounter, HSSFSheet sheet ) { public static HSSFRow getRow( int rowCounter, HSSFSheet sheet )
{
HSSFRow row = sheet.getRow( (short) rowCounter ); HSSFRow row = sheet.getRow( (short) rowCounter );
if ( row == null ) { if ( row == null )
{
row = sheet.createRow( (short) rowCounter ); row = sheet.createRow( (short) rowCounter );
} }
return row; return row;
} }
/** /**
* Get a specific cell from a row. If the cell doesn't exist, then create it. * Get a specific cell from a row. If the cell doesn't exist, then create it.
* *
@ -105,9 +107,12 @@ public class HSSFCellUtil {
*@param column The column index that the cell is in. *@param column The column index that the cell is in.
*@return The cell indicated by the column. *@return The cell indicated by the column.
*/ */
public static HSSFCell getCell( HSSFRow row, int column ) { public static HSSFCell getCell( HSSFRow row, int column )
{
HSSFCell cell = row.getCell( (short) column ); HSSFCell cell = row.getCell( (short) column );
if ( cell == null ) {
if ( cell == null )
{
cell = row.createCell( (short) column ); cell = row.createCell( (short) column );
} }
return cell; return cell;
@ -117,22 +122,27 @@ public class HSSFCellUtil {
/** /**
* Creates a cell, gives it a value, and applies a style if provided * Creates a cell, gives it a value, and applies a style if provided
* *
*@param row the row to create the cell in * @param row the row to create the cell in
*@param column the column index to create the cell in * @param column the column index to create the cell in
*@param value The value of the cell * @param value The value of the cell
*@param style If the style is not null, then set * @param style If the style is not null, then set
*@return A new HSSFCell * @return A new HSSFCell
*/ */
public static HSSFCell createCell( HSSFRow row, int column, String value, HSSFCellStyle style ) {
public static HSSFCell createCell( HSSFRow row, int column, String value, HSSFCellStyle style )
{
HSSFCell cell = getCell( row, column ); HSSFCell cell = getCell( row, column );
cell.setCellValue( value ); cell.setCellValue( value );
if ( style != null ) { if ( style != null )
{
cell.setCellStyle( style ); cell.setCellStyle( style );
} }
return cell; return cell;
} }
/** /**
* Create a cell, and give it a value. * Create a cell, and give it a value.
* *
@ -141,22 +151,23 @@ public class HSSFCellUtil {
*@param value The value of the cell *@param value The value of the cell
*@return A new HSSFCell. *@return A new HSSFCell.
*/ */
public static HSSFCell createCell( HSSFRow row, int column, String value ) { public static HSSFCell createCell( HSSFRow row, int column, String value )
{
return createCell( row, column, value, null ); return createCell( row, column, value, null );
} }
/** /**
* Take a cell, and align it. * Take a cell, and align it.
* *
*@param cell the cell to set the alignment for *@param cell the cell to set the alignment for
*@param workbook The workbook that is being worked with. *@param workbook The workbook that is being worked with.
*@param align the column alignment to use. *@param align the column alignment to use.
*@return The HSSFCell aligned.
*@exception NestableException Thrown if an error happens. *@exception NestableException Thrown if an error happens.
*/ */
public static void setAlignment(HSSFCell cell, HSSFWorkbook workbook, short align) throws NestableException { public static void setAlignment( HSSFCell cell, HSSFWorkbook workbook, short align ) throws NestableException
setCellStyleProperty(cell, workbook, "alignment", new Short(align) ); {
setCellStyleProperty( cell, workbook, "alignment", new Short( align ) );
} }
/** /**
@ -165,13 +176,13 @@ public class HSSFCellUtil {
*@param cell the cell to set the alignment for *@param cell the cell to set the alignment for
*@param workbook The workbook that is being worked with. *@param workbook The workbook that is being worked with.
*@param font The HSSFFont that you want to set... *@param font The HSSFFont that you want to set...
*@return The HSSFCell with the font
*@exception NestableException Thrown if an error happens. *@exception NestableException Thrown if an error happens.
*/ */
public static void setFont(HSSFCell cell, HSSFWorkbook workbook, HSSFFont font) throws NestableException { public static void setFont( HSSFCell cell, HSSFWorkbook workbook, HSSFFont font ) throws NestableException
setCellStyleProperty(cell, workbook, "font", font ); {
setCellStyleProperty( cell, workbook, "font", font );
} }
/** /**
* This method attempt to find an already existing HSSFCellStyle that matches * This method attempt to find an already existing HSSFCellStyle that matches
* what you want the style to be. If it does not find the style, then it * what you want the style to be. If it does not find the style, then it
@ -187,31 +198,37 @@ public class HSSFCellUtil {
*@param cell The cell that needs it's style changes *@param cell The cell that needs it's style changes
*@exception NestableException Thrown if an error happens. *@exception NestableException Thrown if an error happens.
*/ */
public static void setCellStyleProperty( HSSFCell cell, HSSFWorkbook workbook, String propertyName, Object propertyValue ) public static void setCellStyleProperty( HSSFCell cell, HSSFWorkbook workbook, String propertyName, Object propertyValue )
throws NestableException { throws NestableException
try { {
try
{
HSSFCellStyle originalStyle = cell.getCellStyle(); HSSFCellStyle originalStyle = cell.getCellStyle();
HSSFCellStyle newStyle = null; HSSFCellStyle newStyle = null;
Map values = PropertyUtils.describe( originalStyle ); Map values = PropertyUtils.describe( originalStyle );
values.put( propertyName, propertyValue ); values.put( propertyName, propertyValue );
values.remove( "index" ); values.remove( "index" );
// index seems like what index the cellstyle is in the list of styles for a workbook. // index seems like what index the cellstyle is in the list of styles for a workbook.
// not good to compare on! // not good to compare on!
short numberCellStyles = workbook.getNumCellStyles(); short numberCellStyles = workbook.getNumCellStyles();
for ( short i = 0; i < numberCellStyles; i++ ) { for ( short i = 0; i < numberCellStyles; i++ )
{
HSSFCellStyle wbStyle = workbook.getCellStyleAt( i ); HSSFCellStyle wbStyle = workbook.getCellStyleAt( i );
Map wbStyleMap = PropertyUtils.describe( wbStyle ); Map wbStyleMap = PropertyUtils.describe( wbStyle );
wbStyleMap.remove( "index" ); wbStyleMap.remove( "index" );
if ( wbStyleMap.equals( values ) ) { if ( wbStyleMap.equals( values ) )
{
newStyle = wbStyle; newStyle = wbStyle;
break; break;
} }
} }
if ( newStyle == null ) { if ( newStyle == null )
{
newStyle = workbook.createCellStyle(); newStyle = workbook.createCellStyle();
newStyle.setFont( workbook.getFontAt( originalStyle.getFontIndex() ) ); newStyle.setFont( workbook.getFontAt( originalStyle.getFontIndex() ) );
PropertyUtils.copyProperties( newStyle, originalStyle ); PropertyUtils.copyProperties( newStyle, originalStyle );
@ -220,8 +237,10 @@ public class HSSFCellUtil {
cell.setCellStyle( newStyle ); cell.setCellStyle( newStyle );
} }
catch ( Exception e ) { catch ( Exception e )
{
e.printStackTrace(); e.printStackTrace();
throw new NestableException( "Couldn't setCellStyleProperty.", e ); throw new NestableException( "Couldn't setCellStyleProperty.", e );
} }
} }
@ -234,22 +253,24 @@ public class HSSFCellUtil {
*@param cell The cell to check for unicode values *@param cell The cell to check for unicode values
*@return transalted to unicode *@return transalted to unicode
*/ */
public static HSSFCell translateUnicodeValues( HSSFCell cell ) { public static HSSFCell translateUnicodeValues( HSSFCell cell )
{
String s = cell.getStringCellValue(); String s = cell.getStringCellValue();
boolean foundUnicode = false; boolean foundUnicode = false;
for ( Iterator i = unicodeMappings.entrySet().iterator(); i.hasNext(); ) { for ( Iterator i = unicodeMappings.entrySet().iterator(); i.hasNext(); )
{
Map.Entry entry = (Map.Entry) i.next(); Map.Entry entry = (Map.Entry) i.next();
String key = (String) entry.getKey(); String key = (String) entry.getKey();
if ( s.toLowerCase().indexOf( key ) != -1 ) { if ( s.toLowerCase().indexOf( key ) != -1 )
{
s = StringUtils.replace( s, key, "" + entry.getValue().toString() + "" ); s = StringUtils.replace( s, key, "" + entry.getValue().toString() + "" );
foundUnicode = true; foundUnicode = true;
} }
} }
if ( foundUnicode )
if ( foundUnicode ) { {
cell.setEncoding( HSSFCell.ENCODING_UTF_16 ); cell.setEncoding( HSSFCell.ENCODING_UTF_16 );
cell.setCellValue( s ); cell.setCellValue( s );
} }
@ -257,12 +278,14 @@ public class HSSFCellUtil {
} }
static { static
{
unicodeMappings.put( "&alpha;", "\u03B1" ); unicodeMappings.put( "&alpha;", "\u03B1" );
unicodeMappings.put( "&gamma;", "\u03B3" ); unicodeMappings.put( "&gamma;", "\u03B3" );
unicodeMappings.put( "&theta;", "\u03B8" ); unicodeMappings.put( "&theta;", "\u03B8" );
} }
} }