请用POI写出一段java代码:读取动态数据,导出五行三列的数据,sheet名:用户数据,head:名称,年龄,性别。导出10条数据
时间: 2023-03-13 22:22:49 浏览: 42
import java.util.List;
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFRow;
public class POIExample {
public static void main(String[] args) throws Exception {
//Create Blank workbook
Workbook workbook = new XSSFWorkbook();
//Create sheet
XSSFSheet sheet = (XSSFSheet) workbook.createSheet("用户数据");
//Create row
XSSFRow rowhead = sheet.createRow(0);
rowhead.createCell(0).setCellValue("名称");
rowhead.createCell(1).setCellValue("年龄");
rowhead.createCell(2).setCellValue("性别");
//Get dynamic data from the database
List<User> userList = getDynamicDataFromDB();
//Create data rows
int rowNum = 1;
for(User user: userList) {
XSSFRow row = sheet.createRow(rowNum++);
row.createCell(0).setCellValue(user.getName());
row.createCell(1).setCellValue(user.getAge());
row.createCell(2).setCellValue(user.getGender());
}
//Write the output to a file
FileOutputStream fileOut = new FileOutputStream("poi-data.xlsx");
workbook.write(fileOut);
fileOut.close();
workbook.close();
}
private static List<User> getDynamicDataFromDB() {
//TODO: Return dynamic data from the database
List<User> userList = new ArrayList<User>();
//Add 10 users to userList
return userList;
}
static class User {
String name;
int age;
String gender;
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 getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
}
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)