日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java导出excel弹出下载框_[Java教程]Springmvc和poi3.9导出excel并弹出下载框

發(fā)布時間:2023/12/18 java 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java导出excel弹出下载框_[Java教程]Springmvc和poi3.9导出excel并弹出下载框 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

[Java教程]Springmvc和poi3.9導出excel并彈出下載框

0 2013-09-06 20:00:07

Springmvc 和 poi3.9 用java程序從數(shù)據(jù)庫導出數(shù)據(jù)到excel(在博客園的第一篇原創(chuàng)博客)@RequestMapping(value = "/importexcel.htm", method = RequestMethod.GET) public ModelAndView _importExcel(HttpServletRequest request, HttpServletResponse response, Integer cId) throws Exception { // 獲得要導出的數(shù)據(jù)集 List> list = d_ExchangeCodeService.selectExcelRecord(cId); // 創(chuàng)建excel工作簿 Workbook wb = new HSSFWorkbook(); // 創(chuàng)建第一個sheet(頁),并命名 Sheet sheet = wb.createSheet(list.get(0).get("NAME").toString()); // 手動設置列寬。第一個參數(shù)表示要為第幾列設;,第二個參數(shù)表示列的寬度,n為列高的像素數(shù)。 sheet.setColumnWidth((short) 0, (short) (35.7 * 150)); sheet.setColumnWidth((short) 1, (short) (35.7 * 150)); sheet.setColumnWidth((short) 2, (short) (35.7 * 150)); sheet.setColumnWidth((short) 3, (short) (35.7 * 100)); sheet.setColumnWidth((short) 4, (short) (35.7 * 250)); sheet.setColumnWidth((short) 5, (short) (35.7 * 150)); sheet.setColumnWidth((short) 6, (short) (35.7 * 150)); // 創(chuàng)建第一行 Row row = sheet.createRow((short) 0); // 創(chuàng)建兩種單元格格式 CellStyle cs = wb.createCellStyle(); CellStyle cs2 = wb.createCellStyle(); // DataFormat df = wb.createDataFormat(); // 創(chuàng)建兩種字體 Font f = wb.createFont(); Font f2 = wb.createFont(); // 創(chuàng)建第一種字體樣式 f.setFontHeightInPoints((short) 10); f.setColor(IndexedColors.RED.getIndex()); f.setBoldweight(Font.BOLDWEIGHT_BOLD); // 創(chuàng)建第二種字體樣式 f2.setFontHeightInPoints((short) 10); f2.setColor(IndexedColors.BLACK.getIndex()); f2.setBoldweight(Font.BOLDWEIGHT_BOLD); // 設置第一種單元格的樣式 cs.setFont(f); cs.setBorderLeft(CellStyle.BORDER_THIN); cs.setBorderRight(CellStyle.BORDER_THIN); cs.setBorderTop(CellStyle.BORDER_THIN); cs.setBorderBottom(CellStyle.BORDER_THIN); // cs.setDataFormat(df.getFormat("#,##0.0")); // 設置第二種單元格的樣式 cs2.setFont(f2); cs2.setBorderLeft(CellStyle.BORDER_THIN); cs2.setBorderRight(CellStyle.BORDER_THIN); cs2.setBorderTop(CellStyle.BORDER_THIN); cs2.setBorderBottom(CellStyle.BORDER_THIN); // cs2.setDataFormat(df.getFormat("text")); // 創(chuàng)建列(每行里的單元格) Cell cell = row.createCell(0); cell.setCellValue("用戶名"); cell.setCellStyle(cs); cell = row.createCell(1); cell.setCellValue("訂單號"); cell.setCellStyle(cs); cell = row.createCell(2); cell.setCellValue("兌換券序列號"); cell.setCellStyle(cs); cell = row.createCell(3); cell.setCellValue("兌換券金額"); cell.setCellStyle(cs); cell = row.createCell(4); cell.setCellValue("兌換券類型名稱"); cell.setCellStyle(cs); cell = row.createCell(5); cell.setCellValue("使用時間"); cell.setCellStyle(cs); cell = row.createCell(6); cell.setCellValue("使用結束日期"); cell.setCellStyle(cs); DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); for (short i = 0; i < list.size(); i++) { // Row 行,Cell 方格 , Row 和 Cell 都是從0開始計數(shù)的 // 創(chuàng)建一行,在頁sheet上 row = sheet.createRow((short) i + 1); // 在row行上創(chuàng)建一個方格 cell = row.createCell(0); cell.setCellValue(list.get(i).get("usr_UserID") == null ? "未使用" : list.get(i).get("usr_UserID").toString()); cell.setCellStyle(cs2); cell = row.createCell(1); cell.setCellValue(list.get(i).get("ord_OrderID") == null ? "未使用" : list.get(i).get("ord_OrderID").toString()); cell.setCellStyle(cs2); cell = row.createCell(2); cell.setCellValue(list.get(i).get("Account").toString()); cell.setCellStyle(cs2); cell = row.createCell(3); cell.setCellValue(Double.parseDouble(list.get(i).get("Amount").toString())); cell.setCellStyle(cs2); cell = row.createCell(4); cell.setCellValue(list.get(i).get("NAME").toString()); cell.setCellStyle(cs2); cell = row.createCell(5); cell.setCellValue(list.get(i).get("UsedTime") == null ? "未使用" : df.format(list.get(i).get("UsedTime")).toString()); cell.setCellStyle(cs2); cell = row.createCell(6); cell.setCellValue(df.format(list.get(i).get("BlankOutTime")).toString()); cell.setCellStyle(cs2); } ByteArrayOutputStream os = new ByteArrayOutputStream(); try { wb.write(os); } catch (IOException e) { e.printStackTrace(); } byte[] content = os.toByteArray(); InputStream is = new ByteArrayInputStream(content); // 設置response參數(shù),可以打開下載頁面 response.reset(); response.setContentType("application/vnd.ms-excel;charset=utf-8"); response.setHeader("Content-Disposition", "attachment;filename=" + new String((list.get(0).get("NAME").toString() + ".xls").getBytes(), "iso-8859-1")); ServletOutputStream out = response.getOutputStream(); BufferedInputStream bis = null; BufferedOutputStream bos = null; try { bis = new BufferedInputStream(is); bos = new BufferedOutputStream(out); byte[] buff = new byte[2048]; int bytesRead; // Simple read/write loop. while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) { bos.write(buff, 0, bytesRead); } } catch (final IOException e) { throw e; } finally { if (bis != null) bis.close(); if (bos != null) bos.close(); } return null; }

本文網(wǎng)址:http://www.shaoqun.com/a/70018.html

*特別聲明:以上內容來自于網(wǎng)絡收集,著作權屬原作者所有,如有侵權,請聯(lián)系我們:admin@shaoqun.com。

Spring

0

總結

以上是生活随笔為你收集整理的java导出excel弹出下载框_[Java教程]Springmvc和poi3.9导出excel并弹出下载框的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。