利用POI将数据库表导出到Excel

Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能。它的主要结构包括:HSSF,XSSF等等。我们一般都是用于导出Excel,其他的相对少一点。

从数据库中导出到Excel的源码及步骤如下: 
1、首先要有一张表和数据 
数据库表

2、代码,利用JDBC连接数据库

import java.io.FileOutputStream;
import java.sql.DriverManager;
import java.sql.ResultSet;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

public class ReadExcelFromDB {

    public final static String outputFile="C:\\Users\\XueFei\\Desktop\\country.xlsx";

    public final static String url="jdbc:mysql://192.168.3.235:3306/sop";

    public final static String user="你的用户名";

    public final static String password="你的密码";

    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn=(Connection) DriverManager.getConnection(url, user, password);
            Statement stat = (Statement) conn.createStatement();
            ResultSet resultSet = stat.executeQuery("select * from t_bi_country;");
            XSSFWorkbook workbook=new XSSFWorkbook();
            XSSFSheet sheet=workbook.createSheet("countryDB");
            XSSFRow row = sheet.createRow((short)0);
            XSSFCell cell=null;
            cell=row.createCell((short)0);
            cell.setCellValue("code");
            cell=row.createCell((short)1);
            cell.setCellValue("shortname");
            cell=row.createCell((short)2);
            cell.setCellValue("name");
            cell=row.createCell((short)3);
            cell.setCellValue("englishname");
            int i=1;
            while(resultSet.next())
            {
                row=sheet.createRow(i);
                cell=row.createCell(0);
                cell.setCellValue(resultSet.getString("code"));
                cell=row.createCell(1);
                cell.setCellValue(resultSet.getString("shortName"));
                cell=row.createCell(2);
                cell.setCellValue(resultSet.getString("name"));
                cell=row.createCell(3);
                cell.setCellValue(resultSet.getString("englishName"));
                i++;
             }
            FileOutputStream FOut = new FileOutputStream(outputFile);
            workbook.write(FOut);
            FOut.flush();
            FOut.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64

3、运行之后就得到了我们想要的Excel表了。 
结果

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值