• eclipse sql server 导出excel文件


    Jxl.jar

    访问Excel的Jar包

    注意:支持以.xls结尾的Excel文件,可能不支持.xlsx结尾的 

    下载地址:

    程序所需要得包:

    程序代码:

    package partice;
    import java.io.File;
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.ResultSetMetaData;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.Date;
    import javax.swing.JOptionPane;
    import jxl.Workbook;
    import jxl.read.biff.BiffException;
    import jxl.write.Label;
    import jxl.write.WritableSheet;
    import jxl.write.WritableWorkbook;
    import jxl.write.WriteException;
    import jxl.write.biff.RowsExceededException;
    public class export {
        static ResultSet rs = null;
        static ResultSetMetaData rsData = null;
        static int sheetIndex = 0; // 计数器,起始为0
        static int perPageSize = 60000;// excel2003每张表最大为65536行
        static String fileName = "调剂学生信息.xls";// 新建的表格名称
        static String URL = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=studentdatabase";// 数据库IP地址
        static String DriverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
        static String username = "sa";// 用户
        static String password = "19940503";// 密码
    public  void main(String[] args) throws Exception {
            connectOracle();
        }
    public void connectOracle() throws SQLException, ClassNotFoundException {
            Connection conn = null;
            Class.forName(DriverName);
            long begintime = System.currentTimeMillis();
            try {
                System.out.println("数据库连接开始:" + new Date(System.currentTimeMillis()));
                conn = DriverManager.getConnection(URL, username, password);
                Statement state = conn.createStatement(
                        ResultSet.TYPE_SCROLL_INSENSITIVE,
                        ResultSet.CONCUR_READ_ONLY);
                String sql = "select * from register";
                rs = state.executeQuery(sql);
                rsData = rs.getMetaData();// 获得所需要的rs信息
                rs.last();
                int size = rs.getRow();// 列数
                rs.beforeFirst();
                int sheetsize = size / perPageSize + 1;// sheetsize是sheet的总页数
                writeexcel(rs);// 新建一个excel,并且写入第一张表,每次只能写一张表
                for (int i = 1; i < sheetsize; i++) {
                    System.out.println(2);
                    updateexcel(rs);// 写入后面的后面的表
                }
            } catch (Exception e) {
                System.out.println("数据库连接错误");
                e.printStackTrace();
            } finally {
                conn.close();
                System.out.println("数据库连接关闭!用时:"
                        + (System.currentTimeMillis() - begintime) + "ms");
                JOptionPane.showMessageDialog(null, "导出成功", "提示信息", JOptionPane.WARNING_MESSAGE);
            }
        }
    static void writeexcel(ResultSet rs) throws IOException, Exception,
                WriteException {
            WritableWorkbook book = Workbook.createWorkbook(new File(fileName));
            WritableSheet sheet = book.createSheet("第" + (sheetIndex + 1) + "页",
                    sheetIndex);
            int i = 1;// 每一页设为第一行
            while (rs.next()) {
                for (int j = 1; j <= rsData.getColumnCount(); j++) {
                    Label label = new Label(j - 1, i, rs.getString(j) == null ? ""
                            : rs.getString(j).toString());
                    if (i == 1) {// 标题只加载第一次
                        Label titleLabel = new Label(j - 1, 0, rsData
                                .getColumnName(j));
                        sheet.addCell(titleLabel);
                    }
                    sheet.addCell(label);
                }
                i++;
                if (i > perPageSize) {
                    break;
                }
            }
            sheetIndex++;
            book.write();
            book.close();
        }
    static void updateexcel(ResultSet rs) throws BiffException, IOException,
                RowsExceededException, WriteException, SQLException {
            Workbook wb = Workbook.getWorkbook(new File(fileName));
            WritableWorkbook book = Workbook.createWorkbook(new File(fileName), wb);
            WritableSheet sheet = book.createSheet("第" + (sheetIndex + 1) + "页 ",
                    sheetIndex);
            int i = 1;// 每一页设为第一行
            while (rs.next()) {
                for (int j = 1; j <= rsData.getColumnCount(); j++) {
                    Label label = new Label(j - 1, i, rs.getString(j) == null ? ""
                            : rs.getString(j).toString());
                    if (i == 1) {// 标题只加载第一次
                        Label titleLabel = new Label(j - 1, 0, rsData
                                .getColumnName(j));
                        sheet.addCell(titleLabel);
                    }
                    sheet.addCell(label);
                }
                i++;
                if (i > perPageSize) {
                    break;
                }
            }
            sheetIndex++;
            book.write();
            book.close();
        }
    }

    对应数据库

     java实现数据库导出ECXL表格:ttps://blog.csdn.net/captian_900331/article/details/49689199

  • 相关阅读:
    单机 Nexus 部署
    Docker 部署 3 节点 ES 集群
    Harbor 高可用部署
    Python 第四次实验
    es入门
    Golang的Test的用法
    spring elastic
    golang下载包的时候出现 dial tcp 142.251.43.17:443: i/o timeout时候解决
    Java加密并压缩文件
    feign调用添加header
  • 原文地址:https://www.cnblogs.com/fcfc940503/p/10823856.html
Copyright © 2020-2023  润新知