POI操作EXCEL
class="java" name="code">
private void ExportExcel(OutputStream out) throws IOException {
HSSFWorkbook workbook = new HSSFWorkbook(); // 产生工作簿对象
HSSFSheet sheet = workbook.createSheet(); // 产生工作表对象
workbook.setSheetName(0, "统计");// 工作表名称
// 标题
HSSFRow row = sheet.createRow(0);// 创建行
HSSFCell cell = row.createCell(0);// 创建列
// 创建格式
HSSFCellStyle titleStyle = workbook.createCellStyle();
HSSFFont titleFont = workbook.createFont();
titleFont.setFontHeightInPoints((short) 12);
titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 加粗
titleFont.setColor(HSSFFont.COLOR_RED);// 字体颜色
titleStyle.setFont(titleFont);
// 垂直居中
titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
cell.setCellStyle(titleStyle);// 应用格式
cell.setCellValue("业务统计");// 设置单元格内容
sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, 9));// 合并单元格
sheet.setColumnWidth(0, 3000); // 设置列宽
sheet.setColumnWidth(1, 12000);
workbook.write(out);// 向外写出OutputStream out;
}
java中导出
response.setContentType("application/octet-stream");
response.addHeader("Content-Disposition", "attachment; filename=out.xls");
OutputStream out = new BufferedOutputStream(response.getOutputStream());
ExportExcel(out);
out.flush();
out.close();