1.导入相应的jar包(jxl.jar 和 数据库连接的jar包)

2.写数据库连接的工具类

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class Dbutil {
    /*
     * 功能:编写一个静态方法用于与数据库建立连接 输入参数:无 返回值:数据库连接对象
     */
    public static Connection getConnection() {
        // 定义一个连接对象
        Connection conn = null;
        // 定义连接数据库的URL资源
        String url = "jdbc:Oracle:thin:@localhost:1521:orcl";
        // 定义连接数据库的用户名称与密码
        String username = "root";
        String password = "root";
        // 加载数据库连接驱动
        String className = "oracle.jdbc.driver.OracleDriver";
        try {
            Class.forName(className);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 获取数据库的连接对象
        try {
            conn = DriverManager.getConnection(url, username, password);
            System.out.println("数据库连接建立成功...");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 返回连接对象
        return conn;
    }

public static void close(Connection c) {
        if (c != null) {
            try {
                c.close();
            } catch (Throwable e) {

e.printStackTrace();
            }
        }
    }

public static void close(PreparedStatement c) {
        if (c != null) {
            try {
                c.close();
            } catch (Throwable e) {

e.printStackTrace();
            }
        }
    }
}

3.写实体类

public class StuEntity {

private String id;
    private String office_id;
    private String name;
    private String enname;
    private String role_type;
    private String data_scope;
    private String is_sys;
    private String useable;
    private String create_by;
    private String create_date;
    private String update_by;
    private String update_date;
    private String remarks;
    private String del_flag;
    
    public StuEntity() {
        super();
    }

public StuEntity(String id, String office_id, String name, String enname, String role_type, String data_scope,
            String is_sys, String useable, String create_by, String create_date, String update_by, String update_date,
            String remarks, String del_flag) {
        super();
        this.id = id;
        this.office_id = office_id;
        this.name = name;
        this.enname = enname;
        this.role_type = role_type;
        this.data_scope = data_scope;
        this.is_sys = is_sys;
        this.useable = useable;
        this.create_by = create_by;
        this.create_date = create_date;
        this.update_by = update_by;
        this.update_date = update_date;
        this.remarks = remarks;
        this.del_flag = del_flag;
    }

public String getId() {
        return id;
    }

public void setId(String id) {
        this.id = id;
    }

public String getOffice_id() {
        return office_id;
    }

public void setOffice_id(String office_id) {
        this.office_id = office_id;
    }

public String getName() {
        return name;
    }

public void setName(String name) {
        this.name = name;
    }

public String getEnname() {
        return enname;
    }

public void setEnname(String enname) {
        this.enname = enname;
    }

public String getRole_type() {
        return role_type;
    }

public void setRole_type(String role_type) {
        this.role_type = role_type;
    }

public String getData_scope() {
        return data_scope;
    }

public void setData_scope(String data_scope) {
        this.data_scope = data_scope;
    }

public String getIs_sys() {
        return is_sys;
    }

public void setIs_sys(String is_sys) {
        this.is_sys = is_sys;
    }

public String getUseable() {
        return useable;
    }

public void setUseable(String useable) {
        this.useable = useable;
    }

public String getCreate_by() {
        return create_by;
    }

public void setCreate_by(String create_by) {
        this.create_by = create_by;
    }

public String getCreate_date() {
        return create_date;
    }

public void setCreate_date(String create_date) {
        this.create_date = create_date;
    }

public String getUpdate_by() {
        return update_by;
    }

public void setUpdate_by(String update_by) {
        this.update_by = update_by;
    }

public String getUpdate_date() {
        return update_date;
    }

public void setUpdate_date(String update_date) {
        this.update_date = update_date;
    }

public String getRemarks() {
        return remarks;
    }

public void setRemarks(String remarks) {
        this.remarks = remarks;
    }

public String getDel_flag() {
        return del_flag;
    }

public void setDel_flag(String del_flag) {
        this.del_flag = del_flag;
    }

@Override
    public String toString() {
        return "StuEntity [id=" + id + ", office_id=" + office_id + ", name=" + name + ", enname=" + enname
                + ", role_type=" + role_type + ", data_scope=" + data_scope + ", is_sys=" + is_sys + ", useable="
                + useable + ", create_by=" + create_by + ", create_date=" + create_date + ", update_by=" + update_by
                + ", update_date=" + update_date + ", remarks=" + remarks + ", del_flag=" + del_flag + "]";
    }
}

4.获取数据库表中的数据

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 com.ninemax.util.Dbutil;

public class StuService {
    /**
     * @return
     */
    public static List<StuEntity> getAllByDb() {
        List<StuEntity> list = new ArrayList<StuEntity>();
        try {
            Connection conn = null;
            conn = Dbutil.getConnection();
            // 创建预编译语句对象,一般都是用这个而不用Statement
            PreparedStatement pre = null;
            // 创建一个结果集对象
            ResultSet result = null;

String sql = "select * from SYS_ROLE";
            pre = conn.prepareStatement(sql);// 实例化预编译语句
            result = pre.executeQuery();// 执行查询,注意括号中不需要再加参数
            while (result.next()) {
                String id = result.getString("id");
                String office_id = result.getString("office_id");
                String name = result.getString("name");
                String enname = result.getString("enname");
                String role_type = result.getString("role_type");
                String data_scope = result.getString("data_scope");
                String is_sys = result.getString("is_sys");
                String useable = result.getString("useable");
                String create_by = result.getString("create_by");
                String create_date = result.getString("create_date");
                String update_by = result.getString("update_by");
                String update_date = result.getString("update_date");
                String remarks = result.getString("remarks");
                String del_flag = result.getString("del_flag");
                list.add(new StuEntity(id, office_id, name,enname,role_type,data_scope,is_sys,useable,create_by,create_date,update_by,update_date,remarks,del_flag));
            }

} catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return list;
    }

}

5.导入数据到excel表中,并以当前时间命名文件

import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ninemax.util.Dbutil;

import jxl.Workbook;
import jxl.format.Colour;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class sql2excel {

public void createXLS() {
        Connection conn = null;
        try {
            conn = Dbutil.getConnection();
            Date now = new Date();
            SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
            String nowdate = df.format(now);
            // 打开文件
            WritableWorkbook book = Workbook.createWorkbook(new File(nowdate + ".xls"));
            System.out.println(book);
            System.out.println(nowdate + ".xls");
            // 生成名为"第一页"的工作表,参数0表示这是第一
            WritableSheet sheet = book.createSheet("第一页", 0);

// 设置字体为宋体,16号字,加粗,颜色为黑色
            WritableFont font1 = new WritableFont(WritableFont.createFont("宋体"), 12, WritableFont.BOLD);
            font1.setColour(Colour.BLACK);
            WritableCellFormat format1 = new WritableCellFormat(font1);
            format1.setAlignment(jxl.format.Alignment.CENTRE);
            format1.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);

// Label labelA = new Label(0, 0, "CALL_GUID", format1);
            // Label labelB = new Label(1, 0, "RELATIONID", format1);
            // Label labelC = new Label(2, 0, "ANI", format1);
            // Label labelD = new Label(3, 0, "DNIS", format1);
            // Label labelE = new Label(4, 0, "STAFF_ID", format1);
            // Label labelF = new Label(5, 0, "CALLSTARTTIME", format1);
            // Label labelG = new Label(6, 0, "CALLENDTIME", format1);
            // Label labelH = new Label(7, 0, "CALLRESULT", format1);
            // Label labelI = new Label(8, 0, "CALLRESULTREASON_ID", format1);
            // Label labelJ = new Label(9, 0, "CALLREMARK", format1);
            // Label labelK = new Label(10, 0, "EVENT_GUID", format1);

Label labelA = new Label(0, 0, "id", format1);
            Label labelB = new Label(1, 0, "office_id", format1);
            Label labelC = new Label(2, 0, "name", format1);
            Label labelD = new Label(3, 0, "enname", format1);
            Label labelE = new Label(4, 0, "role_type", format1);
            Label labelF = new Label(5, 0, "data_scope", format1);
            Label labelG = new Label(6, 0, "is_sys", format1);
            Label labelH = new Label(7, 0, "useable", format1);
            Label labelI = new Label(8, 0, "create_by", format1);
            Label labelJ = new Label(9, 0, "create_date", format1);
            Label labelK = new Label(10, 0, "update_by", format1);
            Label labelL = new Label(11, 0, "update_date", format1);
            Label labelM = new Label(12, 0, "remarks", format1);
            Label labelN = new Label(13, 0, "del_flag", format1);

// 将定义好的单元格添加到工作表中
            // sheet.addCell(labelA);
            // sheet.addCell(labelB);
            // sheet.addCell(labelC);
            // sheet.addCell(labelD);
            // sheet.addCell(labelE);
            // sheet.addCell(labelF);
            // sheet.addCell(labelG);
            // sheet.addCell(labelH);
            // sheet.addCell(labelI);
            // sheet.addCell(labelJ);
            // sheet.addCell(labelK);
            sheet.addCell(labelA);
            sheet.addCell(labelB);
            sheet.addCell(labelC);
            sheet.addCell(labelD);
            sheet.addCell(labelE);
            sheet.addCell(labelF);
            sheet.addCell(labelG);
            sheet.addCell(labelH);
            sheet.addCell(labelI);
            sheet.addCell(labelJ);
            sheet.addCell(labelK);
            sheet.addCell(labelL);
            sheet.addCell(labelM);
            sheet.addCell(labelN);

// 创建预编译语句对象,一般都是用这个而不用Statement
            PreparedStatement pre = null;
            // 创建一个结果集对象
            ResultSet result = null;
            String sql = "select * from SYS_ROLE";// 预编译语句
            pre = conn.prepareStatement(sql);// 实例化预编译语句
            result = pre.executeQuery();// 执行查询,注意括号中不需要再加参数
            // 查询数据库中所有的数据
            List<StuEntity> list = StuService.getAllByDb();
            System.out.println(list.size());
//            System.out.println("------------------------"+list.toString());
            while (result.next()) {
                for (int i = 0; i < list.size(); i++) {
                    Label labelAi = new Label(0, i + 1, list.get(i).getId());
                    System.out.println("----------------------"+labelAi.toString());
                    Label labelBi = new Label(1, i + 1, list.get(i).getOffice_id());
                    Label labelCi = new Label(2, i + 1, list.get(i).getName());
                    Label labelDi = new Label(3, i + 1, list.get(i).getEnname());
                    Label labelEi = new Label(4, i + 1, list.get(i).getRole_type());
                    Label labelFi = new Label(5, i + 1, list.get(i).getData_scope());
                    Label labelGi = new Label(6, i + 1, list.get(i).getIs_sys());
                    Label labelHi = new Label(7, i + 1, list.get(i).getUseable());
                    Label labelIi = new Label(8, i + 1, list.get(i).getCreate_by());
                    Label labelJi = new Label(9, i + 1, list.get(i).getCreate_date());
                    Label labelKi = new Label(10, i + 1, list.get(i).getUpdate_by());
                    Label labelLi = new Label(11, i + 1, list.get(i).getUpdate_date());
                    Label labelMi = new Label(12, i + 1, list.get(i).getRemarks());
                    Label labelNi = new Label(13, i + 1, list.get(i).getDel_flag());

sheet.addCell(labelAi);
                    sheet.addCell(labelBi);
                    sheet.addCell(labelCi);
                    sheet.addCell(labelDi);
                    sheet.addCell(labelEi);
                    sheet.addCell(labelFi);
                    sheet.addCell(labelGi);
                    sheet.addCell(labelHi);
                    sheet.addCell(labelIi);
                    sheet.addCell(labelJi);
                    sheet.addCell(labelKi);
                    sheet.addCell(labelLi);
                    sheet.addCell(labelMi);
                    sheet.addCell(labelNi);

}
                System.out.println(sheet.toString());
            }
            
            // 写入数据并关闭文件
            book.write();
            book.close();
            System.out.println("创建文件成功!");

} catch (Exception e) {
            System.out.println(e);
        }
    }

public static void main(String[] args) {
        new sql2excel().createXLS();
    }
}

至此,大功告成。

转载于:https://www.cnblogs.com/xgwtzg/p/6179787.html

Java实现Oracle导出数据到Excel相关推荐

  1. Java使用poi导出数据到excel(包括xls和xlsx两种格式)并通过浏览器下载

    情景:将数据导出到excel是java开发常用的功能,数据量不大的时候,xls和xlsx两种格式的文件都行,但是数据量太大的时候就有区别了,xls格式的文件一个sheet页最多只能存六万多条数据,而x ...

  2. Oracle 导出数据到Excel,字符串类型的数字前面的零会被过滤掉的问题解决

    最近从plsql导出数据到Excel表后,类似[000000123123]的数据到了Excel会自动把数字前面的零过滤掉:如下所示: 实际上,很多情况下,这些被过滤的零是有意义的,所以需要在SQL的时 ...

  3. oracle导出表为excel文件路径,Oracle导出数据为excel或文本文件

    将oracle中表的数据导出为excel文件保存,在网上看了很多方法,总的来说有两种. 一是运用excel工具中通过ODBC连接oracle数据库,将文本导出. 一是通过oracle的命令导出.基本上 ...

  4. java调用npoi_NPOI导出数据到Excel

    前言 Asp.net操作Excel已经是老生长谈的事情了,可下面我说的这个NPOI操作Excel,应该是最好的方案了,没有之一,使用NPOI能够帮助开发者在没有安装微软Office的情况下读写Offi ...

  5. java导出为excel文件_java导出数据到excel文件

    有的时候,将一些有用的数据导出到excel是很有必要的.比如说,我现在在做一个学校的在线教学平台,有一个需求是:将学生成绩导出到excel文件中去. 那怎样实现用java导出数据到excel文件呢?? ...

  6. Java导出数据到Excel文件

    Java导出数据到Excel文件 前言 如何导出 导出的基本流程 测试结果 测试数据及结果 测试代码 ExcelExportUtil.class 遇到的问题 lombok的问题 解决 Cell.set ...

  7. java导出文件到excel文件怎么打开_Java导出数据到Excel文件

    Java导出数据到Excel文件需要的jar包:easypoi-0.1.3.jar, poi-3.7-20101029 package com.sais.inkaNet.reportStatistic ...

  8. java 从excel中读取数据_在Java中读取Excel文件的内容和导出数据到Excel文件中

    转自www.chianjavaworld.net 原作者:SonyMusic 读:rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr 在Java ...

  9. java导出excel带上进度条_导出数据至Excel前台js进度条不能隐藏

    在导出数据至Excel时,有时数据会比较大,响应的时间会比较长,想做一个提示进度条,在点击导出数据按钮进,进度条显示出来,在数据导出完毕并成功弹出保存对话框时,进度条自动隐藏起来,但现在有个问题,点击 ...

最新文章

  1. Java 代理(proxy)模式
  2. 8、D8: Default interface methods are only supported starting with Android N (--min-api 24): void
  3. Maven--资源文件resource的问题
  4. 学会怎样使用Jsp 内置标签、jstl标签库及自定义标签
  5. [转载].gdb调试器快速入门
  6. visual studio code安装_Deepin系统中如何安装Visual Studio Code
  7. appium使用真机做安卓移动端自动化测试
  8. Kafka从上手到实践 - Kafka CLI:Topic CLI Producer CLI | 凌云时刻
  9. 互联网创业的准备——行业与商业模式
  10. 测试日照强度的软件,日照分析软件FastSUN - 飞时达软件
  11. 史上最详细的人脸识别和活体检测技术介绍、原理剖析及产品应用!
  12. MATLAB图像处理植物叶片面积计算
  13. mtr--- 网络诊断工具
  14. 代码健壮性的几点思考
  15. html语言让动画停止,如何停止svg动画?
  16. python pip 连接超时,使用国内源下载
  17. 吸烟者问题C++实现
  18. Matlab的bsxfun函数
  19. EasyUI API
  20. 但是我喜欢计算机课用英语怎么说,语文课的英文

热门文章

  1. 不能使用泛型的形参创建对象_数据类型之----泛型
  2. 使用MybatisPlus在实体中添加数据库表中不存在的字段
  3. Vue中引入css文件
  4. 致传统企业朋友:不够痛就别微服务,有坑 (1)
  5. 温故而知新 forEach 无法中断(break)的问题
  6. [转]经典正则表达式
  7. PXE无人值守系统安装配置简要说明
  8. [译] Facebook杯2013年编程挑战赛——预选赛题目及答案
  9. small - HTML元素
  10. SpringBoot属性绑定内部原理(ok)