Đọc Ghi File Excel Trong Java Sử Dụng Apache POI - Viblo
Có thể bạn quan tâm
Nguồn: https://stackjava.com/library/doc-ghi-file-excel-bang-java-su-dung-apache-poi.html
1. Apache POI là gì?Apache POI là một thư viện mã nguồn mở cung cấp bởi apache được sử dụng để xử lý các file office như word, excel, powerpoint…
1.1 Xử lý file Excel với Apache POI
Apache POI xử lý các thành phần trong excel theo đúng lập trình hướng đối tượng – mỗi thành phần trong Excel đều được coi như 1 đối tượng.
Các class cơ bản được dùng để đọc ghi file Excel
HSSF: các class có tên bắt đầu là HSSF được dùng để sử lý các file Microsoft Excel 2003 (.xls) XSSF: các class có tên bắt đầu là XSSF được dùng để sử lý các file Microsoft Excel 2007 trở về sau (.xlsx) XSSFWorkbook và HSSFWorkbook là các class xử lý với Excel Workbook HSSFSheet và XSSFSheet là các class xử lý với Excel Worksheet Row: định nghĩa một dòng trong excel Cell: định nghĩa một ô trong excel (Xem thêm: phân biệt workbook với worksheet)
2. Download thư viện Apache POICho các file Microsoft Excel 2003
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </dependency>Cho các file Microsoft Excel 2007 trở về sau
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.17</version> </dependency> 3. Đọc ghi file Excel bằng Apache POI3.1 Ghi file Excel
Ví dụ đoạn code dưới đây tạo 1 file excel có name là “Demo-ApachePOI-Excel.xlsx”
Tạo 1 Worksheet có name là “Customer_Info”
dòng đầu tiên hiển thị “List Of Customer”
Các dòng tiếp theo hiển thị các thông tin của customer (id, name, email), mỗi thông tin ở 1 column.
package stackjava.com.apachepoiexcel.demo; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class WriteFileExcel { public static void main(String[] args) { System.out.println("Create file excel"); XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet("Customer_Info"); int rowNum = 0; Row firstRow = sheet.createRow(rowNum++); Cell firstCell = firstRow.createCell(0); firstCell.setCellValue("List of Customer"); List<Customer> listOfCustomer = new ArrayList<Customer>(); listOfCustomer.add(new Customer(1, "Sylvester Stallone", "[email protected]")); listOfCustomer.add(new Customer(2, "Tom Cruise", "[email protected]")); listOfCustomer.add(new Customer(3, "Vin Diesel", "[email protected]")); for (Customer customer : listOfCustomer) { Row row = sheet.createRow(rowNum++); Cell cell1 = row.createCell(0); cell1.setCellValue(customer.getId()); Cell cell2 = row.createCell(1); cell2.setCellValue(customer.getName()); Cell cell3 = row.createCell(2); cell3.setCellValue(customer.getEmail()); } try { FileOutputStream outputStream = new FileOutputStream("Demo-ApachePOI-Excel.xlsx"); workbook.write(outputStream); workbook.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println("Done"); } } package stackjava.com.apachepoiexcel.demo; public class Customer { private int id; private String name; private String email; // setter - getter }Kết quả: 
3.2 Đọc file Excel
Bây giờ mình sẽ thực hiện đọc lại file Excel vừa tạo ở trên:
package stackjava.com.apachepoiexcel.demo; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.DataFormatter; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ReadFileExcel { public static void main(String[] args) { try { FileInputStream excelFile = new FileInputStream(new File("Demo-ApachePOI-Excel.xlsx")); Workbook workbook = new XSSFWorkbook(excelFile); Sheet datatypeSheet = workbook.getSheetAt(0); DataFormatter fmt = new DataFormatter(); Iterator<Row> iterator = datatypeSheet.iterator(); Row firstRow = iterator.next(); Cell firstCell = firstRow.getCell(0); System.out.println(firstCell.getStringCellValue()); List<Customer> listOfCustomer = new ArrayList<Customer>(); while (iterator.hasNext()) { Row currentRow = iterator.next(); Customer customer = new Customer(); customer.setId(Integer.parseInt(fmt.formatCellValue(currentRow.getCell(0)))); customer.setName(currentRow.getCell(1).getStringCellValue()); customer.setEmail(currentRow.getCell(2).getStringCellValue()); listOfCustomer.add(customer); } for (Customer customer : listOfCustomer) { System.out.println(customer); } workbook.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }*Lưu ý: id của customer là integer nên khi ghi vào file excel nó sẽ là kiểu numeric, do đó khi đọc ra thì nó sẽ đọc là numeric và chuyển thành double, ví dụ 1 sẽ là 1.0. Do đó ta dùng DataFormatter để định dạng nó về kiểu string và parse lại thành integer.
Kết quả:
List of Customer Customer [id=1, name=Sylvester Stallone, [email protected]] Customer [id=2, name=Tom Cruise, [email protected]] Customer [id=3, name=Vin Diesel, [email protected]]Download code ví dụ trên tại đây
Nguồn: https://stackjava.com/library/doc-ghi-file-excel-bang-java-su-dung-apache-poi.html
Từ khóa » Thư Viện Excel Trong Java
-
Hướng Dẫn đọc Và Ghi File Excel Trong Java Sử Dụng Thư Viện ...
-
Hướng Dẫn đọc Và Ghi File Excel Trong Java Sử Dụng Thư Viện ... - Viblo
-
Đọc Ghi File Excel Trong Java Sử Dụng Apache POI - Openplanning
-
[Java] Thao Tác Với Excel File Bằng Apache POI - Bean's Blog
-
Đọc Ghi Excel File Sử Dụng Apache POI - Deft Blog
-
Xuất Dữ Liệu Ra File Excel Trong Java / TOP #10 Xem Nhiều Nhất ...
-
Xuất Dữ Liệu Ra File Excel Trong Java - Apache Poi 3.14
-
Sử Dụng Apache POI để đọc, Ghi Dữ Liệu Từ File Excel Trong Selenium
-
Java: Cách Tạo Và Chèn Dữ Liệu Vào File Excel | V1Study
-
Xem Nhiều 7/2022 # Hướng Dẫn Đọc Và Ghi File Excel Trong Java ...
-
Top #10 Ghi File Excel Trong Java Xem Nhiều Nhất, Mới Nhất 7 ...
-
[Java] Đọc Ghi File Excel Trong Java Với Jxl - Nguyễn Văn Quân
-
[Selenium Java] Bài 26: Đọc File Excel để Lấy Data Test | Anh Tester