How to read and write excel file

后端 未结 22 3152
北荒
北荒 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 rowIter = mySheet.rowIterator();
            for ( Iterator rowIterator = mySheet.rowIterator() ;rowIterator.hasNext(); )
            {
                for (  Iterator 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

提交回复
热议问题