将数据导入Excle表中

将数据导入Excle表中

public String getDataNumber() {
        WritableWorkbook book = null;
        WritableSheet sheet = null;
        OutputStream os = null;
        Date date = new Date();
        //获取表名如:tab1,tab2,tab3
        String[] tableNames = names.split(",");
        String bookName = new SimpleDateFormat("yyyyMMddHHmm").format(date);
        String queryDate = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(date);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String endTime = dateFormat.format(date).substring(0,14)+"00";
        String startTime = endTime.substring(0,11)+"00:00";
//        String projectPath = ClassUtils.getDefaultClassLoader().getResource("").getPath();
//        String path = projectPath.replaceAll("/", "\\\\").substring(1)+bookName+"_tidb.xls";
//        String path = projectPath+bookName+"_tidb.xls";
        String path = "/home"+bookName+".xls";
        try {
            os = new FileOutputStream(path);
            //打开文件
            book = Workbook.createWorkbook(os);
            //生成工作表
            sheet = book.createSheet("第一页", 1);
            //构造指名单元格位置的列名
            Label label1 = new Label(0, 0, "表名");
            Label label2 = new Label(1, 0, "数据数量");
            Label label3 = new Label(2, 0, "开始时间");
            Label label4 = new Label(3, 0, "结束时间");
            Label label5 = new Label(4, 0, "查询时间");
            sheet.setColumnView(2,25);
            sheet.setColumnView(3,25);
            sheet.setColumnView(4,25);
            //将定义好的单元格添加到工作表中
            sheet.addCell(label1);
            sheet.addCell(label2);
            sheet.addCell(label3);
            sheet.addCell(label4);
            sheet.addCell(label5);
        }catch (Exception e) {
            e.printStackTrace();
        }
        //遍历数据表,查询数据量
        for (int i = 0; i < tableNames.length; i++) {
            long count = 0;
            StringBuilder sql = new StringBuilder();
            sql.append(" select * from "+ tableNames[i]+" where createdate >= str_to_date('");
            sql.append(startTime);
            sql.append("','%Y-%m-%d %H:%i') and createdate <= str_to_date('");
            sql.append(endTime);
            sql.append("','%Y-%m-%d %H:%i') ");
            //统计表数据count
            try {
                //添加一行数据
                jxl.write.Label name = new jxl.write.Label(0, 1+i, tableNames[i]);
                jxl.write.Number number = new jxl.write.Number(1, 1+i, count);
                jxl.write.Label start = new jxl.write.Label(2, 1+i, startTime);
                jxl.write.Label end = new jxl.write.Label(3, 1+i, endTime);
                jxl.write.Label dateTime = new jxl.write.Label(4, 1+i, queryDate);
                sheet.addCell(name);
                sheet.addCell(number);
                sheet.addCell(start);
                sheet.addCell(end);
                sheet.addCell(dateTime);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //写入数据;关闭资源
        try {
            book.write();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (null != book){
                try {
                    book.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (WriteException e) {
                    e.printStackTrace();
                }
            }
            if (null != os){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return "查询完成,请在"+bookName+".xls";
    }

你可能感兴趣的:(Java进阶,数据库,java,Excle)