java&数据库:POI(一)——读取excel

admin 716 0

一切为了作业!本来想用txt文件,使用IO流随便搞一下,但是感觉数据的呈现方式不太好,本来想用python结果被图形化界面劝退。还是使用netbeans的图形化界面简单写一下吧!

java&数据库:POI(一)——读取excel

POI是一个由Apache提供的jar包,用于支持处理Excel、Word、PPT等文件。

官网下载地址:http://poi.apache.org/

导入jar包

尤其是这两个,大坑!必须导入!

java&数据库:POI(一)——读取excel

UI设计

java&数据库:POI(一)——读取excel

数据获取

先定位工作簿,再定位表,再用增强for循环遍历每一个单元格。

            XSSFWorkbook wb = new XSSFWorkbook("C:\\Users\\lance\\Desktop\\data.xlsx");
            XSSFSheet S = wb.getSheet("S");
            XSSFSheet SPJ = wb.getSheet("SPJ");
            String text = new String();
            for(Row row:S)
            {
                
                for(Cell cell:row)
                {
                    cell.setCellType(CellType.STRING);
                    
                    System.out.printf("%-8s",cell.getStringCellValue() );
                    text+=String.format("%-10s", cell.getStringCellValue());
                }
                text+="\n";
                System.out.println();
            }
            text+="\n";text+="\n";
            for(Row row:SPJ)
            {
                
                for(Cell cell:row)
                {
                    cell.setCellType(CellType.STRING);
                    //System.out.print(cell.getStringCellValue());
                    System.out.printf("%-8s",cell.getStringCellValue() );
                    text+=String.format("%-10s", cell.getStringCellValue());
                }
                text+="\n";
                System.out.println();
            }
            dakuang.setText(text);
 
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }

发表评论 取消回复
表情 图片 链接 代码

分享