這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)利用java怎么從數(shù)據(jù)庫中導(dǎo)出Excel表格,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
創(chuàng)新互聯(lián)服務(wù)項(xiàng)目包括通榆網(wǎng)站建設(shè)、通榆網(wǎng)站制作、通榆網(wǎng)頁制作以及通榆網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,通榆網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到通榆省份的部分城市,未來相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
1、基于maven jar包引入如下:
net.sourceforge.jexcelapi jxl 2.6.12
2、首先創(chuàng)建數(shù)據(jù)庫對(duì)應(yīng)的實(shí)體類VO :UserVO(具體代碼省略);
3、確定導(dǎo)出Excel內(nèi)的title列,并放在數(shù)組里:String[] (具體代碼省略);
4、編寫導(dǎo)出Excel的方法:
傳入?yún)?shù):
Excel名稱,Excel內(nèi)的title列數(shù)組String[],數(shù)據(jù)集合List
package bp.util; import java.io.OutputStream; import java.util.List; import javax.servlet.http.HttpServletResponse; 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; public class ExportExcel { public final static String exportExcel(String fileName, String[] Title, List listContent, HttpServletResponse response) { String result = "Excel文件導(dǎo)出成功!"; try { OutputStream os = response.getOutputStream(); response.reset(); response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes("GB2312"), "ISO8859-1")); response.setContentType("application/msexcel"); WritableWorkbook workbook = Workbook.createWorkbook(os); WritableSheet sheet = workbook.createSheet("Sheet1", 0); jxl.SheetSettings sheetset = sheet.getSettings(); sheetset.setProtected(false); WritableFont BoldFont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD); WritableCellFormat wcf_center = new WritableCellFormat(BoldFont); wcf_center.setBorder(Border.ALL, BorderLineStyle.THIN); wcf_center.setVerticalAlignment(VerticalAlignment.CENTRE); wcf_center.setAlignment(Alignment.CENTRE); wcf_center.setWrap(true); for (int i = 0; i < Title.length; i++) { sheet.setColumnView(i, 20); sheet.addCell(new Label(i, 0, Title[i], wcf_center)); } 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_center)); j++; } i++; } workbook.write(); workbook.close(); } catch (Exception e) { result = "Excel文件導(dǎo)出失敗"; e.printStackTrace(); } return result; } }
上述就是小編為大家分享的利用java怎么從數(shù)據(jù)庫中導(dǎo)出Excel表格了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。