introduce
Jakarta POI is a set of Java APIs for accessing Microsoft-formatted documents. Jakarta POI consists of many components, including HSSF for operating Excel format files and HWPF for operating Word. Among various components, only HSSF for operating Excel is relatively mature. Official homepage http://poi.apache.org/index.html, API documentation http://poi.apache.org/apidocs/index.html
accomplish
Complete comments have been added to the code.
import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;import java.util.ArrayList;import java.util.List;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFCellStyle;import org.apache.poi.hssf.usermodel.HSSFRow;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;public class ExcelOperate { public static void main(String[] args) { // Create Excel table createExcel(getStudent()); // Read Excel table List<Student> list = readExcel(); System.out.println(list.toString()); } /** * Initialize data* * @return data*/ private static List<Student> getStudent() { List<Student> list = new ArrayList<Student>(); Student student1 = new Student("Xiao Ming", 8, "Second Grade"); Student student2 = new Student("Xiao Guang", 9, "Second Grade"); Student student3 = new Student("Xiao Hua", 10, "Fourth Grade"); list.add(student1); list.add(student2); list.add(student3); return list; } /** * Create Excel * * @param list * Data*/ private static void createExcel(List<Student> list) { // Create an Excel file HSSFWorkbook workbook = new HSSFWorkbook(); // Create a worksheet HSSFSheet sheet = workbook.createSheet("Student Table One"); // Add the header row HSSFRow hssfRow = sheet.createRow(0); // Set the cell format to center HSSFCellStyle cellStyle = workbook.createCellStyle(); cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // Add header content HSSFCell headCell = hssfRow.createCell(0); headCell.setCellValue("name"); headCell.setCellStyle(cellStyle); headCell = hssfRow.createCell(1); headCell.setCellValue("Age"); headCell.setCellStyle(cellStyle); headCell = hssfRow.createCell(2); headCell.setCellValue("Grade"); headCell.setCellStyle(cellStyle); // Add data content for (int i = 0; i < list.size(); i++) { hssfRow = sheet.createRow((int) i + 1); Student student = list.get(i); // Create a cell and set the value HSSFCell cell = hssfRow.createCell(0); cell.setCellValue(student.getName()); cell.setCellStyle(cellStyle); cell = hssfRow.createCell(1); cell.setCellValue(student.getAge()); cell.setCellStyle(cellStyle); cell = hssfRow.createCell(2); cell.setCellValue(student.getGrade()); cell.setCellStyle(cellStyle); } // Save Excel file try { OutputStream outputStream = new FileOutputStream("D:/students.xls"); workbook.write(outputStream); outputStream.close(); } catch (Exception e) { e.printStackTrace(); } } /** * Read Excel * * @return Data Collection */ private static List<Student> readExcel() { List<Student> list = new ArrayList<Student>(); HSSFWorkbook workbook = null; try { // Read Excel file InputStream inputStream = new FileInputStream("D:/students.xls"); workbook = new HSSFWorkbook(inputStream); inputStream.close(); } catch (Exception e) { e.printStackTrace(); } // Loop worksheet for (int numSheet = 0; numSheet < workbook.getNumberOfSheets(); numSheet++) { HSSFSheet hssfSheet = workbook.getSheetAt(numSheet); if (hssfSheet == null) { continue; } // Loop row for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) { HSSFRow hssfRow = hssfSheet.getRow(rowNum); if (hssfRow == null) { continue; } // Save the contents in the cell into the collection Student student = new Student(); HSSFCell cell = hssfRow.getCell(0); if (cell == null) { continue; } student.setName(cell.getStringCellValue()); cell = hssfRow.getCell(1); if (cell == null) { continue; } student.setAge((int) cell.getNumericCellValue()); cell = hssfRow.getCell(2); if (cell == null) { continue; } student.setGrade(cell.getStringCellValue()); list.add(student); } } return list; }}Attach the Student class code
public class Student { private String name; private int age; private String grade; public Student() { } public Student(String name, int age, String grade) { super(); this.name = name; this.age = age; this.grade = grade; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getGrade() { return grade; } public void setGrade(String grade) { this.grade = grade; } @Override public String toString() { return "Student [name=" + name + ", age=" + age + ", grade=" + grade + "]"; }} Test results
Exported Excel tables
Students
Print the read Excel data
[Student [name=Xiao Ming, age=8, grade=Second grade], Student [name=Xiao Guang, age=9, grade=3], Student [name=Xiao Hua, age=10, grade=4]]
Summarize
The above is the entire content of this article. I hope the content of this article will be of some help to your study or work. If you have any questions, you can leave a message to communicate.