java導(dǎo)出Excel通用方法的實(shí)例詳解
java導(dǎo)出Excel通用方法的實(shí)例詳解
Java導(dǎo)出Excel通用方法,只需要一個(gè)list 集合。通用方法改進(jìn)之處踴躍提出
package oa.common.utils; import java.io.OutputStream; import java.util.List; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import java.lang.reflect.Field; import jxl.Workbook; import jxl.format.Alignment; import jxl.format.Border; import jxl.format.BorderLineStyle; import jxl.format.VerticalAlignment; import jxl.write.Label; import jxl.write.WritableCellFormat; import jxl.write.WritableFont; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; /*** * @author lsf */ public class ExportExcel { /*************************************************************************** * @param fileName EXCEL文件名稱 * @param listTitle EXCEL文件第一行列標(biāo)題集合 * @param listContent EXCEL文件正文數(shù)據(jù)集合 * @return */ public final static String exportExcel(String fileName,String[] Title, List<Object> listContent) { String result="系統(tǒng)提示:Excel文件導(dǎo)出成功!"; // 以下開始輸出到EXCEL try { //定義輸出流,以便打開保存對(duì)話框______________________begin HttpServletResponse response=ServletActionContext.getResponse(); OutputStream os = response.getOutputStream();// 取得輸出流 response.reset();// 清空輸出流 response.setHeader("Content-disposition", "attachment; filename="+ new String(fileName.getBytes("GB2312"),"ISO8859-1")); // 設(shè)定輸出文件頭 response.setContentType("application/msexcel");// 定義輸出類型 //定義輸出流,以便打開保存對(duì)話框_______________________end /** **********創(chuàng)建工作簿************ */ WritableWorkbook workbook = Workbook.createWorkbook(os); /** **********創(chuàng)建工作表************ */ WritableSheet sheet = workbook.createSheet("Sheet1", 0); /** **********設(shè)置縱橫打?。J(rèn)為縱打)、打印紙***************** */ jxl.SheetSettings sheetset = sheet.getSettings(); sheetset.setProtected(false); /** ************設(shè)置單元格字體************** */ WritableFont NormalFont = new WritableFont(WritableFont.ARIAL, 10); WritableFont BoldFont = new WritableFont(WritableFont.ARIAL, 10,WritableFont.BOLD); /** ************以下設(shè)置三種單元格樣式,靈活備用************ */ // 用于標(biāo)題居中 WritableCellFormat wcf_center = new WritableCellFormat(BoldFont); wcf_center.setBorder(Border.ALL, BorderLineStyle.THIN); // 線條 wcf_center.setVerticalAlignment(VerticalAlignment.CENTRE); // 文字垂直對(duì)齊 wcf_center.setAlignment(Alignment.CENTRE); // 文字水平對(duì)齊 wcf_center.setWrap(false); // 文字是否換行 // 用于正文居左 WritableCellFormat wcf_left = new WritableCellFormat(NormalFont); wcf_left.setBorder(Border.NONE, BorderLineStyle.THIN); // 線條 wcf_left.setVerticalAlignment(VerticalAlignment.CENTRE); // 文字垂直對(duì)齊 wcf_left.setAlignment(Alignment.LEFT); // 文字水平對(duì)齊 wcf_left.setWrap(false); // 文字是否換行 /** ***************以下是EXCEL開頭大標(biāo)題,暫時(shí)省略********************* */ //sheet.mergeCells(0, 0, colWidth, 0); //sheet.addCell(new Label(0, 0, "XX報(bào)表", wcf_center)); /** ***************以下是EXCEL第一行列標(biāo)題********************* */ for (int i = 0; i < Title.length; i++) { sheet.addCell(new Label(i, 0,Title[i],wcf_center)); } /** ***************以下是EXCEL正文數(shù)據(jù)********************* */ Field[] fields=null; int i=1; for(Object obj:listContent){ fields=obj.getClass().getDeclaredFields(); int j=0; for(Field v:fields){ v.setAccessible(true); Object va=v.get(obj); if(va==null){ va=""; } sheet.addCell(new Label(j, i,va.toString(),wcf_left)); j++; } i++; } /** **********將以上緩存中的內(nèi)容寫到EXCEL文件中******** */ workbook.write(); /** *********關(guān)閉文件************* */ workbook.close(); } catch (Exception e) { result="系統(tǒng)提示:Excel文件導(dǎo)出失敗,原因:"+ e.toString(); System.out.println(result); e.printStackTrace(); } return result; } }
測(cè)試:
/** * 導(dǎo)出excel * @return */ public String excelPage(){ ExportExcel excel=new ExportExcel(); String str=""; try { str = new String(getHTTP.getRequest().getParameter("wineOrg.orgName").getBytes("iso8859-1"),"UTF-8"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } wineOrg.setOrgName(str); List<Object> li=service.exportExcel(wineOrg); String[] Title={"機(jī)構(gòu)ID","會(huì)員編號(hào)","類別","名稱","省ID","省名稱","城市ID","城市名稱","詳細(xì)地址","聯(lián)系人","性別","聯(lián)系手機(jī)","聯(lián)系電話","傳真","郵箱","QQ","生日","積分","客戶等級(jí)","現(xiàn)金賬戶余額","結(jié)算方式","客戶類型","購(gòu)買次數(shù)","購(gòu)買支數(shù)","創(chuàng)建人ID","創(chuàng)建人姓名","create_time","del","STS","備注","負(fù)責(zé)人ID","負(fù)責(zé)人姓名","審核標(biāo)識(shí)","審核人ID ","審核人姓名","審核日期","分配人ID","分配人姓名","分配日期","修改人ID","修改人姓名 ","修改時(shí)間"}; excel.exportExcel("客戶資料信息.xls",Title, li); return SUCCESS; }
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
Java數(shù)據(jù)導(dǎo)出功能之導(dǎo)出Excel文件實(shí)例
這篇文章主要介紹了Java數(shù)據(jù)導(dǎo)出功能之導(dǎo)出Excel文件實(shí)例,本文給出了jar包的下載地址,并給出了導(dǎo)出Excel文件代碼實(shí)例,需要的朋友可以參考下2015-06-06Java中synchronized關(guān)鍵字的使用和原理詳解
這篇文章主要介紹了Java中synchronized關(guān)鍵字的使用和原理詳解,JVM 是通過進(jìn)入、退出 對(duì)象監(jiān)視器(Monitor)來實(shí)現(xiàn)對(duì)方法、同步塊的同步的,而對(duì)象監(jiān)視器的本質(zhì)依賴于底層操作系統(tǒng)的互斥鎖實(shí)現(xiàn),需要的朋友可以參考下2023-09-09JAVA過濾標(biāo)簽實(shí)現(xiàn)將html內(nèi)容轉(zhuǎn)換為文本的方法示例
這篇文章主要介紹了JAVA過濾標(biāo)簽實(shí)現(xiàn)將html內(nèi)容轉(zhuǎn)換為文本的方法,涉及java針對(duì)HTML代碼的正則替換相關(guān)操作技巧,需要的朋友可以參考下2017-07-07LRU算法及Apache?LRUMap源碼實(shí)例解析
這篇文章主要給大家介紹了關(guān)于LRU算法及Apache?LRUMap源碼解析的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2021-11-11基于Spring Boot保護(hù)Web應(yīng)用程序
這篇文章主要介紹了基于Spring Boot保護(hù)Web應(yīng)用程序,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03SpringBoot無法請(qǐng)求html等靜態(tài)資源文件webapp或者resources/static的問題及解決方案
今天遇到一個(gè)問題無法訪問靜態(tài)資源文件,html,本文給大家分享SpringBoot無法請(qǐng)求html等靜態(tài)資源文件webapp或者resources/static的問題及解決方案,感興趣的朋友一起看看吧2024-05-05超詳細(xì)的Spring Boot入門筆記(總結(jié))
本篇文章主要介紹了超詳細(xì)的Spring Boot入門筆記(總結(jié)),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11