How to read and write excel file

后端 未结 22 2610
北荒
北荒 2020-11-22 04:49

I want to read and write an Excel file from Java with 3 columns and N rows, printing one string in each cell. Can anyone give me simple code snippet for this? Do I need to

相关标签:
22条回答
  • 2020-11-22 04:56
    String path="C:\\Book2.xlsx";
    try {
    
            File f = new File( path );
            Workbook wb = WorkbookFactory.create(f);
            Sheet mySheet = wb.getSheetAt(0);
            Iterator<Row> rowIter = mySheet.rowIterator();
            for ( Iterator<Row> rowIterator = mySheet.rowIterator() ;rowIterator.hasNext(); )
            {
                for (  Iterator<Cell> cellIterator = ((Row)rowIterator.next()).cellIterator() ; cellIterator.hasNext() ;  ) 
                {
                    System.out.println ( ( (Cell)cellIterator.next() ).toString() );
                }
                System.out.println( " **************************************************************** ");
            }
        } catch ( Exception e )
        {
            System.out.println( "exception" );
            e.printStackTrace();
        }
    

    and make sure to have added the jars poi and poi-ooxml (org.apache.poi) to your project

    0 讨论(0)
  • 2020-11-22 04:59

    You can also consider JExcelApi. I find it better designed than POI. There's a tutorial here.

    0 讨论(0)
  • 2020-11-22 04:59

    For reading a xlsx file we can use Apache POI libs Try this:

    public static void readXLSXFile() throws IOException
        {
            InputStream ExcelFileToRead = new FileInputStream("C:/Test.xlsx");
            XSSFWorkbook  wb = new XSSFWorkbook(ExcelFileToRead);
    
            XSSFWorkbook test = new XSSFWorkbook(); 
    
            XSSFSheet sheet = wb.getSheetAt(0);
            XSSFRow row; 
            XSSFCell cell;
    
            Iterator rows = sheet.rowIterator();
    
            while (rows.hasNext())
            {
                row=(XSSFRow) rows.next();
                Iterator cells = row.cellIterator();
                while (cells.hasNext())
                {
                    cell=(XSSFCell) cells.next();
    
                    if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING)
                    {
                        System.out.print(cell.getStringCellValue()+" ");
                    }
                    else if(cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC)
                    {
                        System.out.print(cell.getNumericCellValue()+" ");
                    }
                    else
                    {
                        //U Can Handel Boolean, Formula, Errors
                    }
                }
                System.out.println();
            }
    
        }
    
    0 讨论(0)
  • 2020-11-22 04:59

    Please use Apache POI libs and try this.

        try
        {
            FileInputStream x = new FileInputStream(new File("/Users/rajesh/Documents/rajesh.xls"));
    
            //Create Workbook instance holding reference to .xlsx file
            Workbook workbook = new HSSFWorkbook(x);
    
            //Get first/desired sheet from the workbook
            Sheet sheet = workbook.getSheetAt(0);
    
            //Iterate through each rows one by one
            for (Iterator<Row> iterator = sheet.iterator(); iterator.hasNext();) {
                Row row = (Row) iterator.next();
                for (Iterator<Cell> iterator2 = row.iterator(); iterator2
                        .hasNext();) {
                    Cell cell = (Cell) iterator2.next();
                    System.out.println(cell.getStringCellValue());              
                }               
            }         
            x.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
       }
    }
    
    0 讨论(0)
  • 2020-11-22 05:01

    For reading data from .xlsx workbooks we need to use XSSFworkbook classes.

    XSSFWorkbook xlsxBook = new XSSFWorkbook(fis);

    XSSFSheet sheet = xlsxBook.getSheetAt(0); etc.

    We need to use Apache-poi 3.9 @ http://poi.apache.org/

    For detailed info with example visit : http://java-recent.blogspot.in

    0 讨论(0)
  • 2020-11-22 05:01

    using spring apache poi repo

    if (fileName.endsWith(".xls")) {
    
    
    
    File myFile = new File("file location" + fileName);
                    FileInputStream fis = new FileInputStream(myFile);
    
                    org.apache.poi.ss.usermodel.Workbook workbook = null;
                    try {
                        workbook = WorkbookFactory.create(fis);
                    } catch (InvalidFormatException e) {
    
                        e.printStackTrace();
                    }
    
    
                    org.apache.poi.ss.usermodel.Sheet sheet = workbook.getSheetAt(0);
    
    
                    Iterator<Row> rowIterator = sheet.iterator();
    
    
                    while (rowIterator.hasNext()) {
                        Row row = rowIterator.next();
    
                        Iterator<Cell> cellIterator = row.cellIterator();
                        while (cellIterator.hasNext()) {
    
                            Cell cell = cellIterator.next();
                            switch (cell.getCellType()) {
                            case Cell.CELL_TYPE_STRING:
                                System.out.print(cell.getStringCellValue());
                                break;
                            case Cell.CELL_TYPE_BOOLEAN:
                                System.out.print(cell.getBooleanCellValue());
                                break;
                            case Cell.CELL_TYPE_NUMERIC:
                                System.out.print(cell.getNumericCellValue());
                                break;
                            }
                            System.out.print(" - ");
                        }
                        System.out.println();
                    }
                }
    
    0 讨论(0)
提交回复
热议问题