Java Excel模版导出怎么做?

Java Excel模版导出怎么做?

问题描述:

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

答非所问

补充:我是有一个Excel模板(带样式的),现需要将数据追加到模板Excel文件里

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;

POI 技术,简单点就是直接以CSV格式写到一个CSV文件中,excel也可以打开

poi导出Excel,网上有很多例子的