熟悉下POI的HSSF

因为某些需要,需要用Java保存一些数据成Excel,听朋友说Apache POI Project 不错(以下下简称POI)

POI提供了一系列接口用于读取和生成微软的OLE 2 Compound Document format,就是Excel,Word..

 

因为现在只关心如何读取和生成Excel,所以就直接跳转到

http://poi.apache.org/hssf/index.html 超看如何使用HSSF

 

 这是关于POI-HSSF的简单介绍

POI-HSSF - Java API To Access Microsoft Excel Format Files

Overview

HSSF is the POI Project's pure Java implementation of the Excel '97(-2002) file format.

HSSF provides a way to read spreadsheets create, modify, read and write XLS spreadsheets It provides:

  • low level structures for those with special needs
  • an eventmodel api for efficient read-only access
  • a full usermodel api for creating, reading and modifying XLS files

An alternate way of generating a spreadsheet is via the Cocoon serializer (yet you'll still be using HSSF indirectly). With Cocoon you can serialize any XML datasource (which might be a ESQL page outputting in SQL for instance) by simply applying the stylesheet and designating the serializer.

If you're merely reading spreadsheet data, then use the eventmodel api in the org.apache.poi.hssf.eventusermodel package.

If you're modifying spreadsheet data then use the usermodel api. You can also generate spreadsheets this way.

 

在HSSF的页面内快速浏览了下,发现一个很不错的页面(正好是我需要的)

Busy Developers' Guide to HSSF Features

Busy Developers' Guide to Features

Want to use HSSF read and write spreadsheets in a hurry? This guide is for you. If you're after more in-depth coverage of the HSSF user-API please consult the HOWTO guide as it contains actual descriptions of how to use this stuff.

这个页面包含了大量的代码样例供没有时间查看API文档的程序员看的~~(非常不错的做法啊,人性化:0)

http://poi.apache.org/hssf/quick-guide.html

 

这是我用网页上代码样例(部分)copy出来的测试代码:)

执行后在工程文件文件夹内就成生成名为workbook.xls的Excel文件了

package blog.csdn.net.snailjava.hssf;

import java.io.*;
import java.util.Date;

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

/**
 * <p>Title: HSSF</p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2007</p>
 *
 * <p>Company: geogro</p>
 *
 * @author snail
 * @version 1.0
 */

public class BusyDevTestHssf {
    public BusyDevTestHssf() {
    }

    public void createNewWork() {
        HSSFWorkbook wb = new HSSFWorkbook();
        try {
            FileOutputStream fileOut = new FileOutputStream("workbook.xls");
            wb.write(fileOut);
            fileOut.close();
        } catch (FileNotFoundException ex) {
        } catch (IOException ex) {
        }
    }

    public void createNewSheet() {
        try {
            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();

        } catch (Exception e) {
        }
    }

    public void createCells() {
        try {
            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();

        } catch (Exception e) {
        }
    }

    public void CreatDateCells() {
        try {
            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();
        } catch (FileNotFoundException ex) {
        } catch (IOException ex) {
        }
    }

    public void createBorders() {
        try {
            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();
        } catch (FileNotFoundException ex) {
        } catch (IOException ex) {
        }

    }

    public void createDifferentTypesOfCells() {
        try {
            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();
        } catch (FileNotFoundException ex) {
        } catch (IOException ex) {
        }
    }

    public void createFillWithColor() {

        try {
            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();
        } catch (FileNotFoundException ex) {
        } catch (IOException ex) {
        }

    }


    public static void main(String[] args) {
        BusyDevTestHssf busydevtesthssf = new BusyDevTestHssf();
        System.out.println("TestHSSF!");
//      测试 busydevtesthssf对象的任何方法
        busydevtesthssf.createFillWithColor();
        System.out.println("--------------");

    }
}

 

老猪:因为Apache上的开源项目基本上都是无偿为人民服务的,所以作者都没有多少时间和精力去做详细教学和文档说明,HSSF的【Busy Developers' Guide to HSSF Features】这种形式的文档既简单,又直观,非常方便,是非常好的想法,以后自己写的代码的文档也可以借鉴下。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值