zoukankan      html  css  js  c++  java
  • java输出mysql到excel表格的简单用法

    1》导入包

    jxl.jar下载地址:https://pan.baidu.com/s/10ijORF5sNdmZv3SyN8ImaQ密码:ue50

    mysql的对应jar:https://pan.baidu.com/s/1ZiB3kPewCdMK_WatNjUtiA密码:7afv

    源码:

    新建类

    public class excelTest {
        int id;
        String name;
        String num;
        public String getNum() {
            return num;
        }
        public void setNum(String num) {
            this.num = num;
        }
        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;
        }
        
    }

    java代码:

    import java.io.File;
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.List;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.GregorianCalendar;
    import java.util.Random;
    
    import org.java.team.Writeteam;
    import org.java.tianbao.Tianbao;
    import org.java.util.DBUtil;
    import org.java.xiaowailianjie.WriteXiaowailianjie;
    
    
    
    import jxl.Workbook;
    import jxl.write.Label;
    import jxl.write.WritableSheet;
    import jxl.write.WritableWorkbook;
    import jxl.write.WriteException;
    import jxl.write.biff.RowsExceededException;
    
    public class a {
    
          public static void main(String[] args) throws RowsExceededException, WriteException, IOException {
              
              //1. 导出Excel的路径
              String filePath = "C:/export.xls";
              WritableWorkbook wwb =null;
              Connection connection=DBUtil.getConnection();
                List<excelTest> excelTests=new ArrayList<excelTest>();
                PreparedStatement preparedStatement=null;
                String sql="select * from excelTest";
                ResultSet resultSet=null;
                excelTest exce=null;
              try {
                  wwb = Workbook.createWorkbook(new File(filePath));
              } catch (Exception e) {
                  e.printStackTrace();
              }
               
              //创建Excel表的"学生"区域的数据
              WritableSheet sheet = wwb.createSheet("学生",0);//或者rwb.getSheet(0)获取第一个区域
              try {
                  //2. 连接数据库的几行代码
                  preparedStatement=connection.prepareStatement(sql);
                    
                    resultSet=preparedStatement.executeQuery();
                  
                  while(resultSet.next())
                {
                      exce=new excelTest();
                      exce.setId(resultSet.getInt("id"));
                      exce.setName(resultSet.getString("name"));
                    exce.setNum(resultSet.getString("num"));
                    excelTests.add(exce);
                    
                }
       sheet.addCell(
    new Label(0,0,"编号")); sheet.addCell(new Label(1,0,"姓名")); sheet.addCell(new Label(2,0,"学号")); for(int i = 0; i<excelTests.size(); i++){ //Number对应数据库的int类型数据 sheet.addCell(new jxl.write.Number(0,i+1,excelTests.get(i).getId())); //Label对应数据库String类型数据 sheet.addCell(new Label(1,i+1,excelTests.get(i).getName())); sheet.addCell(new Label(2,i+1,excelTests.get(i).getNum())); } wwb.write(); } catch (SQLException e) { e.printStackTrace(); }finally{ wwb.close(); } } }

    数据库截图

    导出的excel表格截图

  • 相关阅读:
    基于摸板匹配的目標跟蹤算法
    spoj 2713 Can you answer these queries IV
    zoj 3633 Alice's present
    hdu 3642 Get The Treasury
    poj 1195 Mobile phones
    poj 2760 End of Windless Days
    zoj 3540 Adding New Machine
    spoj 1716 Can you answer these queries III
    spoj 1043 Can you answer these queries I
    spoj 2916 Can you answer these queries V
  • 原文地址:https://www.cnblogs.com/837634902why/p/10543836.html
Copyright © 2011-2022 走看看