1. 程式人生 > >springboot中整合POI

springboot中整合POI

1,匯入pom依賴

        <dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi</artifactId>
			<version>3.15</version>
		</dependency>
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml-schemas</artifactId>
			<version>3.15</version>
		</dependency>
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml</artifactId>
			<version>3.15</version>
        </dependency>

2.建立excel表格

 public static void createExcel(String excelName) throws Exception {

        //建立工作簿
        XSSFWorkbook wb = new XSSFWorkbook();
        //建立一個sheet
        XSSFSheet sheet = wb.createSheet();

        // 建立單元格樣式
        XSSFCellStyle style = wb.createCellStyle();
        style.setFillForegroundColor((short) 4); //設定要新增表格北京顏色
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND); //solid 填充
        style.setAlignment(XSSFCellStyle.ALIGN_CENTER); //文字水平居中
        style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);//文字垂直居中
        style.setBorderBottom(BorderStyle.THIN); //底邊框加黑
        style.setBorderLeft(BorderStyle.THIN);  //左邊框加黑
        style.setBorderRight(BorderStyle.THIN); // 有邊框加黑
        style.setBorderTop(BorderStyle.THIN); //上邊框加黑
        //為單元格新增背景樣式
        for (int i = 0; i < 6; i++) { //需要6行表格
            Row row = sheet.createRow(i); //建立行
            for (int j = 0; j < 6; j++) {//需要6列
                row.createCell(j).setCellStyle(style);
            }
        }


        //合併單元格
        sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, 0));//合併單元格,cellRangAddress四個引數,第一個起始行,第二終止行,第三個起始列,第四個終止列
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 1, 5));

        //tian入資料
        XSSFRow row = sheet.getRow(0); //獲取第一行
        row.getCell(1).setCellValue("2018期末考試"); //在第一行中建立一個單元格並賦值
        XSSFRow row1 = sheet.getRow(1); //獲取第二行,為每一列新增欄位
        row1.getCell(1).setCellValue("語文");
        row1.getCell(2).setCellValue("數學");
        row1.getCell(3).setCellValue("英語");
        row1.getCell(4).setCellValue("物理");
        row1.getCell(5).setCellValue("化學");
        XSSFRow row2 = sheet.getRow(2); //獲取第三行
        row2.getCell(0).setCellValue("張三");
        XSSFRow row3 = sheet.getRow(3); //獲取第四行
        row3.getCell(0).setCellValue("張三");
        XSSFRow row4 = sheet.getRow(4); //獲取第五行
        row4.getCell(0).setCellValue("張三");
        XSSFRow row5 = sheet.getRow(5); //獲取第五行
        row5.getCell(0).setCellValue("張三");
        //將資料寫入檔案
        FileOutputStream out = new FileOutputStream(excelName);
        wb.write(out);

    }
    

3.讀取excel表格檔案

public static void readExcel(String excelName) throws IOException {

        //將檔案讀入
        InputStream in  = new FileInputStream(new File(excelName));
        //建立工作簿
        XSSFWorkbook wb = new XSSFWorkbook(in);
        //讀取第一個sheet
        Sheet sheet = wb.getSheetAt(0);
        //獲取第二行
        Row row = sheet.getRow(1);
        //迴圈讀取科目
        for (int i = 1; i < 6; i++) {

            System.out.println(row.getCell(i).getAddress());
        }
    }