免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3636 | 回复: 0
打印 上一主题 下一主题

Apache poi生成excel文件 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-12-23 22:27 |只看该作者 |倒序浏览
poi-bin-2.5-final-20040302 How To
使用HSSF API创建excel文档
几个实体
HSSFWorkBook  整个excel文件
HSSFSheet      
HSSFRow
HSSFCell

创建excel的类在org.apache.poi.hssf.usermodel包中。
HSSFWorkBook
WorkBook是通过new HSSFWorkBook实例来创建。
HSSFSheet
Sheet通过HSSFWorkBook实例的createSheet()函数来创建。
新创建的多个sheet自动按照顺序添加到WorkBook。
Sheet创建的时候并没有名字(底部tab显示的名称),需要调用HSSFWorkbook的setSheetName函数来手工设置。如
HSSFWorkbook.setSheetName(sheetindex,"SheetName",encoding).
参数sheetindex
      从0开始
参数encoding可以取两个值
HSSFWorkbook.ENCODING_COMPRESSED_UNICODE
HSSFWorkbook.ENCODING_UTF_16
encoding可以不指定,默认是ENCODING_COMPRESSED_UNICODE(8bit)
HSSFRow
Row是通过HSSFSheet实例的createRow(rowNumber)函数创建的。
参数rowNumber从0开始。
Only rows that hava cell values should be added to the sheet.
可以调用setRowHeight(height)函数设置Row的高度;
其中height单位为twip,即1/20个point。
高度也可以通过setRowHeightInPoints函数来设置。
HSSFCell
Cell通过HSSFRow实例的createCell(column, type)函数来创建。
Only cells that have values should be added to the row.
Cell的type
HSSFCell.CELL_TYPE_NUMERIC  numeric
HSSFCell.CELL_TYPE_STRING    texual
Cell的值
      调用setCellValue(para)函数来设置。Para参数是String或者double。
单个Cell没有width值,必须HSSFSheet实例的setColumnWidth(colindex, width)函数来设置,单位是1/256个character。
HSSFCellStyle  HSSFFont
Cells are styled with HSSFCellStyle objects which in turn contain a reference to an HSSFFont object. These are created via the HSSFWorkbook object by calling createCellStyle() and createFont(). Once you create the object you must set its parameters (colors, borders, etc). To set a font for an HSSFCellStyle call setFont(fontobj).

把poi-2.5-final-20040302.jar一个包添加到CLASSPATH就可以

import java.io.FileOutputStream;
import java.io.IOException;
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.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class ExcelMaker
{
    public static void main (String[] args) throws IOException
    {
        short rownum;
        // create a new file
        FileOutputStream out = new FileOutputStream("f:\\workbook.xls");
        
        // create a new workbook
        HSSFWorkbook wb = new HSSFWorkbook();
        
        // create a new sheet
        HSSFSheet s = wb.createSheet();
        
        // declare a row object reference
        HSSFRow r = null;
        
        // declare a cell object reference
        HSSFCell c = null;
        
        // create 3 cell styles
        HSSFCellStyle cs = wb.createCellStyle();
        HSSFCellStyle cs2 = wb.createCellStyle();
        HSSFCellStyle cs3 = wb.createCellStyle();
        HSSFDataFormat df = wb.createDataFormat();
        
        // create 2 fonts objects
        HSSFFont f = wb.createFont();
        HSSFFont f2 = wb.createFont();
        //set font 1 to 12 point type
        f.setFontHeightInPoints((short) 12);
        //make it blue
        f.setColor( (short)0xc );
        // make it bold
        //arial is the default font
        f.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        //set font 2 to 10 point type
        f2.setFontHeightInPoints((short) 10);
        //make it red
        f2.setColor( (short)HSSFFont.COLOR_RED );
        //make it bold
        f2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        f2.setStrikeout( true );
        //set cell stlye
        cs.setFont(f);
        //set the cell format
        cs.setDataFormat(df.getFormat("#,##0.0"));
        //set a thin border
        cs2.setBorderBottom(cs2.BORDER_THIN);
        //fill w fg fill color
        cs2.setFillPattern((short) HSSFCellStyle.SOLID_FOREGROUND);
        //set the cell format to text see HSSFDataFormat for a full list
        cs2.setDataFormat(HSSFDataFormat.getBuiltinFormat("text"));
        // set the font
        cs2.setFont(f2);
        // set the sheet name in Unicode
        wb.setSheetName(0, "中文",
         HSSFWorkbook.ENCODING_UTF_16 );
        // in case of compressed Unicode
        // wb.setSheetName(0, "HSSF Test", HSSFWorkbook.ENCODING_COMPRESSED_UNICODE );
        // create a sheet with 30 rows (0-29)
        for (rownum = (short) 0; rownum  30; rownum++)
        {
         // create a row
         r = s.createRow(rownum);
         // on every other row
         if ((rownum % 2) == 0)
         {
         // make the row height bigger (in twips - 1/20 of a point)
         r.setHeight((short) 0x249);
         }
         //r.setRowNum(( short ) rownum);
         // create 10 cells (0-9) (the += 2 becomes apparent later
         for (short cellnum = (short) 0; cellnum  10; cellnum += 2)
         {
         // create a numeric cell
         c = r.createCell(cellnum);
         // do some goofy math to demonstrate decimals
         c.setCellValue(rownum * 10000 + cellnum
         + (((double) rownum / 1000)
         + ((double) cellnum / 10000)));
         String cellValue;
         // create a string cell (see why += 2 in the
         c = r.createCell((short) (cellnum + 1));
         
         // on every other row
         if ((rownum % 2) == 0)
         {
         // set this cell to the first cell style we defined
         c.setCellStyle(cs);
         // set the cell's string value to "Test"
         c.setEncoding( HSSFCell.ENCODING_COMPRESSED_UNICODE );
         c.setCellValue( "Test" );
         }
         else
         {
         c.setCellStyle(cs2);
         // set the cell's string value to "\u0422\u0435\u0441\u0442"
         c.setEncoding( HSSFCell.ENCODING_UTF_16 );
         c.setCellValue( "\u0422\u0435\u0441\u0442" );
         }
         // make this column a bit wider
         s.setColumnWidth((short) (cellnum + 1), (short) ((50 * 8) / ((double) 1 / 20)));
         }
        }
        //draw a thick black border on the row at the bottom using BLANKS
        // advance 2 rows
        rownum++;
        rownum++;
        r = s.createRow(rownum);
        // define the third style to be the default
        // except with a thick black border at the bottom
        cs3.setBorderBottom(cs3.BORDER_THICK);
        //create 50 cells
        for (short cellnum = (short) 0; cellnum  50; cellnum++)
        {
         //create a blank type cell (no value)
         c = r.createCell(cellnum);
         // set it to the thick black border style
         c.setCellStyle(cs3);
        }
        //end draw thick black border
        // demonstrate adding/naming and deleting a sheet
        // create a sheet, set its title then delete it
        s = wb.createSheet();
        wb.setSheetName(1, "DeletedSheet");
//        wb.removeSheetAt(1);
        //end deleted sheet
        // write the workbook to the output stream
        // close our file (don't blow out our file handles
        wb.write(out);
        out.close();
    }
}


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/20146/showart_2129237.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP