jxl 導(dǎo)出數(shù)據(jù)到excel的實(shí)例講解
優(yōu)點(diǎn):
Jxl對(duì)中文支持非常好,操作簡(jiǎn)單,方法看名知意。
Jxl是純javaAPI,在跨平臺(tái)上表現(xiàn)的非常完美,代碼可以再windows或者Linux上運(yùn)行而無(wú)需重新編寫
支持Excel 95-2000的所有版本(網(wǎng)上說(shuō)目前可以支持Excel2007了,還沒(méi)有嘗試過(guò))
生成Excel 2000標(biāo)準(zhǔn)格式
支持字體、數(shù)字、日期操作
能夠修飾單元格屬性
支持圖像和圖表,但是這套API對(duì)圖形和圖表的支持很有限,而且僅僅識(shí)別PNG格式。
缺點(diǎn):
效率低,圖片支持不完善,對(duì)格式的支持不如POI強(qiáng)大
案例:
String times = (new SimpleDateFormat("yyyyMMddHHmmss")).format(new Date()); String fname = "系統(tǒng)日志" + times; // 文件名 List<Logs> list=logsService.selectForList(hql.toString()); String path = request.getSession().getServletContext().getRealPath("/") + "xls/" + (new SimpleDateFormat("yyyyMMdd")).format(new Date()); File file = new File(path); // 如果文件夾不存在則創(chuàng)建 if (!file.exists() && !file.isDirectory()) { file.mkdir(); } response.setContentType("application/vnd.ms-excel;charset=utf-8");// // 指定文件的保存類型。 response.setCharacterEncoding("utf-8"); ExportUtil.writer_log(request,fname, list, response);//下載到本地
writer_log導(dǎo)出方法如下
/** * 生成 excel 文件,導(dǎo)出到本地電腦 * @param fname 文件名 * @param list 需要打印的數(shù)據(jù),即數(shù)據(jù)庫(kù)查詢的數(shù)據(jù)列表 */ public static void writer_log(HttpServletRequest request,String fname, List list, HttpServletResponse response) { try { OutputStream os = response.getOutputStream();//取得輸出流 response.reset();//清空輸出流 // 下面是對(duì)中文文件名的處理 開(kāi)始 response.setCharacterEncoding("UTF-8");//設(shè)置相應(yīng)內(nèi)容的編碼格式 if(isMsBrowser(request)) fname= java.net.URLEncoder.encode(fname ,"UTF-8"); else fname = new String(fname.getBytes("UTF-8"),"ISO-8859-1"); response.setHeader("Content-Disposition","attachment;filename="+fname+".xls"); response.setContentType("application/msexcel;charset=utf-8");//定義輸出類型 // 對(duì)中文文件名的處理 結(jié)束 // 此處的 Workbook 導(dǎo)入的是 import jxl.Workbook; WritableWorkbook wbook = Workbook.createWorkbook(os); // 建立excel文件 WritableSheet sheet = wbook.createSheet("系統(tǒng)日志", 0); // 工作表名稱 CellView cellView = new CellView(); cellView.setAutosize(true); //設(shè)置自動(dòng)大小 sheet.setColumnView(0, 8); //設(shè)置單元格寬度,0是列號(hào),8是寬度 sheet.setColumnView(1, 20); //設(shè)置單元格寬度,1是列號(hào),20是寬度 sheet.setColumnView(2, 24); sheet.setColumnView(3, 20); sheet.setColumnView(4, 30); sheet.setColumnView(5, 13); sheet.setColumnView(6, 15); sheet.setColumnView(7, 32); sheet.setColumnView(8, 15); // 設(shè)置Excel字體 WritableFont wfont = new WritableFont(WritableFont.createFont("宋體"), 22, WritableFont.BOLD, false, jxl.format.UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK); //設(shè)置單元格字體樣式 WritableCellFormat titleFormat = new WritableCellFormat(wfont); //添加單元格字體 titleFormat.setAlignment(Alignment.CENTRE); //設(shè)置文字居中對(duì)齊方式; String[] title = { "系統(tǒng)日志" }; // 設(shè)置Excel表頭 開(kāi)始 for (int i = 0; i < title.length; i++) { // 此處導(dǎo)入的是 import jxl.write.Label; Label excelTitle = new Label(i, 0, title[i], titleFormat); //單元格內(nèi)容 // 參數(shù)順序:開(kāi)始列,開(kāi)始行,結(jié)束列,結(jié)束行 sheet.mergeCells(0, 0, 8, 0); //所在位置,第幾行第幾列 sheet.addCell(excelTitle); //添加單元格信息 } // 設(shè)置Excel表頭 結(jié)束 // 第一行,即顯示時(shí)間,參數(shù):(所在列,所在行,內(nèi)容) WritableFont wfonttime = new WritableFont(WritableFont.createFont("宋體"), 11, WritableFont.NO_BOLD, false, jxl.format.UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK); //設(shè)置單元格字體樣式 WritableCellFormat titletime = new WritableCellFormat(wfonttime);//添加單元格字體 titletime.setAlignment(Alignment.RIGHT); //設(shè)置文字居中對(duì)齊方式; DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); // sheet.setColumnView(1, cellView); //根據(jù)內(nèi)容自動(dòng)設(shè)置列寬 Label contentDate = new Label(0, 1, df.format(new Date()), titletime); //單元格內(nèi)容 // sheet.mergeCells(16, 1, 18, 1); //所在位置,第幾行第幾列,即合并的位置,如沒(méi)合并,可不寫 sheet.addCell(contentDate); //添加單元格信息 // 第一行 結(jié)束 // 第二行,顯示條件標(biāo)題欄 WritableFont wfont2 = new WritableFont(WritableFont.createFont("宋體"), 11, WritableFont.BOLD, false, jxl.format.UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK); //設(shè)置單元格字體樣式 WritableCellFormat titleFormat2 = new WritableCellFormat(wfont2);//添加單元格字體 titleFormat2.setBorder(Border.ALL, BorderLineStyle.THIN); //設(shè)置邊框--實(shí)線; titleFormat2.setAlignment(Alignment.CENTRE); //設(shè)置文字居中對(duì)齊方式; titleFormat2.setVerticalAlignment(VerticalAlignment.CENTRE); //設(shè)置垂直居中; Label content2a = new Label(0, 2, "序號(hào)", titleFormat2); //單元格內(nèi)容--第1格 Label content2b = new Label(1, 2, "用戶名", titleFormat2); //單元格內(nèi)容--第2格 Label content2c = new Label(2, 2, "記錄時(shí)間", titleFormat2); //單元格內(nèi)容--第3格 Label content2d = new Label(3, 2, "操作模塊", titleFormat2); //單元格內(nèi)容--第4格 Label content2e = new Label(4, 2, "操作內(nèi)容", titleFormat2); //單元格內(nèi)容--第5格 Label content2f = new Label(5, 2, "操作動(dòng)作", titleFormat2); //單元格內(nèi)容--第6格 Label content2g = new Label(6, 2, "操作人IP", titleFormat2); //單元格內(nèi)容--第7格 Label content2h = new Label(7, 2, "所屬組織", titleFormat2); //單元格內(nèi)容--第8格 Label content2i = new Label(8, 2, "備注", titleFormat2); //單元格內(nèi)容--第9格 sheet.mergeCells(0, 1, 8, 1); /*sheet.mergeCells(0, 2, 0, 4); //所在位置,第幾行第幾列,即合并的位置,如沒(méi)合并,可不寫;(此處為第3行開(kāi)始到5行,第1列) sheet.mergeCells(1, 2, 1, 4); //所在位置,第幾行第幾列,即合并的位置,如沒(méi)合并,可不寫;(此處為第3行開(kāi)始到5行,第2列) sheet.mergeCells(2, 2, 2, 4); //所在位置,第幾行第幾列,即合并的位置,如沒(méi)合并,可不寫;(此處為第3行開(kāi)始到5行,第3列) sheet.mergeCells(3, 2, 3, 4); //所在位置,第幾行第幾列,即合并的位置,如沒(méi)合并,可不寫;(此處為第3行開(kāi)始到5行,第4列) sheet.mergeCells(4, 2, 4, 4); //所在位置,第幾行第幾列,即合并的位置,如沒(méi)合并,可不寫;(此處為第3行開(kāi)始到5行,第5列) sheet.mergeCells(5, 2, 5, 4); //所在位置,第幾行第幾列,即合并的位置,如沒(méi)合并,可不寫;(此處為第3行開(kāi)始到5行,第6列) sheet.mergeCells(6, 2, 6, 4); //所在位置,第幾行第幾列,即合并的位置,如沒(méi)合并,可不寫;(此處為第3行開(kāi)始到5行,第7列) sheet.mergeCells(7, 2, 7, 4); //所在位置,第幾行第幾列,即合并的位置,如沒(méi)合并,可不寫;(此處為第3行開(kāi)始到5行,第8列) sheet.mergeCells(8, 2, 8, 4); //所在位置,第幾行第幾列,即合并的位置,如沒(méi)合并,可不寫;(此處為第3行開(kāi)始到5行,第9列) sheet.mergeCells(9, 2, 9, 4); //所在位置,第幾行第幾列,即合并的位置,如沒(méi)合并,可不寫;(此處為第3行開(kāi)始到5行,第10列) sheet.mergeCells(10, 2, 12, 3); //所在位置,第幾行第幾列,即合并的位置,如沒(méi)合并,可不寫;(此處為第3行開(kāi)始到4行,第11-13列) sheet.mergeCells(13, 2, 15, 3); //所在位置,第幾行第幾列,即合并的位置,如沒(méi)合并,可不寫;(此處為第3行開(kāi)始到4行,第14-16列) sheet.mergeCells(16, 2, 16, 4); //所在位置,第幾行第幾列,即合并的位置,如沒(méi)合并,可不寫;(此處為第3行開(kāi)始到5行,第17列) sheet.mergeCells(17, 2, 17, 4); //所在位置,第幾行第幾列,即合并的位置,如沒(méi)合并,可不寫;(此處為第3行開(kāi)始到5行,第18列) sheet.mergeCells(18, 2, 18, 4); //所在位置,第幾行第幾列,即合并的位置,如沒(méi)合并,可不寫;(此處為第3行開(kāi)始到5行,第19列) */ sheet.addCell(content2a); //添加第1格單元格信息 sheet.addCell(content2b); //添加第2格單元格信息 sheet.addCell(content2c); //添加第3格單元格信息 sheet.addCell(content2d); //添加第4格單元格信息 sheet.addCell(content2e); //添加第5格單元格信息 sheet.addCell(content2f); //添加第6格單元格信息 sheet.addCell(content2g); //添加第7格單元格信息 sheet.addCell(content2h); //添加第8格單元格信息 sheet.addCell(content2i); //添加第9格單元格信息 WritableFont wf = new WritableFont(WritableFont.createFont("宋體"), 11, WritableFont.NO_BOLD, false, jxl.format.UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK); //設(shè)置單元格字體樣式 WritableCellFormat wcf = new WritableCellFormat(wf); //添加單元格字體 wcf.setBorder(Border.ALL, BorderLineStyle.THIN); //設(shè)置邊框--實(shí)線; wcf.setVerticalAlignment(VerticalAlignment.CENTRE); //設(shè)置垂直對(duì)齊 wcf.setAlignment(Alignment.CENTRE); //設(shè)置文字水平居中對(duì)齊方式; wcf.setWrap(true); //自動(dòng)換行 WritableFont wf1 = new WritableFont(WritableFont.createFont("宋體"), 11, WritableFont.NO_BOLD, false, jxl.format.UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK); //設(shè)置單元格字體樣式 WritableCellFormat wcf1 = new WritableCellFormat(wf1); //添加單元格字體 wcf1.setBorder(Border.LEFT, BorderLineStyle.THIN); //設(shè)置邊框--實(shí)線; wcf1.setVerticalAlignment(VerticalAlignment.CENTRE); //設(shè)置垂直對(duì)齊 wcf1.setAlignment(Alignment.CENTRE); //設(shè)置文字水平居中對(duì)齊方式; // 以下循環(huán)數(shù)據(jù)庫(kù)獲取的信息 int c = 1; // 用于循環(huán)時(shí)Excel的行號(hào) Iterator it = list.iterator(); while (it.hasNext()) { Logs tc = (Logs) it.next(); DateFormat dfmt = new SimpleDateFormat("yyyy.MM"); String xh = String.valueOf(c); if(xh== null){ xh = ""; } Label content0 = new Label(0, c+2, xh, wcf); //序號(hào) String name = tc.getUserName(); if(name == null){ name = ""; } Label content1 = new Label(1, c+2, name, wcf); //用戶名 String xrPresent = tc.getLogTime(); if(xrPresent == null){ xrPresent = ""; } Label content2 = new Label(2, c+2, xrPresent, wcf); //記錄時(shí)間 String czModel = tc.getModel(); if(czModel == null){ czModel = ""; } Label content3 = new Label(3, c+2, czModel, wcf); //操作模塊 String sex = tc.getContent(); if(sex == null){ sex = ""; } Label content4 = new Label(4, c+2, sex, wcf); //操作內(nèi)容 String birthday = tc.getOperate(); if(birthday == null){ birthday = ""; } Label content5 = new Label(5, c+2, birthday, wcf); //操作動(dòng)作 String nation = tc.getIp(); if(nation == null){ nation = ""; } Label content6 = new Label(6, c+2, nation, wcf); //操作人IP String origin = tc.getOrgName(); if(origin == null){ origin = ""; } Label content7 = new Label(7, c+2, origin, wcf); //所屬組織 String bPlace = tc.getRemark(); if(bPlace == null){ bPlace = ""; } Label content8 = new Label(8, c+2, bPlace, wcf); //備注 String abc=""; Label content9 = new Label(9, c+2, abc, wcf1); //備注 sheet.setRowView(c+2, 600); // 設(shè)置行高 sheet.setRowView(c+2, 600); sheet.setRowView(c+2, 600); sheet.setRowView(c+2, 600); sheet.setRowView(c+2, 600); sheet.setRowView(c+2, 600); sheet.setRowView(c+2, 600); sheet.setRowView(c+2, 600); sheet.setRowView(c+2, 600); sheet.setRowView(c+2, 600); sheet.mergeCells(0, c+2, 0, c+2); // 合并第一列第c+2行到第一列第c+2行的所有單元格 sheet.mergeCells(1, c+2, 0, c+2); //mergeCells(a,b,c,d) 單元格合并函數(shù) sheet.mergeCells(2, c+2, 0, c+2); //a 單元格的列號(hào) sheet.mergeCells(3, c+2, 0, c+2); //b 單元格的行號(hào) sheet.mergeCells(4, c+2, 0, c+2); //c 從單元格[a,b]起,向下合并到c列 sheet.mergeCells(5, c+2, 0, c+2); //d 從單元格[a,b]起,向下合并到d行 sheet.mergeCells(6, c+2, 0, c+2); sheet.mergeCells(7, c+2, 0, c+2); sheet.mergeCells(8, c+2, 0, c+2); sheet.mergeCells(9, c+2, 0, c+2); sheet.addCell(content0); sheet.addCell(content1); sheet.addCell(content2); sheet.addCell(content3); sheet.addCell(content4); sheet.addCell(content5); sheet.addCell(content6); sheet.addCell(content7); sheet.addCell(content8); sheet.addCell(content9); c++; } wbook.write(); // 寫入文件 wbook.close(); os.close(); } catch (Exception e) { throw new PaikeException("導(dǎo)出文件出錯(cuò)"); } }
以上這篇jxl 導(dǎo)出數(shù)據(jù)到excel的實(shí)例講解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot應(yīng)用啟動(dòng)流程源碼解析
這篇文章主要介紹了SpringBoot應(yīng)用啟動(dòng)流程源碼解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04Java案例之隨機(jī)驗(yàn)證碼功能實(shí)現(xiàn)實(shí)例
本篇文章主要介紹了Java案例之隨機(jī)驗(yàn)證碼功能實(shí)現(xiàn)實(shí)例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06java創(chuàng)建jar包并被項(xiàng)目引用步驟詳解
這篇文章主要介紹了java創(chuàng)建jar包并被項(xiàng)目引用步驟詳解,jar包實(shí)現(xiàn)了特定功能的,java字節(jié)碼文件的壓縮包,更多相關(guān)內(nèi)容需要的朋友可以參考一下2022-07-07SpringBoot調(diào)用第三方WebService接口的兩種方法
本文主要介紹了SpringBoot調(diào)用第三方WebService接口的兩種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06