POI 操作office2007

来源:互联网 发布:手机淘宝海报在线制作 编辑:程序博客网 时间:2024/06/07 13:23

 XSSFWorkbook与HSSFWorkbook的差别

api基本一致 操作office2007部分代码如下: 

Java代码  收藏代码
  1. public class Excel  
  2.   
  3. //读excel文档  
  4. public void readExcel(String sheetName)  
  5. {  
  6.     XSSFSheet sheet = getSheet(sheetName);  
  7.     if(sheet != null)  
  8.     {  
  9.         for(Iterator<Row> i = sheet.rowIterator(); i.hasNext();)  
  10.         {  
  11.             //Row row = i.next();  
  12.             XSSFRow row = (XSSFRow) i.next();  
  13.             if(row == null)  
  14.                 continue;  
  15.             //System.out.println(row.getRowNum() + "="+row.getCell(0));  
  16.             for(Iterator<Cell> j = row.cellIterator(); j.hasNext();)  
  17.             {  
  18.                 //Cell cell = j.next();  
  19.                 XSSFCell cell = (XSSFCell) j.next();  
  20.                 if(cell == null)  
  21.                     continue;  
  22.                 System.out.print(getCellValue(cell) + " ");  
  23.             }  
  24.             System.out.println();  
  25.         }  
  26.     }  
  27.     else  
  28.     {  
  29.         System.out.println("没有找到工作表");  
  30.     }  
  31. }  
  32.   
  33. //加载工作薄  
  34. private XSSFWorkbook getXSSFWorkBook()  
  35. {  
  36.     XSSFWorkbook workbook = null;  
  37.     try  
  38.     {  
  39.         String path = System.getProperty("user.dir") + System.getProperty("file.separator")+"doc"+ System.getProperty("file.separator")+"ExcelDoc.xlsx";  
  40.         workbook = new XSSFWorkbook(new FileInputStream(path));  
  41.     }  
  42.     catch (Exception e)  
  43.     {  
  44.         e.printStackTrace();  
  45.     }  
  46.     return workbook;  
  47. }  
  48.   
  49. //根据名字取工作表Sheet  
  50. private XSSFSheet getSheet(String sheetName)  
  51. {  
  52.     return this.getXSSFWorkBook().getSheet(sheetName);  
  53. }  
  54.   
  55. //判断Cell单元格的类型。  
  56. private String getCellValue(Cell cell)  
  57. {  
  58.     Object result = null;  
  59.     switch(cell.getCellType())  
  60.     {  
  61.         case Cell.CELL_TYPE_STRING:  
  62.             result = cell.getStringCellValue();  
  63.             break;  
  64.         case Cell.CELL_TYPE_NUMERIC:  
  65.             if(DateUtil.isCellInternalDateFormatted(cell))  
  66.                 result = cell.getDateCellValue();  
  67.             else  
  68.                 result = cell.getNumericCellValue();  
  69.             break;  
  70.         case Cell.CELL_TYPE_FORMULA:  
  71.             result = cell.getCellFormula();  
  72.             break;  
  73.         case Cell.CELL_TYPE_ERROR:  
  74.             result = cell.getErrorCellValue();  
  75.             break;  
  76.         case Cell.CELL_TYPE_BOOLEAN:  
  77.             result = cell.getBooleanCellValue();  
  78.             break;  
  79.         default:  
  80.             result = "NULL";  
  81.             break;  
  82.     }  
  83.     return result.toString();  
  84. }     
  85. //生成excel文件  
  86. public void createExcelDoc()  
  87. {  
  88.     String path = System.getProperty("user.dir") + System.getProperty("file.separator")+"doc"+ System.getProperty("file.separator")+"creatExcelDoc.xlsx";  
  89.     try  
  90.     {  
  91.         FileOutputStream outStream = new FileOutputStream(path);  
  92.         XSSFWorkbook workbook = new XSSFWorkbook();  
  93.           
  94.         XSSFSheet sheet = workbook.createSheet("first");  
  95.         sheet.autoSizeColumn(0);  
  96.         XSSFRow row = sheet.createRow(0);  
  97.         row.createCell(0).setCellValue("Name");  
  98.         row.createCell(1).setCellValue(2.2);  
  99.           
  100.         //helper.createDataFormat() 得到一个DataFormat实例  
  101.         /*CreationHelper helper = workbook.getCreationHelper(); 
  102.         CellStyle dateStyle = workbook.createCellStyle(); 
  103.         dateStyle.setDataFormat(helper.createDataFormat().getFormat("yyyy-MM-dd hh:mm:ss")); 
  104.          
  105.         Cell c = row.createCell(2); 
  106.         c.setCellValue(new Date()); 
  107.         c.setCellStyle(dateStyle);*/  
  108.           
  109.           
  110.         //XSSF....方法  
  111.         XSSFCell cell = row.createCell(2);  
  112.         XSSFCellStyle style = workbook.createCellStyle();  
  113.         XSSFCreationHelper h = workbook.getCreationHelper();  
  114.           
  115.         style.setDataFormat(h.createDataFormat().getFormat("yyyy-MM-dd"));  
  116.         style.setAlignment(XSSFCellStyle.VERTICAL_CENTER);  
  117.           
  118.         cell.setCellStyle(style);  
  119.         cell.setCellValue(new Date());  
  120.           
  121.         //设置第n单元格的宽度,自动  
  122.         sheet.autoSizeColumn(2);  
  123.           
  124.         workbook.write(outStream);  
  125.         System.out.println("create finished!!");  
  126.     }  
  127.     catch (Exception e)  
  128.     {  
  129.         e.printStackTrace();  
  130.     }  
  131. }  


word2007代码 
Java代码  收藏代码
  1. public void readWord()  
  2.   
  3. try  
  4. {  
  5.     String path = System.getProperty("user.dir") + System.getProperty("file.separator")+"doc"+ System.getProperty("file.separator")+"WordDoc.docx";  
  6.       
  7.     XWPFDocument document = new XWPFDocument(new FileInputStream(path));  
  8.       
  9.     XWPFWordExtractor extractor = new XWPFWordExtractor(document);  
  10.       
  11.     System.out.println(document.getFootnotes().size());  
  12.     System.out.println(document.getDocument().getBody());  
  13.     System.out.println(extractor.getText());  
  14.     System.out.println(extractor.getMetadataTextExtractor().getText().toUpperCase());  
  15. }  
  16. catch (Exception e)  
  17. {  
  18.     e.printStackTrace();  
  19. }  
0 0
原创粉丝点击