java poi 匯出excel

來源:互聯網
上載者:User

標籤:

poi的jar:http://poi.apache.org/

下載後會有很多jar,但是如果只是簡單的excel報表的話,匯入一個poi-版本號碼-日期.jar就可以了。

匯出代碼:

private void outputExcel(String queryDate, String[] headers,            List<List<String>> diaochas, HttpServletRequest request,            HttpServletResponse response) throws IOException {        HSSFWorkbook workbook = new HSSFWorkbook();        //createSheet(excel工作表名)        HSSFSheet sheet = workbook.createSheet(queryDate);        //下面是設定excel表中標題的樣式        HSSFCellStyle title_style = workbook.createCellStyle();        title_style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);        title_style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);        title_style.setBorderBottom(HSSFCellStyle.BORDER_THIN);        title_style.setBorderLeft(HSSFCellStyle.BORDER_THIN);        title_style.setBorderRight(HSSFCellStyle.BORDER_THIN);        title_style.setBorderTop(HSSFCellStyle.BORDER_THIN);        title_style.setAlignment(HSSFCellStyle.ALIGN_CENTER);        HSSFFont title_font = workbook.createFont();        title_font.setColor(HSSFColor.VIOLET.index);        title_font.setFontHeightInPoints((short) 12);        title_font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);        title_style.setFont(title_font);        //內容的樣式        HSSFCellStyle content_style = workbook.createCellStyle();        content_style.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);        content_style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);        content_style.setBorderBottom(HSSFCellStyle.BORDER_THIN);        content_style.setBorderLeft(HSSFCellStyle.BORDER_THIN);        content_style.setBorderRight(HSSFCellStyle.BORDER_THIN);        content_style.setBorderTop(HSSFCellStyle.BORDER_THIN);        content_style.setAlignment(HSSFCellStyle.ALIGN_CENTER);        content_style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);        HSSFFont content_font = workbook.createFont();        content_font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);        content_style.setFont(content_font);        //填充標題內容        HSSFRow row = sheet.createRow(0);        for (int i = 0; i < headers.length; i++) {            //設定標題的寬度自適應            sheet.setColumnWidth(i, headers[i].getBytes().length * 2 * 256);            HSSFCell cell = row.createCell(i);            cell.setCellStyle(title_style);            HSSFRichTextString text = new HSSFRichTextString(headers[i]);            cell.setCellValue(text);        }        //填充內容 囧。。。偷懶沒有建立對象,直接用List存放的資料。        for (int i = 0; i < diaochas.size(); i++) {            row = sheet.createRow(i + 1);            List<String> diaocha = diaochas.get(i);            for (int j = 0; j < diaocha.size(); j++) {                HSSFCell cell = row.createCell(j);                cell.setCellStyle(content_style);                HSSFRichTextString richString = new HSSFRichTextString(                        diaocha.get(j));                cell.setCellValue(richString);            }        }        //這裡調用reset()因為我在別的代碼中調用了response.getWriter();        response.reset();        response.setContentType("application/vnd.ms-excel");        response.setHeader("Content-disposition", "attachment;filename="                + queryDate + ".xls");        OutputStream ouputStream = response.getOutputStream();        workbook.write(ouputStream);        ouputStream.flush();        ouputStream.close();    }

嗯 附加一個mysql函數擷取年月的

date_format(formatdate,‘%Y-%m‘)

最後提示一點是,在請求匯出的時候,不能用ajax請求,會沒有反映的。

java poi 匯出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.