Java操作Excel(二)将数据库中的数据导出到Excel中

 

public void exportToExcel() {
  try {
   
   ArrayList list = this.getSelectedCourseStudentsTableData();
   System.out.println("=============size:" + list.size());
   if (list != null && list.size() > 0) {

    Long tp = new Long(System.currentTimeMillis());
    String path = getApplicationBean().getFilePath()
      + "printfiles/" + tp.toString() + ".xls";
    String name=path.substring(path.lastIndexOf('/')+1,path.length());
    
    OutputStream outf = new FileOutputStream(path);
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("StudentCource");
    sheet.setColumnWidth((short)0,(short)4000);  //设置单元格的长度
    sheet.setColumnWidth((short)3,(short)4000); 
    sheet.setColumnWidth((short)5,(short)6000); 
    sheet.setColumnWidth((short)6,(short)6000); 
    sheet.setHorizontallyCenter(true);

    HSSFRow row1 = sheet.createRow(0);

    HSSFCell cell1 = row1.createCell((short) 0);
    cell1.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置编码
    cell1.setCellValue("学号");
    
    cell1 = row1.createCell((short) 1);
    cell1.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置编码
    cell1.setCellValue("姓名");
    
    cell1 = row1.createCell((short) 2);
    cell1.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置编码
    cell1.setCellValue("性别");
    
    cell1 = row1.createCell((short) 3);
    cell1.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置编码
    cell1.setCellValue("学生类别");
    
    cell1 = row1.createCell((short) 4);
    cell1.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置编码
    cell1.setCellValue("年级");
    
    cell1 = row1.createCell((short) 5);
    cell1.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置编码
    cell1.setCellValue("所属学院");
    
    cell1 = row1.createCell((short) 6);
    cell1.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置编码
    cell1.setCellValue("所学专业");

    ArrayList selectedCourseStudentslist = this
      .getSelectedCourseStudentsTableData();
    for (int i = 0; i < selectedCourseStudentslist.size(); i++) {
     HSSFRow row = sheet.createRow(i+1);
     PgtStatusInfo pgtStatusInfo = (PgtStatusInfo) selectedCourseStudentslist
       .get(i);
     HSSFCell cell = row.createCell((short) 0);
     cell.setCellValue(pgtStatusInfo.getStudentNo());
     cell = row.createCell((short) 1);
     cell.setEncoding(HSSFCell.ENCODING_UTF_16);
     cell.setCellValue(pgtStatusInfo.getName());
     cell = row.createCell((short) 2);
     cell.setEncoding(HSSFCell.ENCODING_UTF_16);
     cell.setCellValue(pgtStatusInfo.getSexCn());
     cell = row.createCell((short) 3);
     cell.setEncoding(HSSFCell.ENCODING_UTF_16);
     cell.setCellValue(pgtStatusInfo.getPgtTypeCn());
     cell = row.createCell((short) 4);
     cell.setEncoding(HSSFCell.ENCODING_UTF_16);
     cell.setCellValue(pgtStatusInfo.getGradeCn());
     cell = row.createCell((short) 5);
     cell.setEncoding(HSSFCell.ENCODING_UTF_16);
     cell.setCellValue(pgtStatusInfo.getBelongDeptCn());
     cell = row.createCell((short) 6);
     cell.setEncoding(HSSFCell.ENCODING_UTF_16);
     cell.setCellValue(pgtStatusInfo.getSpeCn());
    }
    wb.write(outf);
    outf.close();

    this.excelLink ="./files/printfiles/" + name;
    this.excelFileLinkVisible = true;
    this.outputFileLinkVisible = false;
   } else {
    getApplicationBean().addMessage(FacesMessage.SEVERITY_INFO,
      "导出excel内容为空!", null);
   }

  } catch (Exception e) {
   getApplicationBean().addMessage(FacesMessage.SEVERITY_ERROR,
     "导出excel操作失败!", e.getMessage());
  }
 }

 
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
可以使用Apache POI库来实现将doris表数据导出excel。具体实现步骤如下: 1. 首先,需要连接到doris数据库,可以使用JDBC连接。 2. 查询doris表数据,并将数据存储到一个List或者数组。 3. 创建一个新的Excel文件,并创建一个工作表。 4. 遍历List或者数组数据,将数据写入到Excel文件。 5. 最后,保存Excel文件。 以下是一个简单的Java代码示例: ``` import java.io.FileOutputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExportDataToExcel { public static void main(String[] args) { try { // 连接到doris数据库 Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:9030/test", "root", ""); // 查询doris表数据 Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM doris_table"); // 将数据存储到List List<String[]> dataList = new ArrayList<String[]>(); while (rs.next()) { String[] data = new String[3]; data[0] = rs.getString("id"); data[1] = rs.getString("name"); data[2] = rs.getString("age"); dataList.add(data); } // 创建一个新的Excel文件 XSSFWorkbook workbook = new XSSFWorkbook(); // 创建一个工作表 org.apache.poi.ss.usermodel.Sheet sheet = workbook.createSheet("Sheet1"); // 遍历List数据,将数据写入到Excel文件 int rowNum = 0; for (String[] data : dataList) { Row row = sheet.createRow(rowNum++); int colNum = 0; for (String field : data) { Cell cell = row.createCell(colNum++); cell.setCellValue(field); } } // 保存Excel文件 FileOutputStream outputStream = new FileOutputStream("doris_data.xlsx"); workbook.write(outputStream); workbook.close(); System.out.println("数据已成功导出Excel文件!"); // 关闭连接 rs.close(); stmt.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值