java寫簡單Excel 首行是目錄 然後前台下載

來源:互聯網
上載者:User

標籤:form   file   下載   exce   container   handle   res   while   chm   

頁面:<form action="${path}/xxx/xxx.do"  method="get" >          表格下載:<input type="submit" value="下載">      </form>

  

@RequestMapping("/download")public void download(HttpServletRequest request, HttpServletResponse response, HttpSession session)throws Exception {ArrayList<String> listTitle = new ArrayList<String>();listTitle.add("a");listTitle.add("b");listTitle.add("c");listTitle.add("d");listTitle.add("e");// 建立表格及其第一列名欄HSSFWorkbook wb = new HSSFWorkbook();HSSFSheet sheet = wb.createSheet("sheet1");HSSFRow row0 = sheet.createRow(0);for (int j = 0; j < 5; j++) {// 5列HSSFCell cell = row0.createCell((short) j);cell.setCellValue(listTitle.get(j).toString());}String maker = SystemUtil.getCureenUser(session);List<MainfestItem> list = transportService.getlist(maker);System.out.println(list.size());// 從第二行輸入for (int i = 1; i <= list.size(); i++) { // 幾行HSSFRow row = sheet.createRow(i);HSSFCell cell0 = row.createCell((short) 0);// 第n+1個cell0.setCellValue(list.get(i - 1).getId().toString());HSSFCell cell1 = row.createCell((short) 1);cell1.setCellValue(list.get(i - 1).getContainerid().toString());HSSFCell cell2 = row.createCell((short) 2);cell2.setCellValue(list.get(i - 1).getIsocode().toString());}ByteArrayOutputStream os = new ByteArrayOutputStream();wb.write(os);byte[] content = os.toByteArray();InputStream is = new ByteArrayInputStream(content);// 設定response參數,可以開啟下載頁面response.reset();response.setContentType("application/vnd.ms-excel;charset=utf-8");response.setHeader("Content-Disposition","attachment;filename=" + new String(("box" + ".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 (Exception e) {// TODO: handle exceptione.printStackTrace();} finally {if (bis != null)bis.close();if (bos != null)bos.close();}}

  

java寫簡單Excel 首行是目錄 然後前台下載

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.