使用java将数据库表导出为Excel表

x33g5p2x  于2022-06-27 转载在 Java  
字(5.0k)|赞(0)|评价(0)|浏览(645)

1、建立数据库表的实体类

  1. public class Food implements Serializable {
  2. //食品id
  3. private int id;
  4. //菜名
  5. private String name;
  6. //菜品
  7. private String type;
  8. //价格
  9. private String pay;
  10. //所属食堂
  11. private String canteen;
  12. public Food() {
  13. }
  14. public int getId() {
  15. return id;
  16. }
  17. public void setId(int id) {
  18. this.id = id;
  19. }
  20. public String getName() {
  21. return name;
  22. }
  23. public void setName(String name) {
  24. this.name = name;
  25. }
  26. public String getType() {
  27. return type;
  28. }
  29. public void setType(String type) {
  30. this.type = type;
  31. }
  32. public String getPay() {
  33. return pay;
  34. }
  35. public void setPay(String pay) {
  36. this.pay = pay;
  37. }
  38. public String getCanteen() {
  39. return canteen;
  40. }
  41. public void setCanteen(String canteen) {
  42. this.canteen = canteen;
  43. }
  44. @Override
  45. public String toString() {
  46. return "Food{" +
  47. "id=" + id +
  48. ", name='" + name + '\'' +
  49. ", type='" + type + '\'' +
  50. ", pay='" + pay + '\'' +
  51. ", canteen='" + canteen + '\'' +
  52. '}';
  53. }
  54. }

2、通过JDBC查询数据库表中的数据

  1. public static void main(String[] args) throws Exception {
  2. List<Food> foods = findAll();
  3. for(Food food:foods){
  4. System.out.println(food);
  5. }
  6. }
  7. public static List<Food> findAll() throws Exception {
  8. Class.forName("com.mysql.jdbc.Driver");
  9. Connection conn = DriverManager.getConnection("jdbc:mysql:///food_consumption_manager", "root", "123456");
  10. String sql = "select * from food";
  11. PreparedStatement pstmt = conn.prepareStatement(sql);
  12. ResultSet rs = pstmt.executeQuery();
  13. List<Food> foodList = new ArrayList<Food>();
  14. while(rs.next()){
  15. Food food = new Food();
  16. food.setId(rs.getInt("id"));
  17. food.setName(rs.getString("name"));
  18. food.setType(rs.getString("type"));
  19. food.setPay(rs.getString("pay"));
  20. food.setCanteen(rs.getString("canteen"));
  21. foodList.add(food);
  22. }
  23. rs.close();
  24. pstmt.close();
  25. conn.close();
  26. return foodList;
  27. }

3、导入Excel表操作依赖包

  1. <dependency>
  2. <groupId>org.apache.poi</groupId>
  3. <artifactId>poi</artifactId>
  4. <version>3.17</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.apache.poi</groupId>
  8. <artifactId>poi-ooxml</artifactId>
  9. <version>3.17</version>
  10. </dependency>

4、将实体列表转化为HSSFWorkbook工作簿对象

  1. public static HSSFWorkbook export(List<Food> foods){
  2. System.out.println("进入HSSFWorkbook===========================");
  3. // 创建excel对象
  4. HSSFWorkbook wk = new HSSFWorkbook();
  5. // 创建一张食物信息表
  6. HSSFSheet sheet = wk.createSheet("食物信息表");
  7. // 创建标题行
  8. HSSFRow smallTitle = sheet.createRow(0);
  9. smallTitle.createCell(0).setCellValue("食品id");
  10. smallTitle.createCell(1).setCellValue("菜名");
  11. smallTitle.createCell(2).setCellValue("菜品");
  12. smallTitle.createCell(3).setCellValue("价格");
  13. smallTitle.createCell(4).setCellValue("食堂");
  14. int count=1;
  15. for(Food food:foods){
  16. HSSFRow row = sheet.createRow(count++);
  17. row.createCell(0).setCellValue(food.getId());
  18. row.createCell(1).setCellValue(food.getName());
  19. row.createCell(2).setCellValue(food.getType());
  20. row.createCell(3).setCellValue(food.getPay());
  21. row.createCell(4).setCellValue(food.getCanteen());
  22. }
  23. return wk;
  24. }

5、将HSSFWorkbook以流写入文件

  1. public static void toExcel(HSSFWorkbook wk) throws Exception {
  2. OutputStream outputStream = new FileOutputStream(new File("food.xls"));
  3. wk.write(outputStream);
  4. wk.close();
  5. }

完整代码

  1. public static void main(String[] args) throws Exception {
  2. List<Food> foods = findAll();
  3. for(Food food:foods){
  4. System.out.println(food);
  5. }
  6. HSSFWorkbook hw = export(foods);
  7. toExcel(hw);
  8. }
  9. public static List<Food> findAll() throws Exception {
  10. Class.forName("com.mysql.jdbc.Driver");
  11. Connection conn = DriverManager.getConnection("jdbc:mysql:///food_consumption_manager", "root", "123456");
  12. String sql = "select * from food";
  13. PreparedStatement pstmt = conn.prepareStatement(sql);
  14. ResultSet rs = pstmt.executeQuery();
  15. List<Food> foodList = new ArrayList<Food>();
  16. while(rs.next()){
  17. Food food = new Food();
  18. food.setId(rs.getInt("id"));
  19. food.setName(rs.getString("name"));
  20. food.setType(rs.getString("type"));
  21. food.setPay(rs.getString("pay"));
  22. food.setCanteen(rs.getString("canteen"));
  23. foodList.add(food);
  24. }
  25. rs.close();
  26. pstmt.close();
  27. conn.close();
  28. return foodList;
  29. }
  30. public static HSSFWorkbook export(List<Food> foods){
  31. System.out.println("进入HSSFWorkbook===========================");
  32. // 创建excel对象
  33. HSSFWorkbook wk = new HSSFWorkbook();
  34. // 创建一张食物信息表
  35. HSSFSheet sheet = wk.createSheet("食物信息表");
  36. // 创建标题行
  37. HSSFRow smallTitle = sheet.createRow(0);
  38. smallTitle.createCell(0).setCellValue("食品id");
  39. smallTitle.createCell(1).setCellValue("菜名");
  40. smallTitle.createCell(2).setCellValue("菜品");
  41. smallTitle.createCell(3).setCellValue("价格");
  42. smallTitle.createCell(4).setCellValue("食堂");
  43. int count=1;
  44. for(Food food:foods){
  45. HSSFRow row = sheet.createRow(count++);
  46. row.createCell(0).setCellValue(food.getId());
  47. row.createCell(1).setCellValue(food.getName());
  48. row.createCell(2).setCellValue(food.getType());
  49. row.createCell(3).setCellValue(food.getPay());
  50. row.createCell(4).setCellValue(food.getCanteen());
  51. }
  52. return wk;
  53. }
  54. public static void toExcel(HSSFWorkbook wk) throws Exception {
  55. OutputStream outputStream = new FileOutputStream(new File("food.xls"));
  56. wk.write(outputStream);
  57. wk.close();
  58. }

输出文件结果

CSDN 社区图书馆,开张营业!

深读计划,写书评领图书福利~

相关文章

最新文章

更多