@RequestMapping("/export")
public void exportcc(HttpServletResponse response){
try {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFCellStyle style = wb.createCellStyle();
HSSFFont font = wb.createFont();
font.setBoldweight((short)18);
HSSFSheet sheet = wb.createSheet("sheet1");
HSSFRow row = sheet.createRow(0);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setFont(font);
HSSFCell cell = row.createCell(0);
cell.setCellValue("ID");
cell.setCellStyle(style);
cell = row.createCell(1);
cell.setCellValue("創建者");
cell.setCellStyle(style);
cell = row.createCell(2);
cell.setCellValue("IP");
cell.setCellStyle(style);
cell = row.createCell(3);
cell.setCellValue("MachineId");
cell.setCellStyle(style);
cell = row.createCell(4);
cell.setCellValue("AppId");
cell.setCellStyle(style);
cell = row.createCell(5);
cell.setCellValue("EnvId");
cell.setCellStyle(style);
cell = row.createCell(6);
cell.setCellValue("Tag");
cell.setCellStyle(style);
List<EnvMachineRelation> list = envMachineRelationRepository.findByAppId(195);
for(int i =1;i<list.size()+1;i++){
export(i,sheet,wb,list.get(i-1),style);
}
FileOutputStream fout = new FileOutputStream("舊諮詢編號1.xls");
try {
String fileName = new String("test1.xls".getBytes(),"ISO8859-1");
response.setContentType("application/octet-stream;charset=ISO8859-1");
response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
OutputStream os = response.getOutputStream();
wb.write(os);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void export(int index,HSSFSheet sheet,HSSFWorkbook wb,EnvMachineRelation rank,HSSFCellStyle style){
HSSFRow row = sheet.createRow(index);
HSSFCell cell = row.createCell(0);
cell.setCellValue(rank.getId());
cell.setCellStyle(style);
cell = row.createCell(1);
cell.setCellValue(rank.getCreator());
cell.setCellStyle(style);
cell = row.createCell(2);
cell.setCellValue(rank.getMachineIp());
cell.setCellStyle(style);
cell = row.createCell(3);
cell.setCellValue(rank.getMachineId());
cell.setCellStyle(style);
cell = row.createCell(4);
cell.setCellValue(rank.getAppId());
cell.setCellStyle(style);
cell = row.createCell(5);
cell.setCellValue(rank.getEnvId());
cell.setCellStyle(style);
cell = row.createCell(6);
cell.setCellValue(rank.getTag());
cell.setCellStyle(style);
}
pom依賴
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.6</version>
</dependency>