码迷,mamicode.com
首页 > 数据库 > 详细

java输出mysql到excel表格的简单用法

时间:2019-03-16 19:39:34      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:.text   rest   ble   编号   cti   mysql   throw   string类   and   

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表格截图

技术图片

 

java输出mysql到excel表格的简单用法

标签:.text   rest   ble   编号   cti   mysql   throw   string类   and   

原文地址:https://www.cnblogs.com/837634902why/p/10543836.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!