当前位置:   article > 正文

将数据库数据导入excel_导出数据到excel表格有什么好处

导出数据到excel表格有什么好处

所用技术:poi,记得导入相关依赖

 用的是原生的jdbc,只需要把数据库配置一下就行了

jdbc类

  1. package com.enter.swing;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.sql.*;
  5. import java.util.Properties;
  6. import java.util.ResourceBundle;
  7. public class JDBC {
  8. /**
  9. * 打开连接
  10. * @return
  11. */
  12. public static Connection getConn(){
  13. Connection conn = null;
  14. try {
  15. Class.forName("数据库驱动");
  16. conn = DriverManager.getConnection("数据库地址","账号","密码");
  17. } catch (Exception e) {
  18. e.printStackTrace();
  19. }
  20. return conn;
  21. }
  22. /**
  23. * 关闭连接
  24. */
  25. public static void closeDB(ResultSet rs, PreparedStatement pstmt,Connection conn){
  26. try {
  27. if(rs != null){
  28. rs.close();
  29. }
  30. } catch (SQLException throwables) {
  31. throwables.printStackTrace();
  32. }
  33. try {
  34. if(pstmt != null){
  35. pstmt.close();
  36. }
  37. } catch (SQLException throwables) {
  38. throwables.printStackTrace();
  39. }
  40. try {
  41. if(conn != null){
  42. conn.close();
  43. }
  44. } catch (SQLException throwables) {
  45. throwables.printStackTrace();
  46. }
  47. }
  48. public static void main(String[] args) {
  49. JDBC.getConn();
  50. }
  51. }

测试类

  1. package com.enter.swing;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.sql.Array;
  5. import java.sql.Connection;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.ResultSetMetaData;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. import org.apache.poi.hssf.usermodel.HSSFCell;
  12. import org.apache.poi.hssf.usermodel.HSSFRow;
  13. import org.apache.poi.hssf.usermodel.HSSFSheet;
  14. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  15. public class Test7 {
  16. private Connection conn;
  17. private PreparedStatement pstmt;
  18. private ResultSet rs;
  19. /**
  20. *
  21. * @param data 要打印的数据集合
  22. * @param path 打印的路径
  23. * @param title 标题(可以为空)
  24. */
  25. @SuppressWarnings("rawtypes")
  26. public static void PrintExcel(ArrayList data,String path,String[] title){
  27. // 1.创建工作簿对象
  28. HSSFWorkbook workbook = new HSSFWorkbook();
  29. // 2.创建工作表对象
  30. HSSFSheet sheet = workbook.createSheet();
  31. // 3.创建第一行
  32. HSSFRow row = sheet.createRow(0);
  33. HSSFCell cell = null;
  34. if(title!=null){
  35. // 4.添加标题栏
  36. for (int i = 0; i < title.length; i++) {
  37. cell = row.createCell(i);
  38. cell.setCellValue(title[i]);
  39. }
  40. // 5.向表格中插入数据
  41. for (int i = 0; i <data.size(); i++) {
  42. // 6.创建行对象
  43. HSSFRow nextrow = sheet.createRow(i+1);
  44. ArrayList als=(ArrayList)data.get(i);
  45. HSSFCell cell2 = null;
  46. for (int j = 0; j < als.size(); j++) {
  47. cell2 = nextrow.createCell(j);
  48. cell2.setCellValue(als.get(j)==null?"":als.get(j).toString());
  49. }
  50. }
  51. }else{
  52. // 5.向表格中插入数据
  53. for (int i = 0; i <data.size(); i++) {
  54. // 6.创建行对象
  55. HSSFRow nextrow = sheet.createRow(i);
  56. ArrayList als=(ArrayList)data.get(i);
  57. HSSFCell cell2 = null;
  58. for (int j = 0; j < als.size(); j++) {
  59. cell2 = nextrow.createCell(j);
  60. cell2.setCellValue(als.get(j)==null?"":als.get(i).toString());
  61. }
  62. }
  63. }
  64. File f = new File(path);
  65. try {
  66. f.createNewFile();
  67. FileOutputStream stream=new FileOutputStream(f);
  68. workbook.write(stream);
  69. //workbook.close();
  70. System.out.println("成生成Excel表格!");
  71. } catch (Exception e) {
  72. e.printStackTrace();
  73. }
  74. }
  75. /**
  76. * 查询自定义SQL
  77. * @return
  78. */
  79. @SuppressWarnings({ "unchecked", "rawtypes" })
  80. public ArrayList getSQLdata(String sql){
  81. ArrayList ls=new ArrayList();
  82. try {
  83. conn = JDBC.getConn();
  84. pstmt = conn.prepareStatement(sql);
  85. rs = pstmt.executeQuery();
  86. ResultSetMetaData metaData = rs.getMetaData();
  87. int columnCount = rs.getMetaData().getColumnCount();//获取列数
  88. while(rs.next()){
  89. ArrayList lsv=new ArrayList();
  90. for (int i = 0; i < columnCount; i++) {
  91. String cname=metaData.getColumnName(i+1);//根据下标拿到列名
  92. Object object = rs.getObject(cname);//根据列名拿到数据
  93. if(object==null){
  94. lsv.add(object);
  95. }else{
  96. lsv.add(object.toString());
  97. }
  98. }
  99. ls.add(lsv);
  100. }
  101. } catch(Exception e){
  102. }finally{
  103. try{
  104. JDBC.closeDB(rs, pstmt, conn);
  105. }catch(Exception ex){
  106. }
  107. }
  108. return ls;
  109. }
  110. public static void main(String[] args) {
  111. Test7 t=new Test7();
  112. ArrayList sqLdata = t.getSQLdata("select id,name,class_id from student");
  113. //传入的数据格式是ArrayList<ArrayList>
  114. Test7.PrintExcel(sqLdata, "D:\\poi_test1.xls", new String[]{ "编号", "姓名", "性别" });
  115. }
  116. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/967374
推荐阅读
相关标签
  

闽ICP备14008679号