java实现excel public void createExcel(){ String SheetName = "新立自办案件及线索报送考评表 "; String[] cellName; cellName = new String[] { "单位" , " 总数", "科级干部 "}; HSSFWorkbook workbook = new HSSFWorkbook(); /*
public void createExcel(){ String SheetName = "新立自办案件及线索报送考评表 "; String[] cellName; cellName = new String[] { "单位" , " 总数", "科级干部 "}; HSSFWorkbook workbook = new HSSFWorkbook(); /******************* 样式定义开始 *******************/ HSSFCellStyle style = workbook.createCellStyle(); HSSFCellStyle style2 = workbook.createCellStyle(); // style.setBorderTop(HSSFCellStyle.BORDER_THIN); // 字体设置 HSSFFont font = workbook.createFont(); font.setFontName( "宋体" );// 设置字体 // font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 粗体显示 font.setFontHeightInPoints(( short) 11); // 设置字体大小 font.setBoldweight(HSSFFont. BOLDWEIGHT_BOLD); // 设置字体加粗 style.setFont(font); style.setAlignment(HSSFCellStyle. ALIGN_CENTER); // 设置文本居中 style2.setAlignment(HSSFCellStyle. ALIGN_CENTER); // 设置文本居中 /******************* 样式定义结束 *******************/ // 设置模板sheet HSSFSheet sheet1 = workbook.createSheet(); // 设置模板名称,防止中文乱码,重要 // workbook.setSheetName(0, SheetName); // 添加标题行 HSSFRow row_title = sheet1.createRow(( short) 0); // 添加标题行所属的列 for ( int i = 0; i < cellName. length; i++) { HSSFCell cell = row_title.createCell(( short) i); cell.setCellType(HSSFCell. CELL_TYPE_STRING); // 设置编码格式,防止中文乱码,重要 cell.setEncoding(HSSFCell. ENCODING_UTF_16); cell.setCellValue(cellName[i]); // 设置列宽度,1 个字符700宽度 (一个中文的长度,同字体大小相关 ) int length = (cellName[i].length() * 1000); if (i == 5) { length = 12000; } sheet1.setColumnWidth(( short) i, ( short) length); // 设置文本样式 cell.setCellStyle(style); } //页面条件查询 String endYf = getRequest().getParameter( "endYf"); String startYf = getRequest().getParameter("startYf" ); String nf = getRequest().getParameter( "nf"); Mapmap = jxkpService.queryJxkp(nf, startYf,endYf); for ( int i = 0; i < map.size(); i++) { HSSFRow row = sheet1.createRow(( short) (i + 1));// 生成一行 JxkpHz bean = map.get(i); HSSFCell cell = row.createCell(( short) 0); cell.setCellType(HSSFCell. CELL_TYPE_STRING); cell.setEncoding(HSSFCell. ENCODING_UTF_16); cell.setCellValue(bean.getDq()); cell.setCellStyle(style2); HSSFCell cell1 = row.createCell(( short) 1); cell1.setCellType(HSSFCell. CELL_TYPE_STRING); cell1.setEncoding(HSSFCell. ENCODING_UTF_16); cell1.setCellValue(bean.getZbajzs()); cell1.setCellStyle(style2); HSSFCell cell2 = row.createCell(( short) 2); cell2.setCellType(HSSFCell. CELL_TYPE_STRING); cell2.setEncoding(HSSFCell. ENCODING_UTF_16); cell2.setCellValue(bean.getZbajkj()); cell2.setCellStyle(style2); } response.reset(); response.setContentType( "application/vnd.ms-excel" ); try { response. setHeader( "Content-Disposition" , "attachment;filename=" + URLEncoder.encode(SheetName.toString() + ".xls" , "UTF8" )); workbook.write(response.getOutputStream()); response.getOutputStream().flush(); response.getOutputStream().close(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }