public HSSFWorkbook CreateJZZJSList() {// 繪制ExcelString fileName = "自動分析詳情";HSSFWorkbook wb = new HSSFWorkbook();//創建工作相簿對象HSSFSheet sheet = wb.createSheet(fileName); // --->創建了一個工作簿sheet.setDefaultRowHeight((short) 600); // ---->有得時候你想設置統一單元格的高度,就用這個方法sheet.setDefaultColumnWidth((short) 40);sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));// 表格樣式固定為15列,此處合并列// 繪制第一行HSSFRow firstrow = sheet.createRow(0); // 下標為0的行開始HSSFCell title = firstrow.createCell(0);HSSFCellStyle style = wb.createCellStyle();title.setCellStyle(style);title.setCellValue(fileName);// 繪制標題行HSSFRow colTitlerow5 = sheet.createRow(1); // 繪制第2行HSSFCell colTitlerowCol5 = colTitlerow5.createCell(0);colTitlerowCol5.setCellStyle(style);colTitlerowCol5.setCellValue("序號");HSSFCell colTitlerowCol6 = colTitlerow5.createCell(1);colTitlerowCol6.setCellStyle(style);colTitlerowCol6.setCellValue("單位名稱");//查詢數據以行形式便利for (int i = 0; i < 6; i++) {HSSFRow row = sheet.createRow(i + 2);//創建行HSSFCell row5 = row.createCell(0);//創建列row5.setCellStyle(style);row5.setCellValue((i + 1));HSSFCell row6 = row.createCell(1);//創建列row6.setCellStyle(style);row6.setCellValue(("我是第二列"));}return wb;}
2.再寫一個進入類
@RequestMapping("/inser")public void btnDaoChuExcel_Click(HttpServletResponse response) {// 生成Xls并導出String xlsName = "自動分析詳情表";HSSFWorkbook wb = CreateJZZJSList();try {response.reset();String fileName = new String((xlsName + ".xls").getBytes("gb2312"), "ISO8859-1");response.setContentType("application/x-msdownload");response.setHeader("Content-disposition", "attachment; filename=" + fileName);OutputStream out = response.getOutputStream();wb.write(out);out.flush();out.close();wb.close();} catch (IOException e) {e.printStackTrace();}}
解析
?