国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

java poi 導出excel

2019-11-15 00:26:49
字體:
來源:轉載
供稿:網友
java poi 導出Excel

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請求,會沒有反映的。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 常德市| 离岛区| 巨鹿县| 黄浦区| 区。| 大渡口区| 勃利县| 武义县| 南城县| 秀山| 江门市| 榕江县| 博客| 沙雅县| 宝坻区| 海伦市| 玛曲县| 花莲县| 崇信县| 剑河县| 三门县| 柏乡县| 和田县| 南京市| 中江县| 云梦县| 白朗县| 蒲城县| 鄂托克旗| 威远县| 五莲县| 大新县| 宾川县| 南江县| 常宁市| 白山市| 六盘水市| 井陉县| 岳普湖县| 托克托县| 德兴市|