Java——导出数据到excel表(Poi方式)

    xiaoxiao2021-12-14  20

    导出数据到xls格式的excel表

    jar包下载地址 http://download.csdn.net/detail/zhengyikuangge/9700556


    导出数据到xls格式的excel表

    // 声明一个工作薄 HSSFWorkbook wb = new HSSFWorkbook(); // 声明一个sheet并命名 HSSFSheet sheet = wb.createSheet("学生表"); HSSFCellStyle style = wb.createCellStyle(); // 创建第一行(也可以称为表头) HSSFRow row = sheet.createRow(0); // 样式字体居中 style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 给表头第一行一次创建单元格 HSSFCell cell = row.createCell(0); //row为第一行,当前cell为第一列 cell.setCellValue("第一列"); cell.setCellStyle(style); cell = row.createCell(1); //row为第一行,当前cell为第二列 cell.setCellValue("第二例"); cell.setCellStyle(style); cell = row.createCell(2); //row为第一行,当前cell为第三列 cell.setCellValue("第三列"); cell.setCellStyle(style); // 向单元格里填充数据 for (int i = 0; i < 3; i++) { row = sheet.createRow(i + 1); row.createCell(0).setCellValue("第"+(i+1)+"行第1列"); //row为第(i+1)行,当前cell为第一列 row.createCell(1).setCellValue("第"+(i+1)+"行第2列"); //row为第(i+1)行,当前cell为第二列 row.createCell(2).setCellValue("第"+(i+1)+"行第3列"); //row为第(i+1)行,当前cell为第三列 } try { FileOutputStream out = new FileOutputStream("E://学生表.xls"); //如果没有该文件则会新建 wb.write(out); out.close(); System.out.println("执行完成"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
    转载请注明原文地址: https://ju.6miu.com/read-968649.html

    最新回复(0)