Use Java to implement Excel import and export of form data
Abstract: In the daily software development process, the import and export of form data is a common requirement. This article will introduce how to use the Java programming language to implement the Excel import and export function of form data, and provide code examples.
1. Import form data
The first step is to introduce the Apache POI library. The POI project is a Java library developed by Apache for manipulating files in Microsoft Office formats, including Excel. To use POI in a Maven project, you need to add the following dependencies in the pom.xml file:
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency>
Create an Excel workbook object.
InputStream inputStream = new FileInputStream("data.xlsx"); Workbook workbook = new XSSFWorkbook(inputStream);
Get the first Sheet in Excel.
Sheet sheet = workbook.getSheetAt(0);
Loop through each row and read the form data.
Iterator<Row> rowIterator = sheet.iterator(); while (rowIterator.hasNext()) { Row row = rowIterator.next(); Iterator<Cell> cellIterator = row.cellIterator(); while (cellIterator.hasNext()) { Cell cell = cellIterator.next(); System.out.print(cell.toString() + " "); } System.out.println(); }
In the above code, we get the data of each cell by calling the cell.toString() method and output it to the console line by line.
2. Export form data
Create an Excel workbook object.
Workbook workbook = new XSSFWorkbook();
Create a new Sheet and set the Sheet name.
Sheet sheet = workbook.createSheet("表单数据");
Create header row.
Row headerRow = sheet.createRow(0);
Write header data.
Cell headerCell1 = headerRow.createCell(0); headerCell1.setCellValue("姓名"); Cell headerCell2 = headerRow.createCell(1); headerCell2.setCellValue("年龄");
Write form data.
List<User> userList = getUserList(); // 假设getUserList()方法返回一个包含表单数据的User对象列表 for (int i = 0; i < userList.size(); i++) { User user = userList.get(i); Row row = sheet.createRow(i + 1); Cell cell1 = row.createCell(0); cell1.setCellValue(user.getName()); Cell cell2 = row.createCell(1); cell2.setCellValue(user.getAge()); }
In the above code, we assume that the User object contains two attributes: name and age, and obtains user data through the getUserList() method.
Write Excel to file.
OutputStream outputStream = new FileOutputStream("data.xlsx"); workbook.write(outputStream); outputStream.close();
This article introduces how to use Java to implement the Excel import and export function of form data. Through the Apache POI library, we can easily read and write Excel files. When importing data, you need to create an Excel workbook object and traverse each row and cell; when exporting data, you need to create header rows and form data, and write Excel to the file. As shown above, the Excel import and export functions of form data can be realized through simple code examples, improving efficiency and flexibility in software development.
References:
The above is the detailed content of Implementing Excel import and export of form data using Java. For more information, please follow other related articles on the PHP Chinese website!