Java POI 創建excel表格,讀取excel表格信息


office 2007及以上版本

前提條件,引入jar包

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.17</version>
    </dependency>
    <dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.17</version>

創建excel表格

public class WriteExcel07 {
       @Test  
        public void writeExcel() throws IOException{  
            //創建工作簿  
            XSSFWorkbook workBook = new XSSFWorkbook();  
            //創建工作表  
            XSSFSheet sheet = workBook.createSheet("helloWorld");  
            //創建行  
            XSSFRow row = sheet.createRow(0);  
            //創建單元格,操作第三行第三列  
            XSSFCell cell = row.createCell(0, CellType.STRING);  
            cell.setCellValue("helloWorld");  
              
            FileOutputStream outputStream = new FileOutputStream(new File("C:\\Users\\shay_deng\\Desktop\\test.xlsx"));  
            workBook.write(outputStream);  
              
            workBook.close();//記得關閉工作簿  
        }  
}

讀取excel表格信息

public class ReadExcel07 {
    @Test  
    public void readExcel() throws IOException{  
        FileInputStream inputStream = new FileInputStream(new File("C:\\Users\\shay_deng\\Desktop\\test.xlsx"));  
        //讀取工作簿  
        XSSFWorkbook workBook = new XSSFWorkbook(inputStream);  
        //讀取工作表  
        XSSFSheet sheet = workBook.getSheetAt(0);  
        //讀取行  
        XSSFRow row = sheet.getRow(0);  
        //讀取單元格  
        XSSFCell cell = row.getCell(0);  
        String value = cell.getStringCellValue();  
          
        System.out.println(value);  
          
        inputStream.close();//關閉工作簿  
        workBook.close();  
    }
}

1.合並單元格,屬於工作表,獨立創建,應用於工作表

2.樣式,屬於工作表,由工作簿創建,應用於單元格

3.字體,屬於工作表,由工作簿創建,應用於樣式

4.設置背景顏色,一定要先設置顏色的填充模式

public class TestPOIExcelStyle {
     @Test  
        public void testExcelStyle() throws IOException{  
            //1.創建工作簿  
            HSSFWorkbook workBook = new HSSFWorkbook();  
              
            //創建合並單元格對象  
            CellRangeAddress rangeAddress = new CellRangeAddress(2, 2, 2, 4);  
            //創建樣式  
            HSSFCellStyle style = workBook.createCellStyle();  
            style.setAlignment(HorizontalAlignment.CENTER);  
            style.setVerticalAlignment(VerticalAlignment.CENTER);  
            //創建字體  
            HSSFFont font = workBook.createFont();  
            font.setFontHeightInPoints((short) 16);  
            //font.setFontHeight((short)320); 效果和上面一樣。用這個方法設置大小,值要設置為字體大小*20倍,具體看API文檔  
            font.setColor(HSSFColor.GREEN.index);  
            font.setBold(true);  
            style.setFont(font);  
            //設置背景  
            style.setFillPattern(FillPatternType.SOLID_FOREGROUND);  
            style.setFillForegroundColor(HSSFColor.RED.index);  
              
            //2.創建工作表  
            HSSFSheet sheet = workBook.createSheet("helloWorld");  
            //添加合並區域  
            sheet.addMergedRegion(rangeAddress);  
              
            //3.創建行  
            HSSFRow row = sheet.createRow(2);  
            //4.創建單元格  
            HSSFCell cell = row.createCell(2);  
            cell.setCellValue("helloWorld");  
            cell.setCellStyle(style);  
              
            //輸出  
            FileOutputStream outputStream = new FileOutputStream(new File("C:\\Users\\shay_deng\\Desktop\\test.xls"));  
            workBook.write(outputStream);  
              
            workBook.close();  
            outputStream.close();  
        }  
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM