原文地址:
<wbr></wbr>
一. Apache POI 简介( http://poi.apache.org/)
使用Java程序读写Microsoft Office,提供了下面这几种类型:
HSSF-提供读写Microsoft Excel XLS格式档案的功能。
XSSF-提供读写Microsoft Excel OOXML XLSX格式档案的功能。
HWPF-提供读写Microsoft Word DOC格式档案的功能。
HSLF- 供读写Microsoft PowerPoint格式档案的功能。
HDGF-提供读Microsoft Visio格式档案的功能。
HPBF-提供读Microsoft Publisher格式档案的功能。
二、POI操作Excel
1. 官方快速帮助:http://poi.apache.org/spreadsheet/quick-guide.html
2. 导入包:poi-3.6.jar
参考:
1. http://www.blogjava.net/vwpolo/archive/2009/09/16/295243.html
2. http://hacker-zxf.iteye.com/blog/746546
3. http://zmx.iteye.com/blog/622536
4. http://canfly2010.iteye.com/blog/701726
使用Java程序读写Microsoft Office,提供了下面这几种类型:
HSSF-提供读写Microsoft Excel XLS格式档案的功能。
XSSF-提供读写Microsoft Excel OOXML XLSX格式档案的功能。
HWPF-提供读写Microsoft Word DOC格式档案的功能。
HSLF- 供读写Microsoft PowerPoint格式档案的功能。
HDGF-提供读Microsoft Visio格式档案的功能。
HPBF-提供读Microsoft Publisher格式档案的功能。
二、POI操作Excel
1. 官方快速帮助:http://poi.apache.org/spreadsheet/quick-guide.html
2. 导入包:poi-3.6.jar
参考:
1. http://www.blogjava.net/vwpolo/archive/2009/09/16/295243.html
2. http://hacker-zxf.iteye.com/blog/746546
3. http://zmx.iteye.com/blog/622536
4. http://canfly2010.iteye.com/blog/701726
5. http://blog.youkuaiyun.com/aerchi
- packageexcel.poi;
- importjava.io.File;
- importjava.io.FileInputStream;
- importjava.io.FileOutputStream;
- importjava.io.IOException;
- importjava.io.InputStream;
- importjava.util.Date;
- importjava.util.Iterator;
- importorg.apache.poi.POITextExtractor;
- importorg.apache.poi.extractor.ExtractorFactory;
- importorg.apache.poi.hssf.usermodel.HSSFCell;
- importorg.apache.poi.hssf.usermodel.HSSFCellStyle;
- importorg.apache.poi.hssf.usermodel.HSSFDataFormat;
- importorg.apache.poi.hssf.usermodel.HSSFRow;
- importorg.apache.poi.hssf.usermodel.HSSFSheet;
- importorg.apache.poi.hssf.usermodel.HSSFWorkbook;
- importorg.apache.poi.openxml4j.exceptions.InvalidFormatException;
- importorg.apache.poi.openxml4j.exceptions.OpenXML4JException;
- importorg.apache.poi.poifs.filesystem.POIFSFileSystem;
- importorg.apache.poi.ss.usermodel.IndexedColors;
- importorg.apache.poi.xssf.usermodel.XSSFCell;
- importorg.apache.poi.xssf.usermodel.XSSFCellStyle;
- importorg.apache.poi.xssf.usermodel.XSSFCreationHelper;
- importorg.apache.poi.xssf.usermodel.XSSFRow;
- importorg.apache.poi.xssf.usermodel.XSSFSheet;
- importorg.apache.poi.xssf.usermodel.XSSFWorkbook;
- importorg.apache.xmlbeans.XmlException;
- publicclassReadExcel{
- /**
- *读取office2003xls
- *@paramfilePath
- */
- @SuppressWarnings({"unchecked","deprecation"})
- publicvoidloadXls(StringfilePath){
- try{
- InputStreaminput=newFileInputStream("D:\\aerchi\\test\\xls\\流量.xls");
- POIFSFileSystemfs=newPOIFSFileSystem(input);
- HSSFWorkbookwb=newHSSFWorkbook(fs);
- HSSFSheetsheet=wb.getSheetAt(0);
- //Iterateovereachrowinthesheet
- Iteratorrows=sheet.rowIterator();
- while(rows.hasNext()){
- HSSFRowrow=(HSSFRow)rows.next();
- System.out.println("Row#"+row.getRowNum());
- //Iterateovereachcellintherowandprintoutthecell"s
- //content
- Iteratorcells=row.cellIterator();
- while(cells.hasNext()){
- HSSFCellcell=(HSSFCell)cells.next();
- System.out.println("Cell#"+cell.getCellNum());
- switch(cell.getCellType()){
- caseHSSFCell.CELL_TYPE_NUMERIC:
- System.out.println(cell.getNumericCellValue());
- break;
- caseHSSFCell.CELL_TYPE_STRING:
- System.out.println(cell.getStringCellValue());
- break;
- caseHSSFCell.CELL_TYPE_BOOLEAN:
- System.out.println(cell.getBooleanCellValue());
- break;
- caseHSSFCell.CELL_TYPE_FORMULA:
- System.out.println(cell.getCellFormula());
- break;
- default:
- System.out.println("unsuportedselltype");
- break;
- }
- }
- }
- }catch(IOExceptionex){
- ex.printStackTrace();
- }
- }
- /**
- *读取xlsx文本
- *@paramfilePath
- */
- publicvoidloadXlsxText(StringfilePath){
- FileinputFile=newFile("D:\\aerchi.xlsx");
- try{
- POITextExtractorextractor=ExtractorFactory.createExtractor(inputFile);
- System.out.println(extractor.getText());
- }catch(InvalidFormatExceptione){
- e.printStackTrace();
- }catch(IOExceptione){
- e.printStackTrace();
- }catch(OpenXML4JExceptione){
- e.printStackTrace();
- }catch(XmlExceptione){
- e.printStackTrace();
- }
- }
- /**
- *读取office2007xlsx
- *@paramfilePath
- */
- publicvoidloadXlsx(StringfilePath){
- //构造XSSFWorkbook对象,strPath传入文件路径
- XSSFWorkbookxwb=null;
- try{
- xwb=newXSSFWorkbook("D:\\aerchi.xlsx");
- }catch(IOExceptione){
- System.out.println("读取文件出错");
- e.printStackTrace();
- }
- //读取第一章表格内容
- XSSFSheetsheet=xwb.getSheetAt(0);
- //定义row、cell
- XSSFRowrow;
- Stringcell;
- //循环输出表格中的内容
- for(inti=sheet.getFirstRowNum()+1;i<sheet.getPhysicalNumberOfRows();i++){
- row=sheet.getRow(i);
- for(intj=row.getFirstCellNum();j<row.getPhysicalNumberOfCells();j++){
- //通过row.getCell(j).toString()获取单元格内容,
- if(j==1&&i!=0){
- cell=row.getCell(j).getDateCellValue().toLocaleString();
- }else{
- cell=row.getCell(j).toString();
- }
- /*//获取字体和背景颜色
- StringrgbShort=row.getCell(j).getCellStyle().getFont().getCTFont().getColorArray()[0].xmlText();
- rgbShort=ReadExcel.substringBetween(rgbShort,"rgb=\"","\"/>");
- StringrgbShort=row.getCell(j).getCellStyle().getFillBackgroundXSSFColor().getCTColor().toString();
- Colorcolor=newColor(Color.BLUE.getRGB());
- System.out.print(cell+",index:"+rgbShort+"red:"+color.getRed()+"blue:"+color.getBlue()+"\t");*/
- System.out.print(cell+"\t");
- }
- System.out.println("");
- }
- }
- /**
- *HSSF写入excelxls格式
- *@paramfilePath
- *@throwsIOException
- */
- publicvoidwriteXls(StringfilePath)throwsIOException{
- //工作簿23.
- HSSFWorkbookhssfworkbook=newHSSFWorkbook();
- //创建sheet页25.
- HSSFSheethssfsheet=hssfworkbook.createSheet();
- //sheet名称
- hssfworkbook.setSheetName(0,"研发部门");
- //取得第一行29.
- HSSFRowhssfrow=hssfsheet.createRow(0);
- //创建第一个单元格并处理乱码31.
- HSSFCellhssfcell_0=hssfrow.createCell((short)0);
- //hssfcell_0.setEncoding(HSSFWorkbook.ENCODING_UTF_16);
- //对第一个单元格赋值34.
- hssfcell_0.setCellValue("研发工程师1");
- //日期单元格格式处理
- HSSFCellStylehssfcellstyle=hssfworkbook.createCellStyle();
- //m/d/yyh:mm39.
- hssfcellstyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
- //创建第二个单元格41.
- HSSFCellhssfcell_1=hssfrow.createCell((short)1);
- hssfcell_1.setCellValue(newDate());
- hssfcell_1.setCellStyle(hssfcellstyle);
- hssfrow.createCell((short)2).setCellValue(true);
- hssfrow.createCell((short)3).setCellValue(122.00);
- //输出49.
- FileOutputStreamfileoutputstream=newFileOutputStream("d:\\exceltext.xls");
- hssfworkbook.write(fileoutputstream);
- fileoutputstream.close();
- }
- @SuppressWarnings("static-access")
- publicvoidwriteXlsx(StringfilePath)throwsIOException{
- //工作簿
- XSSFWorkbookhssfworkbook=newXSSFWorkbook();
- //获得CreationHelper对象,这个应该是一个帮助类
- XSSFCreationHelperhelper=hssfworkbook.getCreationHelper();
- //创建sheet页
- XSSFSheethssfsheet=hssfworkbook.createSheet();
- //设置sheet名称
- hssfworkbook.setSheetName(0,"我的测试sheet");
- //取得第一行
- XSSFRowfirstRow=hssfsheet.createRow(0);
- //创建第一个单元格
- XSSFCellhssfcell_0=firstRow.createCell(0);
- //hssfcell_0.setEncoding(HSSFWorkbook.ENCODING_UTF_16);并处理乱码
- //对第一个单元格赋值
- hssfcell_0.setCellValue("名称");
- //创建第二个单元格
- XSSFCellhssfcell_1=firstRow.createCell(1);
- hssfcell_1.setCellValue("创建日期");
- //日期单元格格式处理
- XSSFCellStyledateCellStyle=hssfworkbook.createCellStyle();
- //m/d/yyh:mm设置日期格式
- dateCellStyle.setDataFormat(helper.createDataFormat().getFormat("yyyy-MM-ddhh:mm:ss"));
- dateCellStyle=ReadExcel.setFillBackgroundColors(dateCellStyle,IndexedColors.BLACK.getIndex(),IndexedColors.YELLOW.getIndex(),dateCellStyle.SOLID_FOREGROUND);
- //设置其他标题
- firstRow.createCell(2).setCellValue("用户");
- firstRow.createCell(3).setCellValue("备注");
- //写入所有内容行
- for(introwInt=1;rowInt<10;rowInt++){
- XSSFRowrow=hssfsheet.createRow(rowInt);
- XSSFCellcell_0=row.createCell(0);
- cell_0.setCellValue("碌人乘凉");
- XSSFCellcell_1=row.createCell(1);
- cell_1.setCellValue(newDate());
- cell_1.setCellStyle(dateCellStyle);
- XSSFCellcell_2=row.createCell(2);
- cell_2.setCellValue("超级会员");
- XSSFCellcell_3=row.createCell(3);
- cell_3.setCellValue("这里是备注信息");
- }
- //输出49.
- FileOutputStreamfileoutputstream=newFileOutputStream("d:\\exceltext.xlsx");
- hssfworkbook.write(fileoutputstream);
- fileoutputstream.close();
- }
- /**
- *前景和背景填充的着色
- *@paramcellStyle
- *@parambgIndexedColors.ORANGE.getIndex();
- *@paramfgIndexedColors.ORANGE.getIndex();
- *@paramfpCellStyle.SOLID_FOREGROUND
- *@return
- */
- publicstaticXSSFCellStylesetFillBackgroundColors(XSSFCellStylecellStyle,shortbg,shortfg,shortfp){
- cellStyle.setFillBackgroundColor(bg);
- cellStyle.setFillForegroundColor(fg);
- cellStyle.setFillPattern(fp);
- returncellStyle;
- }
- publicstaticvoidmain(String[]args){
- ReadExcelreadExcel=newReadExcel();
- /*try{
- readExcel.writeXlsx("");
- }catch(IOExceptione){
- e.printStackTrace();
- }*/
- readExcel.loadXls("");
- }
- }