Jakata Poi HSSF:纯java的Excel解决方案 & 用POI的HSSF来控制EXCEL的研究

Jakata Poi HSSF:纯java的Excel解决方案
2007年01月15日 星期一 下午 08:16
微软在桌面 系统上的成功,令我们不得不大量使用它的办公产品,如:Word,Excel。时至今日,它的源代码仍然不公开已封锁了我们的进一步应用和开发。然而在要求更高的 服务器领域, 微软本身的产品移植性不好, 
性能不佳。在我们实际的开发中,表现层的解决方案虽然有多样,但是Ie浏览器已成为最多人使用的浏览器,因为大家都用Windows。在企业办公 系统中,常常有客户这样子要求:你要把我们的报表直接用Excel打开。或者是:我们已经习惯用Excel打印。这样子如果用.net开发是没有问题的,但是有j2ee这个比.net更有前途的开放式的开发环境,难道我为了解决打印的要求去另写客户端的控件?或者在 服务器端使用本地代码?第一种方案的问题是关键数据的处理有时候不能在客户端做,第2种方案的问题是牺牲了代码的可移植性和稳定性。如果让客户端只负责处理生成好的报表,那将是一种诱人的选择。

Apache的Jakata项目的POI子项目,目标是处理ole2对象。目前比较成熟的是HSSF接口,处理MS Excel(97-2002)对象。它不象我们仅仅是用csv生成的没有格式的可以由Excel转换的东西,而是真正的Excel对象,你可以控制一些属性如sheet,cell等等。这是一个年轻的项目,所以象HDF这样直接支持Word对象的好东西仍然在设计中。其它支持word格式的纯 java方案还有itext,不过也是仍在奋斗中。但是HSSF已经成熟到能够和足够我们使用了。另外,无锡永中Office的实现方案也是纯 java的解决方案,不过那也是完全商业的产品,并不是公开代码项目。其实,从开发历史的角度讲,在80年代中期starOffice的原作者在德国成立了StarOffice suite公司,然后到1999年夏天starOffice被sun收购,再到2000年6月starOffice5.2的发布;并且从starOffice6.0开始,starOffice建立在OpenOffice的api的基础上,这个公开代码的 office项目已经进行了很长的时间。虽然那是由C++写的,但是POI的代码部分也是由openOffice改过来的。所以,应该对POI充满足够的信心。国内已经有部分公司在他们的办公自动化等Web项目中使用poi了,如日恒的i office,海泰的HTOffice等。

java当初把核心处理设成Unicode,带来的好处是另代码适应了多语言环境。然而由于老外的英语只有26个字母,有些情况下,一些 程序员用8位的byte处理,一不小心就去掉了CJK的高位。或者是由于习惯在 程序中采用硬编码,还有多种原因,使得许多 java应用在CJK的处理上很烦恼。还好在POI HSSF中考虑到这个问题,可以设置encoding为双字节。

POI可以到www.apache.org下载到。编译好的jar主要有这样4个:poi包,poi Browser包,poi hdf包,poi hssf例程包。实际运行时,需要有poi包就可以了。如果用Jakarta ant编译和运行,下载apache Jakarta POI的release中的src包,它里面已经为你生成好了build 文件了。只要运行ant就可以了(ant 的安装和使用在此不说了)。如果是用Jbuilder 运行,请在新建的项目中加入poi包。以Jbuilder6为例,选择Tools菜单项的config libraries...选项,新建一个lib。在弹出的菜单中选择poi包,如这个jakarta-poi-1.5.1-final-20020820.jar,把poi添加到jbuilder中。然后,右键点击你的项目,在project的properties菜单中path的required Libraries中,点add,添加刚才加入到jbuilder中的poi到你现在的项目中。如果你仅仅是为了熟悉POI hssf的使用,可以直接看POI的samples包中的源代码,并且运行它。hssf的各种对象都有例程的介绍。hssf提供的例程在org.apache.poi.hssf.usermodel.examples包中,共有14个,生成的目标xls都是workbook.xls。如果你想看更多的例程,可以参考hssf的Junit test cases,在poi的包的源代码中有。hssf都有测试代码。

这里只对部分例程的实现做介绍。

HSSF提供给用户使用的对象在org.apache.poi.hssf.usermodel包中,主要部分包括Excell对象,样式和格式,还有辅助操作。有以下几种对象:

HSSFWorkbook excell的文档对象

HSSFSheet excell的表单

HSSFRow excell的行 

HSSFCell excell的格子单元

HSSFFont excell字体

HSSFName 名称

HSSFDataFormat 日期格式

在poi1.7中才有以下2项:

HSSFHeader sheet头

HSSFFooter sheet尾

和这个样式

HSSFCellStyle cell样式

辅助操作包括

HSSFDateUtil 日期

HSSFPrintSetup 打印 

HSSFErrorConstants 错误信息表

仔细看org.apache.poi.hssf包的结构,不难发现HSSF的内部实现遵循的是M VC模型。

这里我用Rose把org.apache.poi.hssf.usermodel包中的对象反向导入并根据相互关系作了整理,详见下面两图:


图1 基本对象 

从中不难可以发现每一个基本对象都关联了一个Record对象。Record对象是一个参考Office格式的相关记录。


图2 HSSFWorkbook

HSSFWorkbook即是一个Excell对象。这幅类图体现的是HSSFWorkbook和基本对象的相互关系。可见,许多对象中也建立了Workbook的引用。还需要注意的是在HSSFWorkbook和HSSFSheet中建立了log机制POILogger,而且POILogger也是使用apache Log4J实现的。

先看poi的examples包中提供的最简单的例子,建立一个空xls 文件

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import  java.io.FileOutputStream;

import  java.io.IOException;

public class NewWorkbook

{

public static void main(String[] args)

throws IOException

{

HSSFWorkbook wb = new HSSFWorkbook();//建立新HSSFWorkbook对象

FileOutputStream fileOut = new FileOutputStream("workbook.xls");

wb.write(fileOut);//把Workbook对象输出到 文件workbook.xls中

fileOut.close();

}

}

通过这个例子,我们建立的是一个空白的xls 文件(不是空 文件)。在此基础上,我们可以进一步看其它的例子。


import org.apache.poi.hssf.usermodel.*;

import  java.io.FileOutputStream;

import  java.io.IOException;

public class CreateCells

{

public static void main(String[] args)

throws IOException

{

HSSFWorkbook wb = new HSSFWorkbook();//建立新HSSFWorkbook对象

HSSFSheet sheet = wb.createSheet("new sheet");//建立新的sheet对象


// Create a row and put some cells in it. Rows are 0 based.

HSSFRow row = sheet.createRow((short)0);//建立新行

// Create a cell and put a value in it.

HSSFCell cell = row.createCell((short)0);//建立新cell

cell.setCellvalue(1);//设置cell的整数类型的值


// Or do it on one line.

row.createCell((short)1).setCellvalue(1.2);//设置cell浮点类型的值

row.createCell((short)2).setCellvalue("test");//设置cell 字符类型的值

row.createCell((short)3).setCellvalue(true);//设置cell布尔类型的值 

HSSFCellStyle cellStyle = wb.createCellStyle();//建立新的cell样式

cellStyle.setDataFormat(HSSFDataFormat.getFormat("m/d/yy h:mm"));//设置cell样式为定制的日期格式

HSSFCell dCell =row.createCell((short)4);

dCell.setCellvalue(new Date());//设置cell为日期类型的值

dCell.setCellStyle(cellStyle); //设置该cell日期的显示格式

HSSFCell csCell =row.createCell((short)5);

csCell.setEncoding(HSSFCell.ENCODING_UTF_16);//设置cell编码解决中文高位字节截断

csCell.setCellvalue("中文测试_Chinese Words Test");//设置中西文结合 字符

row.createCell((short)6).setCellType(HSSFCell.CELL_TYPE_ERROR);//建立错误cell


// Write the output to a file

FileOutputStream fileOut = new FileOutputStream("workbook.xls");

wb.write(fileOut);

fileOut.close();

}

}

我稍微修改了原来的examples包中的CreateCells类写了上面的功能测试类。通过这个例子,我们可以清楚的看到xls 文件从大到小包括了HSSFWorkbook HSSFSheet HSSFRow HSSFCell这样几个对象。我们可以在cell中设置各种类型的值。尤其要注意的是如果你想正确的显示非欧美的 字符时,尤其象中日韩这样的语言,必须设置编码为16位的即是HSSFCell.ENCODING_UTF_16,才能保证 字符的高8位不被截断而引起编码失真形成乱码。

其他测试可以通过参考examples包中的测试例子掌握poi的详细用法,包括字体的设置,cell大小和低纹的设置等。需要注意的是POI是一个仍然在完善中的公开代码的项目,所以有些功能正在不断的扩充。如HSSFSheet的getFooter() getHeader()和setFooter(HSSFFooter hsf) setHeader(HSSFHeader hsh)是在POI1.7中才有的,而POI1.5中就没有。运行测试熟悉代码或者使用它做项目时请注意POI的版本。

另外需要注意的是HSSF也有它的对xls基于事件的解析。可以参考例程中的EventExample. java。它通过实现HSSFListener完成从普通流认知Xls中包含的内容,在apache Cocoon中的org.apache.cocoon.serialization.HSSFSerializer中用到了这个解析。因为Cocoon2是基于事件的,所以POI为了提供快速的解析也提供了相应的事件。当然我们自己也可以实现这个事件接口。

因为POI还不是一个足够成熟的项目,所以有必要做进一步的开发和测试。但是它已经为我们用纯 java操作ole2对象提供了可能,而且克服了ole对象调用的缺陷,提供了 服务器端的Excel解决方案。

 

用POI的HSSF来控制EXCEL的研究
1.创建工作簿 (WORKBOOK)
    HSSFWorkbook wb = new HSSFWorkbook();
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
2.创建工作表(SHEET)
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet1 = wb.createSheet("new sheet");
    HSSFSheet sheet2 = wb.createSheet("second sheet");
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
3.创建单元格(CELL)
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("new sheet");
    // Create a row and put some cells in it. Rows are 0 based.
    HSSFRow row = sheet.createRow((short)0);
    // Create a cell and put a value in it.
    HSSFCell cell = row.createCell((short)0);
    cell.setCellValue(1);
    // Or do it on one line.
    row.createCell((short)1).setCellValue(1.2);
    row.createCell((short)2).setCellValue("This is a string");
    row.createCell((short)3).setCellValue(true);
    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
4.创建指定单元格式的单元格
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("new sheet");
    // Create a row and put some cells in it. Rows are 0 based.
    HSSFRow row = sheet.createRow((short)0);
    // Create a cell and put a date value in it.  The first cell is not styled
    // as a date.
    HSSFCell cell = row.createCell((short)0);
    cell.setCellValue(new Date());
    // we style the second cell as a date (and time).  It is important to
    // create a new cell style from the workbook otherwise you can end up
    // modifying the built in style and effecting not only this cell but other cells.
    HSSFCellStyle cellStyle = wb.createCellStyle();
    cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
    cell = row.createCell((short)1);
    cell.setCellValue(new Date());
    cell.setCellStyle(cellStyle);
    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
5. 单元格的不同格式
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("new sheet");
    HSSFRow row = sheet.createRow((short)2);
    row.createCell((short) 0).setCellValue(1.1);
    row.createCell((short) 1).setCellValue(new Date());
    row.createCell((short) 2).setCellValue("a string");
    row.createCell((short) 3).setCellValue(true);
    row.createCell((short) 4).setCellType(HSSFCell.CELL_TYPE_ERROR);
    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
6.单元格的不通对齐方式
    public static void main(String[] args)
            throws IOException
    {
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("new sheet");
        HSSFRow row = sheet.createRow((short) 2);
        createCell(wb, row, (short) 0, HSSFCellStyle.ALIGN_CENTER);
        createCell(wb, row, (short) 1, HSSFCellStyle.ALIGN_CENTER_SELECTION);
        createCell(wb, row, (short) 2, HSSFCellStyle.ALIGN_FILL);
        createCell(wb, row, (short) 3, HSSFCellStyle.ALIGN_GENERAL);
        createCell(wb, row, (short) 4, HSSFCellStyle.ALIGN_JUSTIFY);
        createCell(wb, row, (short) 5, HSSFCellStyle.ALIGN_LEFT);
        createCell(wb, row, (short) 6, HSSFCellStyle.ALIGN_RIGHT);
        // Write the output to a file
        FileOutputStream fileOut = new FileOutputStream("workbook.xls");
        wb.write(fileOut);
        fileOut.close();
    }
    /**
     * Creates a cell and aligns it a certain way.
     *
     * @param wb        the workbook
     * @param row       the row to create the cell in
     * @param column    the column number to create the cell in
     * @param align     the alignment for the cell.
     */
    private static void createCell(HSSFWorkbook wb, HSSFRow row, short column, short align)
    {
        HSSFCell cell = row.createCell(column);
        cell.setCellValue("Align It");
        HSSFCellStyle cellStyle = wb.createCellStyle();
        cellStyle.setAlignment(align);
        cell.setCellStyle(cellStyle);
    }
7.单元格的边框设置
Working with borders
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("new sheet");
    // Create a row and put some cells in it. Rows are 0 based.
    HSSFRow row = sheet.createRow((short) 1);
    // Create a cell and put a value in it.
    HSSFCell cell = row.createCell((short) 1);
    cell.setCellValue(4);
    // Style the cell with borders all around.
    HSSFCellStyle style = wb.createCellStyle();
    style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    style.setBottomBorderColor(HSSFColor.BLACK.index);
    style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    style.setLeftBorderColor(HSSFColor.GREEN.index);
    style.setBorderRight(HSSFCellStyle.BORDER_THIN);
    style.setRightBorderColor(HSSFColor.BLUE.index);
    style.setBorderTop(HSSFCellStyle.BORDER_MEDIUM_DASHED);
    style.setTopBorderColor(HSSFColor.BLACK.index);
    cell.setCellStyle(style);
    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
8.填充和颜色设置
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("new sheet");
    // Create a row and put some cells in it. Rows are 0 based.
    HSSFRow row = sheet.createRow((short) 1);
    // Aqua background
    HSSFCellStyle style = wb.createCellStyle();
    style.setFillBackgroundColor(HSSFColor.AQUA.index);
    style.setFillPattern(HSSFCellStyle.BIG_SPOTS);
    HSSFCell cell = row.createCell((short) 1);
    cell.setCellValue("X");
    cell.setCellStyle(style);
    // Orange "foreground", foreground being the fill foreground not the font color.
    style = wb.createCellStyle();
    style.setFillForegroundColor(HSSFColor.ORANGE.index);
    style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    cell = row.createCell((short) 2);
    cell.setCellValue("X");
    cell.setCellStyle(style);
    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
9.合并单元格操作
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("new sheet");
    HSSFRow row = sheet.createRow((short) 1);
    HSSFCell cell = row.createCell((short) 1);
    cell.setCellValue("This is a test of merging");
    sheet.addMergedRegion(new Region(1,(short)1,1,(short)2));
    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
10.字体设置
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("new sheet");
    // Create a row and put some cells in it. Rows are 0 based.
    HSSFRow row = sheet.createRow((short) 1);
    // Create a new font and alter it.
    HSSFFont font = wb.createFont();
    font.setFontHeightInPoints((short)24);
    font.setFontName("Courier New");
    font.setItalic(true);
    font.setStrikeout(true);
    // Fonts are set into a style so create a new one to use.
    HSSFCellStyle style = wb.createCellStyle();
    style.setFont(font);
    // Create a cell and put a value in it.
    HSSFCell cell = row.createCell((short) 1);
    cell.setCellValue("This is a test of fonts");
    cell.setCellStyle(style);
    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
11.自定义颜色
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet();
    HSSFRow row = sheet.createRow((short) 0);
    HSSFCell cell = row.createCell((short) 0);
    cell.setCellValue("Default Palette");
    //apply some colors from the standard palette,
    // as in the previous examples.
    //we'll use red text on a lime background
    HSSFCellStyle style = wb.createCellStyle();
    style.setFillForegroundColor(HSSFColor.LIME.index);
    style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    HSSFFont font = wb.createFont();
    font.setColor(HSSFColor.RED.index);
    style.setFont(font);
    cell.setCellStyle(style);
    //save with the default palette
    FileOutputStream out = new FileOutputStream("default_palette.xls");
    wb.write(out);
    out.close();
    //now, let's replace RED and LIME in the palette
    // with a more attractive combination
    // (lovingly borrowed from freebsd.org)
    cell.setCellValue("Modified Palette");
    //creating a custom palette for the workbook
    HSSFPalette palette = wb.getCustomPalette();
    //replacing the standard red with freebsd.org red
    palette.setColorAtIndex(HSSFColor.RED.index,
            (byte) 153,  //RGB red (0-255)
            (byte) 0,    //RGB green
            (byte) 0     //RGB blue
    );
    //replacing lime with freebsd.org gold
    palette.setColorAtIndex(HSSFColor.LIME.index, (byte) 255, (byte) 204, (byte) 102);
    //save with the modified palette
    // note that wherever we have previously used RED or LIME, the
    // new colors magically appear
    out = new FileOutputStream("modified_palette.xls");
    wb.write(out);
    out.close();
12.读和重写EXCEL文件
    POIFSFileSystem fs      =
            new POIFSFileSystem(new FileInputStream("workbook.xls"));
    HSSFWorkbook wb = new HSSFWorkbook(fs);
    HSSFSheet sheet = wb.getSheetAt(0);
    HSSFRow row = sheet.getRow(2);
    HSSFCell cell = row.getCell((short)3);
    if (cell == null)
        cell = row.createCell((short)3);
    cell.setCellType(HSSFCell.CELL_TYPE_STRING);
    cell.setCellValue("a test");
    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
13.在EXCEL单元格中使用自动换行
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet s = wb.createSheet();
    HSSFRow r = null;
    HSSFCell c = null;
    HSSFCellStyle cs = wb.createCellStyle();
    HSSFFont f = wb.createFont();
    HSSFFont f2 = wb.createFont();
    cs = wb.createCellStyle();
    cs.setFont( f2 );
    //Word Wrap MUST be turned on
    cs.setWrapText( true );
    r = s.createRow( (short) 2 );
    r.setHeight( (short) 0x349 );
    c = r.createCell( (short) 2 );
    c.setCellType( HSSFCell.CELL_TYPE_STRING );
    c.setCellValue( "Use /n with word wrap on to create a new line" );
    c.setCellStyle( cs );
    s.setColumnWidth( (short) 2, (short) ( ( 50 * 8 ) / ( (double) 1 / 20 ) ) );
    FileOutputStream fileOut = new FileOutputStream( "workbook.xls" );
    wb.write( fileOut );
    fileOut.close();
14.数字格式自定义
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("format sheet");
    HSSFCellStyle style;
    HSSFDataFormat format = wb.createDataFormat();
    HSSFRow row;
    HSSFCell cell;
    short rowNum = 0;
    short colNum = 0;
    row = sheet.createRow(rowNum++);
    cell = row.createCell(colNum);
    cell.setCellValue(11111.25);
    style = wb.createCellStyle();
    style.setDataFormat(format.getFormat("0.0"));
    cell.setCellStyle(style);
    row = sheet.createRow(rowNum++);
    cell = row.createCell(colNum);
    cell.setCellValue(11111.25);
    style = wb.createCellStyle();
    style.setDataFormat(format.getFormat("#,##0.0000"));
    cell.setCellStyle(style);
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
15.调整工作单位置
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("format sheet");
    HSSFPrintSetup ps = sheet.getPrintSetup();
    sheet.setAutobreaks(true);
    ps.setFitHeight((short)1);
    ps.setFitWidth((short)1);

    // Create various cells and rows for spreadsheet.
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
16.设置打印区域
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("Sheet1");
    wb.setPrintArea(0, "$A$1:$C$2");
    //sets the print area for the first sheet
    //Alternatively:
    //wb.setPrintArea(0, 0, 1, 0, 0) is equivalent to using the name reference (See the JavaDocs for more details)
    // Create various cells and rows for spreadsheet.
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
17.标注脚注
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("format sheet");
    HSSFFooter footer = sheet.getFooter()
    footer.setRight( "Page " + HSSFFooter.page() + " of " + HSSFFooter.numPages() );
 
    // Create various cells and rows for spreadsheet.
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
18.使用方便的内部提供的函数
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet1 = wb.createSheet( "new sheet" );
    // Create a merged region
    HSSFRow row = sheet1.createRow( (short) 1 );
    HSSFRow row2 = sheet1.createRow( (short) 2 );
    HSSFCell cell = row.createCell( (short) 1 );
    cell.setCellValue( "This is a test of merging" );
    Region region = new Region( 1, (short) 1, 4, (short) 4 );
    sheet1.addMergedRegion( region );
    // Set the border and border colors.
    final short borderMediumDashed = HSSFCellStyle.BORDER_MEDIUM_DASHED;
    HSSFRegionUtil.setBorderBottom( borderMediumDashed,
        region, sheet1, wb );
    HSSFRegionUtil.setBorderTop( borderMediumDashed,
        region, sheet1, wb );
    HSSFRegionUtil.setBorderLeft( borderMediumDashed,
        region, sheet1, wb );
    HSSFRegionUtil.setBorderRight( borderMediumDashed,
        region, sheet1, wb );
    HSSFRegionUtil.setBottomBorderColor(HSSFColor.AQUA.index, region, sheet1, wb);
    HSSFRegionUtil.setTopBorderColor(HSSFColor.AQUA.index, region, sheet1, wb);
    HSSFRegionUtil.setLeftBorderColor(HSSFColor.AQUA.index, region, sheet1, wb);
    HSSFRegionUtil.setRightBorderColor(HSSFColor.AQUA.index, region, sheet1, wb);
    // Shows some usages of HSSFCellUtil
    HSSFCellStyle style = wb.createCellStyle();
    style.setIndention((short)4);
    HSSFCellUtil.createCell(row, 8, "This is the value of the cell", style);
    HSSFCell cell2 = HSSFCellUtil.createCell( row2, 8, "This is the value of the cell");
    HSSFCellUtil.setAlignment(cell2, wb, HSSFCellStyle.ALIGN_CENTER);
    // Write out the workbook
    FileOutputStream fileOut = new FileOutputStream( "workbook.xls" );
    wb.write( fileOut );
    fileOut.close();
19.在工作单中移动行,调整行的上下位置
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("row sheet");
    // Create various cells and rows for spreadsheet.
    // Shift rows 6 - 11 on the spreadsheet to the top (rows 0 - 5)
    sheet.shiftRows(5, 10, -5);
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
20.选种指定的工作单
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("row sheet");
    sheet.setSelected(true);
    // Create various cells and rows for spreadsheet.
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
21.工作单的放大缩小
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet1 = wb.createSheet("new sheet");
    sheet1.setZoom(3,4);   // 75 percent magnification
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
22.头注和脚注
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("new sheet");
    HSSFHeader header = sheet.getHeader();
    header.setCenter("Center Header");
    header.setLeft("Left Header");
    header.setRight(HSSFHeader.font("Stencil-Normal", "Italic") +
                    HSSFHeader.fontSize((short) 16) + "Right w/ Stencil-Normal Italic font and size 16");
    FileOutputStream fileOut = new FileOutputStream("workbook.xls");
    wb.write(fileOut);
    fileOut.close();
//-------------------------------以上实例代码均来自官方网站
//-------------------------------POI中使用的颜色是用颜色索引来实现,如下:
   /*
    * 颜色对照表 数字代表颜色索引
        8: BLACK
        60: BROWN
        59: OLIVE_GREEN
        58: DARK_GREEN
        56: DARK_TEAL
        18: DARK_BLUE
        32: DARK_BLUE
        62: INDIGO
        63: GREY_80_PERCENT
        53: ORANGE
        19: DARK_YELLOW
        17: GREEN
        21: TEAL
        38: TEAL
        12: BLUE
        39: BLUE
        54: BLUE_GREY
        23: GREY_50_PERCENT
        10: RED
        52: LIGHT_ORANGE
        50: LIME
        57: SEA_GREEN
        49: AQUA
        48: LIGHT_BLUE
        20: VIOLET
        36: VIOLET
        55: GREY_40_PERCENT
        14: PINK
        33: PINK
        51: GOLD
        13: YELLOW
        34: YELLOW
        11: BRIGHT_GREEN
        35: BRIGHT_GREEN
        15: TURQUOISE
        35: TURQUOISE
        16: DARK_RED
        37: DARK_RED
        40: SKY_BLUE
        61: PLUM
        25: PLUM
        22: GREY_25_PERCENT
        45: ROSE
        43: LIGHT_YELLOW
        42: LIGHT_GREEN
        41: LIGHT_TURQUOISE
        27:LIGHT_TURQUOISE
        44: PALE_BLUE
        46: LAVENDER
        9: WHITE
        24: CORNFLOWER_BLUE
        26: LEMON_CHIFFON
        25: MAROON
        28: ORCHID
        29: CORAL
        30: ROYAL_BLUE
        31: LIGHT_CORNFLOWER_BLUE
    */
//----------------------------------------------------你可以按上面的方法来自定义颜色
   /*
    * 自定义颜色,去掉注释,贴加,其他则查看颜色对照表
   HSSFPalette palette = this.getCustomPalette();
   palette.setColorAtIndex(idx,
            i,  //RGB red (0-255)
            j,    //RGB green
            k     //RGB blue
         );         
   */

 

 

转自:  http://hi.baidu.com/horsewhite/blog  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值