public class Food implements Serializable {
//食品id
private int id;
//菜名
private String name;
//菜品
private String type;
//价格
private String pay;
//所属食堂
private String canteen;
public Food() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getPay() {
return pay;
}
public void setPay(String pay) {
this.pay = pay;
}
public String getCanteen() {
return canteen;
}
public void setCanteen(String canteen) {
this.canteen = canteen;
}
@Override
public String toString() {
return "Food{" +
"id=" + id +
", name='" + name + '\'' +
", type='" + type + '\'' +
", pay='" + pay + '\'' +
", canteen='" + canteen + '\'' +
'}';
}
}
public static void main(String[] args) throws Exception {
List<Food> foods = findAll();
for(Food food:foods){
System.out.println(food);
}
}
public static List<Food> findAll() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql:///food_consumption_manager", "root", "123456");
String sql = "select * from food";
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
List<Food> foodList = new ArrayList<Food>();
while(rs.next()){
Food food = new Food();
food.setId(rs.getInt("id"));
food.setName(rs.getString("name"));
food.setType(rs.getString("type"));
food.setPay(rs.getString("pay"));
food.setCanteen(rs.getString("canteen"));
foodList.add(food);
}
rs.close();
pstmt.close();
conn.close();
return foodList;
}
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
public static HSSFWorkbook export(List<Food> foods){
System.out.println("进入HSSFWorkbook===========================");
// 创建excel对象
HSSFWorkbook wk = new HSSFWorkbook();
// 创建一张食物信息表
HSSFSheet sheet = wk.createSheet("食物信息表");
// 创建标题行
HSSFRow smallTitle = sheet.createRow(0);
smallTitle.createCell(0).setCellValue("食品id");
smallTitle.createCell(1).setCellValue("菜名");
smallTitle.createCell(2).setCellValue("菜品");
smallTitle.createCell(3).setCellValue("价格");
smallTitle.createCell(4).setCellValue("食堂");
int count=1;
for(Food food:foods){
HSSFRow row = sheet.createRow(count++);
row.createCell(0).setCellValue(food.getId());
row.createCell(1).setCellValue(food.getName());
row.createCell(2).setCellValue(food.getType());
row.createCell(3).setCellValue(food.getPay());
row.createCell(4).setCellValue(food.getCanteen());
}
return wk;
}
public static void toExcel(HSSFWorkbook wk) throws Exception {
OutputStream outputStream = new FileOutputStream(new File("food.xls"));
wk.write(outputStream);
wk.close();
}
public static void main(String[] args) throws Exception {
List<Food> foods = findAll();
for(Food food:foods){
System.out.println(food);
}
HSSFWorkbook hw = export(foods);
toExcel(hw);
}
public static List<Food> findAll() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql:///food_consumption_manager", "root", "123456");
String sql = "select * from food";
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
List<Food> foodList = new ArrayList<Food>();
while(rs.next()){
Food food = new Food();
food.setId(rs.getInt("id"));
food.setName(rs.getString("name"));
food.setType(rs.getString("type"));
food.setPay(rs.getString("pay"));
food.setCanteen(rs.getString("canteen"));
foodList.add(food);
}
rs.close();
pstmt.close();
conn.close();
return foodList;
}
public static HSSFWorkbook export(List<Food> foods){
System.out.println("进入HSSFWorkbook===========================");
// 创建excel对象
HSSFWorkbook wk = new HSSFWorkbook();
// 创建一张食物信息表
HSSFSheet sheet = wk.createSheet("食物信息表");
// 创建标题行
HSSFRow smallTitle = sheet.createRow(0);
smallTitle.createCell(0).setCellValue("食品id");
smallTitle.createCell(1).setCellValue("菜名");
smallTitle.createCell(2).setCellValue("菜品");
smallTitle.createCell(3).setCellValue("价格");
smallTitle.createCell(4).setCellValue("食堂");
int count=1;
for(Food food:foods){
HSSFRow row = sheet.createRow(count++);
row.createCell(0).setCellValue(food.getId());
row.createCell(1).setCellValue(food.getName());
row.createCell(2).setCellValue(food.getType());
row.createCell(3).setCellValue(food.getPay());
row.createCell(4).setCellValue(food.getCanteen());
}
return wk;
}
public static void toExcel(HSSFWorkbook wk) throws Exception {
OutputStream outputStream = new FileOutputStream(new File("food.xls"));
wk.write(outputStream);
wk.close();
}
CSDN 社区图书馆,开张营业!
深读计划,写书评领图书福利~
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/weixin_43598687/article/details/125380604
内容来源于网络,如有侵权,请联系作者删除!