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;