poi-hssf总结
poi-hssf小结一、概述?? ?1. Apache POI是Apache软件基金会的开放源码函式库,POI提供API给java程式对Micros
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.代码示例:
?
[java] view plaincopyprint?
- //导包:commons-logging..jar、log4j...jar、poi-..jar ??import?java.io.FileOutputStream;??
- import?java.util.Calendar;??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;??
- import?org.apache.poi.hssf.util.HSSFColor;??public?class?test?{??
- ????public?static?void?main(String[]?args)?throws?Exception?{??????????//创建,存在内存中 ??
- ????????HSSFWorkbook?wb?=?new?HSSFWorkbook();??????????HSSFSheet?sheet?=?wb.createSheet("first?sheet中文");??
- ????????wb.createSheet("second?sheet");????????????
- ????????//创建行,并存放各种类型数据 ??????????HSSFRow?row?=?sheet.createRow(0);??
- ????????HSSFCell?cell?=?row.createCell(0);??????????cell.setCellValue(false);??
- ????????row.createCell(1).setCellValue(Calendar.getInstance());??????????row.createCell(2).setCellValue(new?Date());??
- ????????row.createCell(3).setCellValue(1234567.987654f);??????????String?desc?=?"asdfsadfdsafdfasfasdf";??
- ????????row.createCell(4).setCellValue(new?HSSFRichTextString(desc));????????????
- ????????//格式化数据 ??????????HSSFDataFormat?format?=?wb.createDataFormat();//创建格式对象 ??
- ????????HSSFCellStyle?style?=?wb.createCellStyle();//创建样式对象 ????????????
- ????????//设置格式 ??????????style.setDataFormat(format.getFormat("yyyy-MM-dd?hh:mm:ss"));??
- ????????cell?=?row.getCell(1);??????????cell.setCellStyle(style);//对cell应用样式 ??
- ????????row.getCell(2).setCellStyle(style);????????????
- ????????//设置列宽 ??????????sheet.setColumnWidth(1,?5000);//单位:点的1/20 ??
- ????????sheet.autoSizeColumn(2);????????????
- ????????//格式化数字 ??????????style?=?wb.createCellStyle();??
- ????????style.setDataFormat(format.getFormat("#,###.###"));//保留3位小数 ??????????row.getCell(3).setCellStyle(style);??
- ??????????????????//文本自动换行 ??
- ????????sheet.setColumnWidth(4,?3000);??????????style?=?wb.createCellStyle();??
- ????????style.setWrapText(true);//回绕文本(wrap:缠绕) ??????????row.getCell(4).setCellStyle(style);??
- ??????????????????//设置文本对齐方式 ??
- ????????sheet.setColumnWidth(0,?5000);??????????row?=?sheet.createRow(1);??
- ????????row.createCell(0).setCellValue("left?top");??????????row.createCell(1).setCellValue("center?center");??
- ????????row.createCell(2).setCellValue("right?bottom");????????????
- ????????//对齐方式-->左上 ??????????style?=?wb.createCellStyle();??
- ????????style.setAlignment(HSSFCellStyle.ALIGN_LEFT);//(alignment:队列?;align:排列) ??????????style.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);??
- ????????row.getCell(0).setCellStyle(style);??????????//对齐方式-->中中 ??
- ????????style?=?wb.createCellStyle();??????????style.setAlignment(HSSFCellStyle.ALIGN_CENTER);??
- ????????style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);??????????row.getCell(1).setCellStyle(style);??
- ????????//对齐方式-->右下 ??????????style?=?wb.createCellStyle();??
- ????????style.setAlignment(HSSFCellStyle.ALIGN_RIGHT);??????????style.setVerticalAlignment(HSSFCellStyle.VERTICAL_BOTTOM);??
- ????????row.getCell(2).setCellStyle(style);????????????
- ????????//设置行高 ??????????row.setHeightInPoints(50);??
- ????????//设置字体 ??????????style?=?row.getCell(1).getCellStyle();??
- ????????HSSFFont?font?=?wb.createFont();??????????font.setFontName("宋体");??
- ????????font.setFontHeightInPoints((short)?13);??????????font.setColor(HSSFColor.RED.index);??
- ????????style.setFont(font);????????????
- ????????//文本旋转(正数为逆时针,负数为顺时针) ??????????style.setRotation((short)?-30);??
- ??????????????????//设置边框 ??
- ????????row?=?sheet.createRow(2);??????????cell?=?row.createCell(0);??
- ????????style?=?wb.createCellStyle();??????????style.setBorderBottom(HSSFCellStyle.BORDER_DASH_DOT_DOT);//(dash:破折号) ??
- ????????style.setBottomBorderColor(HSSFColor.BLUE.index);??????????cell.setCellStyle(style);??
- ??????????????????//计算列(运用函数) ??
- ????????row?=?sheet.createRow(3);??????????row.createCell(0).setCellValue(2);??
- ????????row.createCell(1).setCellValue(5.1);??????????row.createCell(2).setCellValue(3.9);??
- ????????row.createCell(3).setCellFormula("sum(A4:C4)");????????????
- ????????//整体移动行(1:开始行;3:结束行;2:向下移动多少行,负数则为上移) ??????????sheet.shiftRows(1,?3,?2);??
- ??????????????????/*拆分窗格?
- ?????????*?1000:左侧窗格的宽度??????????*?2000:上侧窗格的高度?
- ?????????*?3:右侧窗格开始显示的列的索引??????????*?4:下侧窗格开始显示的行的索引?
- ?????????*?1:激活哪个面板区??????????*/??
- ????????sheet.createSplitPane(1000,?2000,?3,?4,?1);????????????
- ????????//冻结窗口 ??????????//1:冻结前几列;?2:冻结前几行 ??
- ????????sheet.createFreezePane(1,?2,?3,?4);????????????
- ????????//从内存写出到e:/testPOI.xls,不存在就创建 ??????????wb.write(new?FileOutputStream("e:/testPOI.xls"));??
- ????}??}??
转自:http://blog.csdn.net/wyply115/article/details/8451711