开发者社区> 问答> 正文

Java Excel模版导出怎么做?

如题
有一个5行的表头模版,现需要将数据导出到该模版。这个如何做?

展开
收起
蛮大人123 2016-03-18 15:25:09 2840 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪
    List list = exampleService.queryForList();
    response.reset();// 清空输出流
    response.setHeader("Content-disposition",
    "attachment; filename=project.xls");// 设定输出文件头
    response.setContentType("application/msexcel");// 定义输出类型
    HSSFWorkbook wbook = new HSSFWorkbook();
    try {
    OutputStream os = response.getOutputStream();// 从响应里获取输出流
    HSSFSheet sheet = wbook.createSheet("项目");// 创建工作表
    sheet.setDefaultColumnWidth(20);// 设置表格默认宽度
    HSSFCellStyle style = wbook.createCellStyle();// 创建表格样式
    style.setVerticalAlignment(CellStyle.ALIGN_CENTER);// 设置文本居中
    HSSFRow row = sheet.createRow(0);// 表格标题行
    HSSFCell cell = null;
    for (int i = 0; i < PSHOW.PROJECT_ARRAY.length; i++) {
    cell = row.createCell(i);// 给这一行添加一个表格
    cell.setCellStyle(style);
    cell.setCellValue(PSHOW.PROJECT_ARRAY[i]);// 设置表格内容
    }
    for (int i = 0; i < list.size(); i++) {
    int j = 0;
    row = sheet.createRow(i + 1);
    cell = row.createCell(j++);
    cell.setCellValue(list.get(i).getProjectName());
    cell = row.createCell(j++);
    cell.setCellValue(list.get(i).getProjectNo());
    cell = row.createCell(j++);
    cell.setCellValue(list.get(i).getCompany());
    cell = row.createCell(j++);
    cell.setCellValue(list.get(i).getContactPerson());
    cell = row.createCell(j++);
    cell.setCellValue(list.get(i).getContact());
    cell = row.createCell(j++);
    cell.setCellValue(list.get(i).getCreateDate());
    cell = row.createCell(j++);
    cell.setCellValue(list.get(i).getStartDate());
    cell = row.createCell(j++);
    cell.setCellValue(list.get(i).getEndDate());
    cell = row.createCell(j++);
    cell.setCellValue(list.get(i).getRemarks());
    }
    wbook.write(os);// 写入到流中
    os.flush();
    os.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    return null;
    2019-07-17 19:06:50
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载