java中的export方法實(shí)現(xiàn)導(dǎo)出excel文件
1.export函數(shù)
//導(dǎo)出文件接口
public String export(){
return this.myExport(exportList);
}2.導(dǎo)出列名
private String myExport(List<BusinessDept> list){
com.bronzesoft.power.tools.json.JSONObject info = new com.bronzesoft.power.tools.json.JSONObject();
try{
List<String> headList = new ArrayList<String>(Arrays.asList("年", "月", "部門","部門負(fù)責(zé)人","經(jīng)營值","收入", "支出","填報(bào)工時(shí)","標(biāo)準(zhǔn)工時(shí)","經(jīng)營參數(shù)"));
Commonutil.export(list,headList,"部門經(jīng)營總覽導(dǎo)出");
info = Commonutil.setInfo(info,"部門經(jīng)營總覽導(dǎo)出");
}catch (Exception e){
LogUtil.error(this.getClass().getName()+".exportExcel()", e);
}
return info.toString();
}3.export實(shí)現(xiàn)方法
?首先,理解一下一個(gè)Excel的文件的組織形式,一個(gè)Excel文件對(duì)應(yīng)于一個(gè)workbook(XSSFWorkbook),一個(gè)workbook可以有多個(gè)sheet(XSSFSheet)組成,一個(gè)sheet是由多個(gè)row(XSSFRow)組成,一個(gè)row是由多個(gè)cell(XSSFCell)組成。?

public static <T> boolean export(List<T> list,List<String> headList,String fileName){
return myExport(list,headList,fileName,com.bronzesoft.rdm.platform.util.Constants.PATH + com.bronzesoft.power.platform.Constants.TEMPFOLDER_DIR );
}
//將list導(dǎo)出為excel,文件名為fileName
public static <T> boolean myExport(List<T> list,List<String> headList,String fileName,String path){
try{
if(!checkListAndHead(list,headList)){
LogUtil.info( "head的長度有問題,導(dǎo)出的文件不正確" );
}
File file = new File(path + File.separator + fileName + ".xlsx");
if(!file.exists()) {
file.createNewFile();
}
//創(chuàng)建對(duì)應(yīng)excel文件,存儲(chǔ)路徑path待確定
XSSFWorkbook workBook = new XSSFWorkbook();
//創(chuàng)建一個(gè)excel的sheet頁
XSSFSheet sheet = workBook.createSheet(fileName);
XSSFRow row = null;
XSSFCell cell = null;
Map<String, CellStyle> styles = StyleUtil.createStyles(workBook);
//表頭樣式
CellStyle headCenterIndex = styles.get("headCenter");
//列的樣式居中,背景顏色為白色
CellStyle center = styles.get("centerWhite");
row = sheet.createRow(0);
//表頭數(shù)據(jù)
for (int i = 0; i < headList.size(); i++) {
//創(chuàng)建列
cell = row.createCell(i);
//設(shè)置列的value
cell.setCellValue(headList.get(i));
//設(shè)置列的樣式
cell.setCellStyle(headCenterIndex);
}
//表格數(shù)據(jù)
//寫入表格數(shù)據(jù)
String codeName = "";
int rownum = 1;
T obj = null;
for(int i = 0; i < list.size(); i++){
obj = list.get(i);
if(null != obj){
Field[] fields = obj.getClass().getDeclaredFields();
row = sheet.createRow(rownum);
for(int j = 0; j< fields.length; j++){
fields[j].setAccessible(true);
codeName = String.valueOf(fields[j].get(obj));
//創(chuàng)建第j列
cell = row.createCell(j);
cell.setCellValue(Commonutil.getStringVal(codeName));
cell.setCellStyle(center);
}
}
rownum++;
}
// //i代表列,設(shè)置列的寬度
// for (int i = 0; i < headList.size(); i++) {
// if(i == 0){
// sheet.setColumnWidth(i, 2000);
// }else if(i == 1 || i == 3){
// sheet.setColumnWidth(i, 6000);
// }else{
// sheet.setColumnWidth(i, 4000);
// }
// }
//將文件寫到臨時(shí)目錄
FileOutputStream out = new FileOutputStream(file);
workBook.write(out);
}catch (Exception e){
LogUtil.error(fileName + "export失敗" );
}
return true;
}
//檢查head的size是否符合規(guī)范
public static <T> boolean checkListAndHead(List<T> list,List<String> headList){
if(list.size()>0){
T t = list.get(0);
if(getColumnCount(t) != headList.size()){
LogUtil.info( "head的長度有問題" );
return false;
}
}
return true;
}
//獲取一個(gè)對(duì)象成員變量的個(gè)數(shù)
public static <T> int getColumnCount(T t){
Field[] fields = t.getClass().getDeclaredFields();
int count = fields.length;
return count;
}4.前端對(duì)接
public static JSONObject setInfo(JSONObject info,String fileName) throws Exception {
Storage s = Commonutil.getDefaultStorage();
info.put("port", String.valueOf(s.getPort()));
info.put("dirAddress", Base64Util.encode(com.bronzesoft.rdm.platform.util.Constants.PATH));
info.put("address", Base64Util.encode(com.bronzesoft.power.platform.Constants.TEMPFOLDER_DIR + File.separator + fileName + ".xlsx"));
info.put("name", Base64Util.encode(fileName));
info.put("extendName", "xlsx");
return info;
}5.前端代碼
function doExport(){
synAjax.tabCall("com.bronzesoft.rdm.SeDeptTotalTab", "export", null, function(data){
doDownload(data);
});
}
function doDownload(data){
console.log('data',data)
r = eval('('+data+')');
$("#_file_dir").val(r.dirAddress);
$("#_file_address").val(r.address);
$("#_file_name").val(r.name);
$("#_file_extname").val(r.extendName);
if ($.browser.safari) { $("#_file_downform").attr("target", ""); }
var servlet = powerPath + "download";
$("#_file_downform").attr("action", servlet).submit();
}到此這篇關(guān)于java中的export方法實(shí)現(xiàn)導(dǎo)出excel文件的文章就介紹到這了,更多相關(guān)java導(dǎo)出excel文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Jmeter中的timeshift()函數(shù)獲取當(dāng)前時(shí)間進(jìn)行加減
這篇文章主要介紹了Jmeter中的timeshift()函數(shù)獲取當(dāng)前時(shí)間進(jìn)行加減,TimeShift(格式,日期,移位,語言環(huán)境,變量)可對(duì)日期進(jìn)行移位加減操作,本文給大家詳細(xì)講解,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-10-10
Mybatis自定義TypeHandler解決特殊類型轉(zhuǎn)換問題詳解
這篇文章主要介紹了Mybatis自定義TypeHandler解決特殊類型轉(zhuǎn)換問題詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
SpringBoot和Vue2項(xiàng)目配置https協(xié)議過程
本文詳細(xì)介紹了SpringBoot項(xiàng)目和Vue2項(xiàng)目的部署流程及SSL證書配置,對(duì)于SpringBoot項(xiàng)目,需將.pfx文件放入resources目錄并配置server,然后打包部署,Vue2項(xiàng)目中,涉及檢查nginx的SSL模塊、編譯新的nginx文件2024-10-10
Java動(dòng)態(tài)獲取實(shí)現(xiàn)類的方式詳解
這篇文章主要介紹了Java動(dòng)態(tài)獲取實(shí)現(xiàn)類的方式詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2024-01-01
Hikari?數(shù)據(jù)庫連接池內(nèi)部源碼實(shí)現(xiàn)的小細(xì)節(jié)
這篇文章主要介紹了Hikari?數(shù)據(jù)庫連接池內(nèi)部源碼實(shí)現(xiàn)的小細(xì)節(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
啟動(dòng)Tomcat時(shí)出現(xiàn)大量亂碼的解決方法
今天給大家?guī)淼氖顷P(guān)于Java的相關(guān)知識(shí),文章圍繞著啟動(dòng)Tomcat時(shí)出現(xiàn)大量亂碼的解決方法展開,文中有非常詳細(xì)的介紹及圖文示例,需要的朋友可以參考下2021-06-06
Spring?Boot中@Import三種使用方式實(shí)例詳解
這篇文章主要介紹了Spring?Boot中@Import三種使用方式,主要有引入普通類,引入importSelector的實(shí)現(xiàn)類及引入importBeanDefinitionRegister的實(shí)現(xiàn)類,結(jié)合實(shí)例代碼給大家講解的非常詳細(xì),需要的朋友可以參考下2022-11-11

