poi-hssf小结

一、概述
    1. Apache POI是Apache软件基金会的开放源码函式库,POI提供API给java程式对Microsoft Office格式档案读和写的功能.
    2. 结构
        HSSF - 提供读写Microsoft Excel XLS格式档案的功能。
        XSSF - 提供读写Microsoft Excel OOXML XLSX格式档案的功能。
        HWPF - 提供读写Microsoft Word DOC格式档案的功能。
        HSLF - 提供读写Microsoft PowerPoint格式档案的功能。
        HDGF - 提供读Microsoft Visio格式档案的功能。
        HPBF - 提供读Microsoft Publisher格式档案的功能。
        HSMF - 提供读Microsoft Outlook格式档案的功能
    
二、HSSF代码示例
    1.excel结构
        HSSFWorkbook     excel文档对象介绍
        HSSFSheet         excel的表单
        HSSFRow         excel的行
        HSSFCell         excel的格子单元
        HSSFFont         excel字体
        HSSFName         名称
        HSSFDataFormat     日期格式
        poi1.7中增加以下2项:
        HSSFHeader         sheet头
        HSSFFooter         sheet尾
        HSSFCellStyle    cell样式
        辅助操作包括
        HSSFDateUtil         日期
        HSSFPrintSetup         打印
        HSSFErrorConstants    错误信息表

    2.代码示例:

  1. //导包:commons-logging..jar、log4j...jar、poi-..jar   
  2. import java.io.FileOutputStream;  
  3. import java.util.Calendar;  
  4. import java.util.Date;  
  5. import org.apache.poi.hssf.usermodel.HSSFCell;  
  6. import org.apache.poi.hssf.usermodel.HSSFCellStyle;  
  7. import org.apache.poi.hssf.usermodel.HSSFDataFormat;  
  8. import org.apache.poi.hssf.usermodel.HSSFFont;  
  9. import org.apache.poi.hssf.usermodel.HSSFRichTextString;  
  10. import org.apache.poi.hssf.usermodel.HSSFRow;  
  11. import org.apache.poi.hssf.usermodel.HSSFSheet;  
  12. import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
  13. import org.apache.poi.hssf.util.HSSFColor;  
  14. public class test {  
  15.     public static void main(String[] args) throws Exception {  
  16.         //创建,存在内存中   
  17.         HSSFWorkbook wb = new HSSFWorkbook();  
  18.         HSSFSheet sheet = wb.createSheet("first sheet中文");  
  19.         wb.createSheet("second sheet");  
  20.           
  21.         //创建行,并存放各种类型数据   
  22.         HSSFRow row = sheet.createRow(0);  
  23.         HSSFCell cell = row.createCell(0);  
  24.         cell.setCellValue(false);  
  25.         row.createCell(1).setCellValue(Calendar.getInstance());  
  26.         row.createCell(2).setCellValue(new Date());  
  27.         row.createCell(3).setCellValue(1234567.987654f);  
  28.         String desc = "asdfsadfdsafdfasfasdf";  
  29.         row.createCell(4).setCellValue(new HSSFRichTextString(desc));  
  30.           
  31.         //格式化数据   
  32.         HSSFDataFormat format = wb.createDataFormat();//创建格式对象   
  33.         HSSFCellStyle style = wb.createCellStyle();//创建样式对象   
  34.           
  35.         //设置格式   
  36.         style.setDataFormat(format.getFormat("yyyy-MM-dd hh:mm:ss"));  
  37.         cell = row.getCell(1);  
  38.         cell.setCellStyle(style);//对cell应用样式   
  39.         row.getCell(2).setCellStyle(style);  
  40.           
  41.         //设置列宽   
  42.         sheet.setColumnWidth(15000);//单位:点的1/20   
  43.         sheet.autoSizeColumn(2);  
  44.           
  45.         //格式化数字   
  46.         style = wb.createCellStyle();  
  47.         style.setDataFormat(format.getFormat("#,###.###"));//保留3位小数   
  48.         row.getCell(3).setCellStyle(style);  
  49.           
  50.         //文本自动换行   
  51.         sheet.setColumnWidth(43000);  
  52.         style = wb.createCellStyle();  
  53.         style.setWrapText(true);//回绕文本(wrap:缠绕)   
  54.         row.getCell(4).setCellStyle(style);  
  55.           
  56.         //设置文本对齐方式   
  57.         sheet.setColumnWidth(05000);  
  58.         row = sheet.createRow(1);  
  59.         row.createCell(0).setCellValue("left top");  
  60.         row.createCell(1).setCellValue("center center");  
  61.         row.createCell(2).setCellValue("right bottom");  
  62.           
  63.         //对齐方式-->左上   
  64.         style = wb.createCellStyle();  
  65.         style.setAlignment(HSSFCellStyle.ALIGN_LEFT);//(alignment:队列 ;align:排列)   
  66.         style.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);  
  67.         row.getCell(0).setCellStyle(style);  
  68.         //对齐方式-->中中   
  69.         style = wb.createCellStyle();  
  70.         style.setAlignment(HSSFCellStyle.ALIGN_CENTER);  
  71.         style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);  
  72.         row.getCell(1).setCellStyle(style);  
  73.         //对齐方式-->右下   
  74.         style = wb.createCellStyle();  
  75.         style.setAlignment(HSSFCellStyle.ALIGN_RIGHT);  
  76.         style.setVerticalAlignment(HSSFCellStyle.VERTICAL_BOTTOM);  
  77.         row.getCell(2).setCellStyle(style);  
  78.           
  79.         //设置行高   
  80.         row.setHeightInPoints(50);  
  81.         //设置字体   
  82.         style = row.getCell(1).getCellStyle();  
  83.         HSSFFont font = wb.createFont();  
  84.         font.setFontName("宋体");  
  85.         font.setFontHeightInPoints((short13);  
  86.         font.setColor(HSSFColor.RED.index);  
  87.         style.setFont(font);  
  88.           
  89.         //文本旋转(正数为逆时针,负数为顺时针)   
  90.         style.setRotation((short) -30);  
  91.           
  92.         //设置边框   
  93.         row = sheet.createRow(2);  
  94.         cell = row.createCell(0);  
  95.         style = wb.createCellStyle();  
  96.         style.setBorderBottom(HSSFCellStyle.BORDER_DASH_DOT_DOT);//(dash:破折号)   
  97.         style.setBottomBorderColor(HSSFColor.BLUE.index);  
  98.         cell.setCellStyle(style);  
  99.           
  100.         //计算列(运用函数)   
  101.         row = sheet.createRow(3);  
  102.         row.createCell(0).setCellValue(2);  
  103.         row.createCell(1).setCellValue(5.1);  
  104.         row.createCell(2).setCellValue(3.9);  
  105.         row.createCell(3).setCellFormula("sum(A4:C4)");  
  106.           
  107.         //整体移动行(1:开始行;3:结束行;2:向下移动多少行,负数则为上移)   
  108.         sheet.shiftRows(132);  
  109.           
  110.         /*拆分窗格 
  111.          * 1000:左侧窗格的宽度 
  112.          * 2000:上侧窗格的高度 
  113.          * 3:右侧窗格开始显示的列的索引 
  114.          * 4:下侧窗格开始显示的行的索引 
  115.          * 1:激活哪个面板区 
  116.          */  
  117.         sheet.createSplitPane(10002000341);  
  118.           
  119.         //冻结窗口   
  120.         //1:冻结前几列; 2:冻结前几行   
  121.         sheet.createFreezePane(1234);  
  122.           
  123.         //从内存写出到e:/testPOI.xls,不存在就创建   
  124.         wb.write(new FileOutputStream("e:/testPOI.xls"));  
  125.     }  
  126. }  

转自:http://blog.csdn.net/wyply115/article/details/8451711

猜你喜欢

转载自xueqi.iteye.com/blog/1839565