请问 poi-3.5-beta4 可以读取Excel2007吗?如果可以请把读取.xlsx文件的方法贴出来。非常感谢!!!

解决方案 »

  1.   

    POI 3.5 beta 4, and Office Open XML Support (2008-12-01)
    We are currently working to support the new Office Open XML file formats, such as XLSX and PPTX, which were introduced in Office 2007.
    官方说支持XLSX和PPTX
      

  2.   

    以前无聊时候写的一点东西,翻出来给你参考一下!import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Iterator;import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFCell;
    import org.apache.poi.hssf.usermodel.HSSFRichTextString;
    import org.apache.poi.hssf.usermodel.HSSFRow;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.poifs.filesystem.POIFSFileSystem;public class DefaultPOIExcelReader implements POIExcelReader { private String xlsPath;
    private int currentSheetNumber;
    private HSSFWorkbook workBook;

    public DefaultPOIExcelReader(String xlsPath) {
    this.xlsPath = xlsPath;
    currentSheetNumber = 0;
    iniXLSDocument();
    }

    @Override
    public HSSFSheet readSingleSheet(int sheetNumber) {
    // -- SSFSheet         sheet    = 
    int totalSheets = workBook.getNumberOfSheets();
    if(totalSheets < sheetNumber) return null;
    currentSheetNumber = sheetNumber; // update current sheet number;
    return workBook.getSheetAt(sheetNumber);
    } @Override
    public POIExcelReportWriter getExcelWriter(String xlsNewPath) {
    return new DefaultPOIExcelReportWriter(xlsNewPath);
    }

    @Override
    public ExcelContentHandler getDefaultPOIExcelHandler() { return new DefaultExcelContentHandler(workBook.getSheetAt(currentSheetNumber));
    }

    private void iniXLSDocument() {
    InputStream inputStream = null;
    // -- String xlsPath = "D://workspaces//testPOI.xls";
    try {
    inputStream = new FileInputStream (xlsPath);
    } catch (FileNotFoundException e) {
    System.out.println ("File not found in the specified path.");
    e.printStackTrace ();
    } try
    {
    POIFSFileSystem fileSystem = null;
    fileSystem = new POIFSFileSystem (inputStream);
    workBook = new HSSFWorkbook (fileSystem);
    }
    catch (IOException e)
    {
    System.err.println ("an error occured on reading excel book.");
    e.printStackTrace ();
    }
    }

    public static void main(String[] args) {
    InputStream inputStream = null;
    String testingpath = "D://workspaces//project manager.xls";
    String xlsPath = "D://workspaces//testPOI.xls";
    try
    {
    inputStream = new FileInputStream (testingpath);
    }
    catch (FileNotFoundException e)
    {
    System.out.println ("File not found in the specified path.");
    e.printStackTrace ();
    } POIFSFileSystem fileSystem = null; try
    {
    fileSystem = new POIFSFileSystem (inputStream); HSSFWorkbook      workBook = new HSSFWorkbook (fileSystem);
    HSSFSheet         sheet    = workBook.getSheetAt (0);
    Iterator<HSSFRow> rows     = sheet.rowIterator();
    int flag = 0;
    while (rows.hasNext ())
    {
    HSSFRow row = rows.next ();
    flag++;
    if(flag == 22) break; // display row number in the console.
    System.out.println ("Row No.: " + row.getRowNum ()); // once get a row its time to iterate through cells.
    Iterator<HSSFCell> cells = row.cellIterator(); while (cells.hasNext ())
    {
    HSSFCell cell = cells.next ();
    if(row.getRowNum () == 18) {
    System.out.println ("Cell No.: " + cell.getCellNum ());
    } /*
     * Now we will get the cell type and display the values
     * accordingly.
     */
    switch (cell.getCellType ())
    {
    case HSSFCell.CELL_TYPE_NUMERIC :
    {

    // cell type numeric.
    System.out.println ("Numeric value: " + cell.getNumericCellValue ());

    break;
    }

    case HSSFCell.CELL_TYPE_STRING :
    {

    // cell type string.
    HSSFRichTextString richTextString = cell.getRichStringCellValue ();
    if(handleEmptyString(richTextString.getString()).startsWith("Script:")) {
    System.out.println ("find correct script........ ");
    }
    System.out.println ("String value: " + richTextString.getString ());
    break;
    }

    default :
    {

    // types other than String and Numeric.
    // -- System.out.println ("Type not supported.");

    break;
    }
    }
    }
    }
    }
    catch (IOException e)
    {
    e.printStackTrace ();
    }
    }

    private static String handleEmptyString(String sourceStr) {
    if(sourceStr == null || "".equals(sourceStr))
    return "";
    while(sourceStr.charAt(0) == ' ') {
    sourceStr = sourceStr.substring(1);
    }
    return sourceStr.trim();
    } @Override
    public int getTotalSheetsNumber() {
    if(workBook == null) return 0;
    return workBook.getNumberOfSheets(); }
      

  3.   

    介绍了 Java读取excel2007
    http://kxjhlele.javaeye.com/blog/321392
      

  4.   

    介绍了 Java读取excel2007
    http://kxjhlele.javaeye.com/blog/321392
      

  5.   

    http://apache.freelamp.com/poi/dev/src/
    这里是官方包下载地址,
    含有docs
    关于Excel2007的支持依据官方说明:
    HSSF and XSSF
    HSSF and XSSF are the set of APIs for reading and writing Microsoft Excel 97-2007 and OOXML spreadsheets using (only) Java.
    相关的事例代码文档中都有的