Java中excel文件怎么使用apache poi進(jìn)行生成?很多新手對此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
在網(wǎng)站建設(shè)、成都網(wǎng)站制作中從網(wǎng)站色彩、結(jié)構(gòu)布局、欄目設(shè)置、關(guān)鍵詞群組等細(xì)微處著手,突出企業(yè)的產(chǎn)品/服務(wù)/品牌,幫助企業(yè)鎖定精準(zhǔn)用戶,提高在線咨詢和轉(zhuǎn)化,使成都網(wǎng)站營銷成為有效果、有回報(bào)的無錫營銷推廣。創(chuàng)新互聯(lián)建站專業(yè)成都網(wǎng)站建設(shè)10年了,客戶滿意度97.8%,歡迎成都創(chuàng)新互聯(lián)客戶聯(lián)系。
首先,jar
maven 添加依賴
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.15</version> </dependency>
開始以為是poi,然后就直接加poi的依賴,誰知道并沒有所需要的類。查了查才發(fā)現(xiàn)是poi-ooxml
要用到的類
最最基本的使用
//創(chuàng)建excel文檔 XSSFWorkbook workbook = new XSSFWorkbook(); //創(chuàng)建sheet XSSFSheet sheet = workbook.createSheet("sheetName"); int rownum=0; //創(chuàng)建首行 XSSFRow firstrow = sheet.createRow(rownum++); int cellnum = 0; //把保存在titles中的各個(gè)列名,分別在row中創(chuàng)建cell for(String key : titles){ XSSFCell cell = firstrow.createCell(cellnum++); cell.setCellValue(key); } //下面可以繼續(xù)創(chuàng)建行 //把excel寫到要寫的outputStream中 workbook.write(output); //最后關(guān)閉 workbook.close();
小例子一枚
利用反射,把bean類中各屬性(用getXxx取出),寫入到excel中
ExcelUtil.java
package me.paul.excelDemo; import java.io.IOException; import java.io.OutputStream; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExcelUtil { public <T> void getExcel(List<T> list,Class<T> c,OutputStream output) throws IOException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{ Map<String,Method> methodMap = new LinkedHashMap<>(); Method[] methods = c.getDeclaredMethods(); for(int i=0;i<methods.length;i++){ Method method = methods[i]; String name = method.getName(); Pattern pattern = Pattern.compile("get(.*)"); Matcher matcher = null; if((matcher = pattern.matcher(name)).matches()){ name = matcher.group(1); char ch = name.charAt(0); char newch = (char) (ch + 32); name = name.replace(ch,newch); methodMap.put(name, method); } } XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet(c.getCanonicalName()); int rownum=0; XSSFRow firstrow = sheet.createRow(rownum++); int cellnum = 0; for(String key : methodMap.keySet()){ XSSFCell cell = firstrow.createCell(cellnum++); cell.setCellValue(key); } for(T t : list){ XSSFRow row = sheet.createRow(rownum++); cellnum = 0; for(String key:methodMap.keySet()){ Method method = methodMap.get(key); //設(shè)置可訪問,之前不知道這方法,所以關(guān)于反射那篇文章有錯(cuò)誤,見諒見諒 method.setAccessible(true); Object obj = method.invoke(t); XSSFCell cell = row.createCell(cellnum++); cell.setCellValue(obj== null ? "":obj.toString()); } } workbook.write(output); workbook.close(); } }
App.java 進(jìn)行測試使用
package me.paul.excelDemo; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.List; public class App { public static void main(String[] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, IOException { List<User> list = new ArrayList<>(); User u = new User(); u.setId(1); u.setName("Paul"); u.setAge(18); list.add(u); u = new User(); u.setId(2); u.setName("Johnson"); u.setAge(20); list.add(u); u = new User(); u.setId(3); u.setName("David"); u.setAge(22); list.add(u); OutputStream output = new FileOutputStream("/home/paul/user.xlsx"); new ExcelUtil().getExcel(list, User.class,output); output.close(); } }
測試結(jié)果截圖
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。
網(wǎng)頁題目:Java中excel文件怎么使用apachepoi進(jìn)行生成
文章出自:http://www.rwnh.cn/article22/pgcpcc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、網(wǎng)站設(shè)計(jì)公司、網(wǎng)站制作、網(wǎng)站建設(shè)、電子商務(wù)、網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)