• 关于POI的系统整理


    java操作excel文件,有两种工具可以使用,一种是jxl,一种是poi。最近做的需求,使用到poi,因此本文介绍poi的使用(目前只介绍写excel文件)。

    一、简介

          Jakarta POI 是apache的子项目,目标是处理ole2对象。它提供了一组操纵Windows文档的Java API

    目前比较成熟的是HSSF接口,处理MS Excel对象。它不象我们仅仅是用csv生成的没有格式的可以由Excel转换的东西,而是真正的Excel对象,你可以控制一些属性如sheet,cell等等。 poi中,通过HSSF,你可以用纯Java代码来读取、写入、修改Excel文件。

    HSSF 为读取操作提供了两类API:usermodel和eventusermodel,即“用户模型”和“事件-用户模型”。前者很好理解,后者比较抽象,但操作效率要高得多。

    二、使用HSSF API创建excel文档

    1、HSSF的几个实体类

    HSSFWorkBook  : 整个excel文件

    HSSFSheet :工作表

    HSSFRow : 行

    HSSFCell : 单元格

    HSSFCellStyle : 单元格样式

    HSSFFont :单元格字体

    HSSFDataFormat : 单元格内容格式化

    HSSFRichTextString : 单元格文本内容

    还有其他一些实体,这里不一一列举

    2、HSSFWorkBook

    WorkBook是通过new HSSFWorkBook实例来创建。

    HSSFWorkbook workBook = new HSSFWorkbook();  

    3、HSSFSheet Sheet通过HSSFWorkBook实例的createSheet()函数来创建。 HSSFSheet sheet = workBook.createSheet(); 新创建的多个sheet自动按照顺序添加到WorkBook。 Sheet创建的时候并没有指定名字(底部tab显示的名称),可以调用HSSFWorkbook的setSheetName函数来手工设置。如 workBook.setSheetName(sheetindex,"SheetName"); 参数sheetindex       从0开始

    4、HSSFRow Row是通过HSSFSheet实例的createRow(rowNumber)函数创建的。 参数rowNumber从0开始。 HSSFRow row = sheet.createRow(0); 可以调用setHeight(height)函数设置Row的高度; 其中height单位为twip,即1/20个point。 高度也可以通过setHeightInPoints函数来设置。

    5、HSSFCell Cell通过HSSFRow实例的createCell(column)或createCell(column, type)函数来创建。 Cell的type HSSFCell.CELL_TYPE_NUMERIC

    HSSFCell.CELL_TYPE_STRING

    HSSFCell.CELL_TYPE_FORMULA

    HSSFCell.CELL_TYPE_BLANK   默认值

    HSSFCell.CELL_TYPE_BOOLEAN

    Cell的值       调用setCellValue(para)函数来设置。Para参数是HSSFRichTextString、double、Date、Calander。       单个Cell没有width值,必须HSSFSheet实例的setColumnWidth(colindex, width)函数来设置,单位是1/256个character。例如,如果第一列单元格宽度设置为8个字符宽度,则使用:sheet.setColumnWidth(0, 8*256);

          另外,HSSFSheet提供了autoSizeColumn(short column)方法,来根据单元格内容,自动调整列的宽度。该方法效率很低,不推荐使用(特别是数据量很大的时候)。

    6、HSSFFont

    HSSFFont是通过HSSFWorkbook实例的createFont()方法创建的,即:

    HSSFFont font = workBook.createFont();

    font.setFontHeightInPoints((short) 11); // 字体大小 font.setFontName("宋体");

    7、HSSFDataFormat

    HSSFDataFormat,用来设置单元格的格式,例如数字、日期格式。通过HSSFWorkbook实例的createDataFormat()方法来创建对象,即:

    HSSFDataFormat dataFormat = workBook.createDataFormat();

    创建之后,使用style进行设置,即:

    style.setDataFormat(dataFormat.getFormat("yyyy-mm-dd hh:mm:ss"));

    8、HSSFCellStyle

    HSSFCellStyle实例是通过HSSFWorkbook实例的createCellStyle()方法来创建的,即:

    HSSFCellStyle style = workBook.createCellStyle();
    
    style.setFont(font);
     style.setAlignment(HSSFCellStyle.ALIGN_LEFT);
     style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); 
     style.setWrapText(false); // 是否换行
    
    style.setDataFormat(dataFormat.getFormat("yyyy-mm-dd hh:mm:ss"));
    
    import java.io.BufferedOutputStream;
     import java.io.FileOutputStream;
     import java.io.IOException;
     import java.util.Date;
    
    import org.apache.poi.hssf.usermodel.HSSFCell;
     import org.apache.poi.hssf.usermodel.HSSFCellStyle;
     import org.apache.poi.hssf.usermodel.HSSFDataFormat;
     import org.apache.poi.hssf.usermodel.HSSFFont;
     import org.apache.poi.hssf.usermodel.HSSFRichTextString;
     import org.apache.poi.hssf.usermodel.HSSFRow;
     import org.apache.poi.hssf.usermodel.HSSFSheet;
     import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    
    public class WriteExcel {
         private static final String DATE_FORMAT = "yyyy-mm-dd hh:mm:ss";
         private static final String MONEY_FORMAT = "###,##0.00";
         
         public static void main(String[] args) throws IOException {
             export();
         }
         
         public static HSSFCellStyle createCellStyle(HSSFWorkbook workBook, HSSFFont font, short align) {
             HSSFCellStyle style = workBook.createCellStyle();
             style.setFont(font);
             style.setAlignment(align);
             style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
             return style;
         }
         
         public static HSSFCellStyle createWrapCellStyle(HSSFWorkbook workBook, HSSFFont font, short align, boolean wrapped) {
             HSSFCellStyle style = workBook.createCellStyle();
             style.setFont(font);
             style.setAlignment(align);
             style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
             style.setWrapText(wrapped);
             return style;
         }
         
         public static HSSFCellStyle createFormatCellStyle(HSSFWorkbook workBook, HSSFFont font, short align, HSSFDataFormat dataFormat, String format) {
             HSSFCellStyle style = workBook.createCellStyle();
             style.setFont(font);
             style.setAlignment(align);
             style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
             style.setDataFormat(dataFormat.getFormat(format));
             return style;
         }
         
         public static void createStringCell(HSSFRow row, short column, HSSFCellStyle cellStyle, String value) {
             HSSFCell cell = row.createCell(column);
             cell.setCellStyle(cellStyle);
             cell.setCellValue(new HSSFRichTextString(value));
         }
         
         public static void createNumberCell(HSSFRow row, short column, HSSFCellStyle cellStyle, double value) {
             HSSFCell cell = row.createCell(column);
             cell.setCellStyle(cellStyle);
             cell.setCellValue(value);
         }
         
         public static void createDateCell(HSSFRow row, short column, HSSFCellStyle cellStyle, Date value) {
             HSSFCell cell = row.createCell(column);
             cell.setCellStyle(cellStyle);
             cell.setCellValue(value);
         }
         
         public static void export() throws IOException {
             HSSFWorkbook workBook = new HSSFWorkbook();
             HSSFSheet sheet = workBook.createSheet();
             workBook.setSheetName(0, "sheet1");
             
             // 字体
            HSSFFont titleFont = workBook.createFont();
             titleFont.setFontHeightInPoints((short) 11);
             titleFont.setFontName("宋体");
             HSSFDataFormat dataFormat = workBook.createDataFormat();
             
             // 单元格样式
            HSSFCellStyle leftStyle = createCellStyle(workBook, titleFont, HSSFCellStyle.ALIGN_LEFT);
             HSSFCellStyle rightStyle = createCellStyle(workBook, titleFont, HSSFCellStyle.ALIGN_RIGHT);
             HSSFCellStyle wrapStyle = createWrapCellStyle(workBook, titleFont, HSSFCellStyle.ALIGN_LEFT, true);
             HSSFCellStyle dateStyle = createFormatCellStyle(workBook, titleFont, HSSFCellStyle.ALIGN_RIGHT, dataFormat, DATE_FORMAT);
             HSSFCellStyle moneyStyle = createFormatCellStyle(workBook, titleFont, HSSFCellStyle.ALIGN_RIGHT, dataFormat, MONEY_FORMAT);
             
             int rowIndex = 0;
    
            // excel标题行
            HSSFRow row = sheet.createRow(rowIndex);
             short index = (short) 0;
             for (int i = 0; i < 6; i ++) {
                 createStringCell(row, index, leftStyle, "列标题" + i);
                 sheet.setColumnWidth(index, (short) (20 * 256)); // 20个字符的宽度
                index++;
             }
    
            // 内容
            for (int i = 0; i < 10; i++) {
                 row = sheet.createRow(++rowIndex);
                 createStringCell(row, (short) 0, leftStyle, "26688135");
                 createStringCell(row, (short) 1, wrapStyle, "这是简介");
                 createNumberCell(row, (short) 2, moneyStyle, 100.05);
                 createDateCell(row, (short) 3, dateStyle, new Date());
                 createStringCell(row, (short) 4, wrapStyle, "第一行内容 /r/n第二行内容/r/n第三行内容"); // 使用/r/n,强制换行
                createNumberCell(row, (short) 5, rightStyle, 100);
             }
             
             // 输出
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("abc.xls"));
             workBook.write(bos);
             bos.close();
         }
     }

    转载自:http://blog.csdn.net/zhutulang/article/details/6885637


    常见的单元格设置还有:

    1.合并单元格;

    2.单元格水平居中、垂直居中;

    3.定义日期格式:

      1. poi的“Quick Guide”中提供了 “How to create date 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);

    该方法只能创建HSSFDataFormat方法中内建的几种日期格式,而不是真正的完全自定义格式,如果只需要内建的格式,请参考源码。

      2. 真正的日期自定义格式,应该参考“Quick Guide”中提供了 “Create user defined data formats  ”例子,代码如下:

      

        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("format sheet");
        HSSFDataFormat format = wb.createDataFormat();
        HSSFRow row = sheet.createRow(0);
        HSSFCell cell = row.createCell(0);
        HSSFCellStyle style = wb.createCellStyle();
        style.setDataFormat(format.getFormat"yyyy年MM月dd日"));
        cell.setCellValue(new Date());
        cell.setCellStyle(style);

    参考文档:

    官网主页:http://poi.apache.org/

    很好的教程:http://poi.apache.org/spreadsheet/quick-guide.html

  • 相关阅读:
    2015-04
    2014-12
    2014-9
    nginx中ngx_http_ssl_module模块
    nginx中ngx_http_gzip_module模块
    Nginx中ngx_http_log_module模块
    Nginx中ngx_http_auth_basic_moudel和ngx_http_stub_status_module模块
    nginx中ngx_http_access_module模块
    nginx中ngx_http_core_module模块
    Nginx安装
  • 原文地址:https://www.cnblogs.com/chickenbeer/p/4738752.html
Copyright © 2020-2023  润新知