将数据库查询结果导出成Excel表格

使用Java代码,从数据库中获取结果集,将结果集导出成Excel表格形式。

从数据库中查询学生表所有数据,将其导出成Excel表格,点击查看学生表表结构 

package com.test.test.test1;

import com.test.test.db.StudentDb;
import com.test.test.entity.Student;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFCellUtil;
import org.apache.poi.ss.usermodel.*;

import java.io.File;
import java.util.List;

public class ExportExcelTest {

    private static final String OUT_PATH = "F:\\数据导出\\表格\\学生表";

    public static void main(String[] args) {

        try {
            String sql = "select * from student";

            // 从数据库中通过查询语句获取的结果集
            List<Student> lst = new StudentDb().getStudent(sql);

            String colName[] = {"学号", "姓名", "年龄", "性别", "地区", "专业", "班级", "名族"};

            // 创建工作簿对象
            HSSFWorkbook workbook = new HSSFWorkbook();

            // 创建sheet对象
            HSSFSheet sheet = workbook.createSheet();

            // 创建第1行
            HSSFRow row0 = sheet.createRow(0);
            // 设置行高
            row0.setHeightInPoints((short) 24);
            // 设置列宽
//        sheet.setColumnWidth(0,20*256);


            // 创建字体对象
            HSSFFont font0 = workbook.createFont();
            // 设置字体
            font0.setFontName("宋体");
            // 设置字体是否加粗
            font0.setBold(true);
            // 设置字体颜色
            font0.setColor(Font.COLOR_RED);
            // 设置字体高度
            font0.setFontHeightInPoints((short) 14);

            // 创建单元格样式
            HSSFCellStyle cellStyle0 = workbook.createCellStyle();
            // 设置水平方向上的文本对齐方式
            cellStyle0.setAlignment(HorizontalAlignment.CENTER);
            // 设置竖直方向上的文本对齐方式
            cellStyle0.setVerticalAlignment(VerticalAlignment.CENTER);
            // 设置单元格顶部边框使用的边框类型
            cellStyle0.setBorderTop(BorderStyle.THIN);
            // 设置单元格底部边框使用的边框类型
            cellStyle0.setBorderBottom(BorderStyle.THIN);
            // 设置单元格左边框使用的边框类型
            cellStyle0.setBorderLeft(BorderStyle.THIN);
            // 设置单元格右边框使用的边框类型
            cellStyle0.setBorderRight(BorderStyle.THIN);
            // 设置此样式的字体
            cellStyle0.setFont(font0);
            // 设置单元格文本是否换行
            cellStyle0.setWrapText(false);
            // 设置单元格前景填充颜色 【注意:确保前景色设置在背景色之前。】
            cellStyle0.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
            // 设置单元格背景填充颜色
            cellStyle0.setFillBackgroundColor(IndexedColors.BLACK.getIndex());
            // 设置一个充满了前景色的单元格.
            cellStyle0.setFillPattern(FillPatternType.SOLID_FOREGROUND);

            HSSFCellStyle cellStyle1 = workbook.createCellStyle();
            cellStyle1.setVerticalAlignment(VerticalAlignment.CENTER);


            for (int cell0i = 0; cell0i < colName.length; cell0i++) {
                HSSFCellUtil.createCell(row0, cell0i, colName[cell0i], cellStyle0);
            }

            for (int j = 0; j < lst.size(); j++) {
                Student stu = lst.get(j);
                // 为每一条记录创建一个行对象
                HSSFRow row1 = sheet.createRow(j + 1);
                row1.setHeightInPoints(24);
                row1.setRowStyle(cellStyle1);

                HSSFCell cell0 = row1.createCell(0);
                cell0.setCellValue(stu.getSid().toString());
                cell0.setCellStyle(cellStyle1);


                HSSFCell cell1 = row1.createCell(1);
                cell1.setCellValue(stu.getSname());
                cell1.setCellStyle(cellStyle1);

                HSSFCell cell2 = row1.createCell(2);
                cell2.setCellValue(stu.getSage().toString());

                HSSFCell cell3 = row1.createCell(3);
                cell3.setCellValue(stu.getSsex());

                HSSFCell cell4 = row1.createCell(4);
                cell4.setCellValue(stu.getSnativeplace());

                HSSFCell cell5 = row1.createCell(5);
                cell5.setCellValue(stu.getSmajor());

                HSSFCell cell6 = row1.createCell(6);
                cell6.setCellValue(stu.getSclass());

                HSSFCell cell7 = row1.createCell(7);
                cell7.setCellValue(stu.getSnative());

            }

            File file = new File(OUT_PATH);
            if (file.exists()) {
                System.out.println("文件的输出路径已存在 : " + OUT_PATH);
            } else {
                file.mkdirs();
                // 如果文件目录不存在,则逐级创建
                System.out.println("文件的输出路径为 : " + OUT_PATH);
            }

            String fileName = "A1";
            String fileSuffix = ".xls";
            workbook.write(new File(OUT_PATH, fileName + fileSuffix));


        } catch (Exception e) {
            System.out.println(e);
        } finally {
            try {
                // 打开资源管理器到指定文件夹
                Runtime.getRuntime().exec("cmd /c start explorer " + ExportExcelTest.OUT_PATH);
            } catch (Exception e) {
                System.out.println(e);
            }
        }
    }
}

程序运行所需jar包下载:https://pan.baidu.com/s/1x6yAAc-g0lKdYXydfBVasA 
提取码:3s78 


—— END ——

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值