日期:2014-05-17 浏览次数:20784 次
用的poi
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
ByteArrayOutputStream bao = null;
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet;
HSSFRow row;
HSSFCell cell;
sheet = workbook.createSheet();
row = sheet.createRow(0);
cell = row.createCell(0);
cell.setCellValue("第一行第一列");
cell = row.createCell(1);
cell.setCellValue("第一行第二列");
cell = row.createCell(2);
cell.setCellValue("第一行第三列");
cell = row.createCell(3);
cell.setCellValue("第一行第四列");
cell = row.createCell(4);
cell.setCellValue("第一行第五列");
cell = row.createCell(5);
cell.setCellValue("第一行第六列");
for (int i = 0; i < 30000; i++) {
row = sheet.createRow(i + 1);
cell = row.createCell(0);
cell.setCellValue("123");
cell = row.createCell(1);
cell.setCellValue("123");
cell = row.createCell(2);
cell.setCellValue("123");
cell = row.createCell(3);
cell.setCellValue("");
cell = row.createCell(4);
cell.setCellValue("");
cell = row.createCell(5);
cell.setCellValue("123");
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
response.setContentType("application/octet-stream");
response.setHeader("Content-length", workbook.getBytes() + "");
String filename = new String((sdf.format(new Date()) + "tt.xls")
.getBytes("GBK"), "iso8859-1");
response.setHeader("Content-disposition", "attachment; filename="
+ filename);
OutputStream reponseOs = response.getOutputStream();
workbook.write(reponseOs);
reponseOs.flush();
reponseOs.close();
response.flushBuffer();