HSSFWorkbook workbook = new HSSFWorkbook();
OutputStream os = null;
try {
os = response.getOutputStream();
} catch (IOException e1) {
e1.printStackTrace();
}
response.reset();
response.setContentType("application/vnd.ms-excel");
if (request.getHeader("User-Agent").indexOf("Firefox") != -1) {
response.setHeader("Content-Disposition",
"attachment; filename=dc.xls");
} else {
response.setHeader("Content-Disposition",
"attachment; filename="+wu+".xls"); //导出文件名
}
HSSFCellStyle cellstyle = workbook.createCellStyle();
cellstyle.setBorderLeft((short) 1);
cellstyle.setBorderRight((short) 1);
cellstyle.setBorderTop((short) 1);
cellstyle.setBorderBottom((short) 1);
HSSFFont myfont = workbook.createFont();
myfont.setFontName("Verdana");
myfont.setFontHeightInPoints((short) 8);
cellstyle.setAlignment((short) 2);
cellstyle.setWrapText(true);
cellstyle.setFont(myfont);
HSSFSheet sheet = workbook.createSheet("new sheet");
sheet.setDefaultColumnWidth(Short.valueOf("15"));
HSSFRow row = null;
try {
row = sheet.createRow(0);
HSSFCell cell11 = row.createCell((short) 0);
cell11.setCellStyle(cellstyle);
cell11.setEncoding((short) 1);
cell11.setCellValue("列名1"); //列名
cell11 = row.createCell((short) 1);
cell11.setCellStyle(cellstyle);
cell11.setEncoding((short) 1);
cell11.setCellValue("列名2");
cell11 = row.createCell((short) 2);
cell11.setCellStyle(cellstyle);
cell11.setEncoding((short) 1);
cell11.setCellValue("列名3");
cell11 = row.createCell((short) 3);
cell11.setCellStyle(cellstyle);
cell11.setEncoding((short) 1);
cell11.setCellValue("列名4");
cell11 = row.createCell((short) 4);
cell11.setCellStyle(cellstyle);
cell11.setEncoding((short) 1);
cell11.setCellValue("列名5");
if (list1 != null && list1.size() > 0) { //list为从数据库读出的数据,以下是将list循环写入excel
for (int i = 0; i < list1.size(); i++) {
row = sheet.createRow((short) sheet.getLastRowNum() + 1);
HSSFCell cell = row.createCell((short) 0);
cell.setCellStyle(cellstyle);
cell.setEncoding((short) 1);
cell.setCellValue(list1.get(i).toString());
cell = row.createCell((short) 1);
cell.setCellStyle(cellstyle);
cell.setEncoding((short) 1);
cell.setCellValue(originality);
cell = row.createCell((short) 2);
cell.setCellStyle(cellstyle);
cell.setEncoding((short) 1);
cell.setCellValue(q);
cell = row.createCell((short) 3);
cell.setCellStyle(cellstyle);
cell.setEncoding((short) 1);
cell.setCellValue(1);
cell = row.createCell((short) 4);
cell.setCellStyle(cellstyle);
cell.setEncoding((short) 1);
cell.setCellValue("1111");
}
}
workbook.write(os);
os.flush();
os.close();
os = null;
} catch (Exception e) {
e.printStackTrace();
}