利用POI的HSSFWorkbook操作Excel文件


Apache POI提供了操作microsoft documnent的java API,本文主要介紹HSSFWorkbook如何操作Excel文檔。

HSSF是Horrible SpreadSheet Format的簡稱,主要用來讀取和寫入Excel文檔

一,引入poi依賴

       <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.0.1</version>
        </dependency>

二,寫入數據到Excel中

編程步驟:

(1)實例化HSSFWorkbook對象

(2)創建sheet頁,行,單元格並設置其中的值

(3)實例化文件輸出流FileOutputStream

 1     public static void writeDataToExcel() {
 2 
 3         HSSFWorkbook wb = new HSSFWorkbook();
 4         //創建sheet頁
 5         HSSFSheet sheet = wb.createSheet("學生表");
 6         //創建行
 7         HSSFRow row0 = sheet.createRow(0);
 8         //創建單元格
 9         HSSFCell cell0 = row0.createCell(0);
10         //往單元格中設置值
11         cell0.setCellValue("學生成績表");
12         //合並單元格
13         sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 3));
14 
15         //創建表格第一行
16         HSSFRow row1 = sheet.createRow(1);
17         row1.createCell(0).setCellValue("姓名");
18         row1.createCell(1).setCellValue("性別");
19         row1.createCell(2).setCellValue("科目");
20         row1.createCell(3).setCellValue("成績");
21 
22         HSSFRow row2 = sheet.createRow(2);
23         row2.createCell(0).setCellValue("小琦");
24         row2.createCell(1).setCellValue("男");
25         row2.createCell(2).setCellValue("數據結構");
26         row2.createCell(3).setCellValue("100");
27 
28        ......
29 
30         FileOutputStream outputStream = null;
31         try {
32             outputStream = new FileOutputStream("E:\\test.xls");
33             //輸出到excel文件中
34             wb.write(outputStream);
35             outputStream.flush();
36         } catch (Exception e) {
37             e.printStackTrace();
38         } finally {
39             try {
40                 outputStream.close();
41                 wb.close();
42             } catch (IOException e) {
43                 e.printStackTrace();
44             }
45         }
46 
47     }

返回結果:

三,讀取Excel文檔中的數據

編程步驟:

(1)實例化文件輸入流FileInputStream

(2)實例化HSSFWorkbook對象

(3)使用getSheetAt獲取sheet頁,遍歷並獲取單元格中的值

 1 public static List<Student> readFromExcel(String filePath) {
 2 
 3         FileInputStream fileInputStream = null;
 4         HSSFWorkbook workbook = null;
 5 
 6         List<Student> studentList = new ArrayList<>();
 7         try {
 8             fileInputStream = new FileInputStream(filePath);
 9 
10             workbook = new HSSFWorkbook(fileInputStream);
11 
12             //獲取excel文檔中第一個表單
13             HSSFSheet sheet = workbook.getSheetAt(0);
14             //遍歷返回的結果
15             for (Row row : sheet) {
16                 //從數據行開始讀取
17                 if (row.getRowNum() < 2) {
18                     continue;
19                 }
20                 Student stu = new Student();
21                 stu.setName(row.getCell(0).getStringCellValue());
22                 stu.setGender(row.getCell(1).getStringCellValue());
23                 stu.setSubjectName(row.getCell(2).getStringCellValue());
24                 stu.setGrade(row.getCell(3).getStringCellValue());
25                 studentList.add(stu);
26             }
27 
28         } catch (Exception e) {
29             e.printStackTrace();
30         } finally {
31             try {
32                 workbook.close();
33                 fileInputStream.close();
34             } catch (Exception e) {
35                 e.printStackTrace();
36             }
37         }
38 
39         return studentList;
40     }

四,總結:

利用HSSFWorkbook的 createSheet,createRow,createCell,setCellValue,getSheetAt,getCell,getStringCellValue等

這些方法可以很方便的操作Excel文檔,從而完成像表格文件導入或導出的功能。


免責聲明!

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



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