您好,登錄后才能下訂單哦!
import java.io.IOException; import java.io.OutputStream; import java.lang.reflect.Field; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang3.StringUtils; import org.joda.time.DateTime; import com.travelzen.framework.net.http.TZHttpClient; import com.travelzen.tops.front.ota.member.item.CustomerItem; public class CSV { /** * 目標(biāo)輸出流 */ private OutputStream stream; /** * 表頭 */ private Map<String,String> fields; /** * 數(shù)據(jù)源model所有字段map */ private static Map<String, Field> fieldMap = new HashMap<>(); public CSV(HttpServletResponse response,Map<String,String> fields,String fileName,Class<?> clz) throws IOException{ if(response == null || fields == null || fileName == null || clz == null) throw new IllegalArgumentException(); getFieldMap(clz,fieldMap); this.stream = response.getOutputStream(); this.fields = fields; response.setContentType("application/octet-stream;charset=GBK"); response.setHeader("Content-Disposition", "attachment;fileName="+ fileName); //寫(xiě)表頭,生成指定名字的文件,返回客戶端 StringBuilder hb = new StringBuilder(); for(Entry<String, String> e : fields.entrySet()) hb.append(e.getValue()+","); stream.write(hb.substring(0, hb.length() - 1).getBytes("GBK")); stream.flush(); } /** * 往表格中插入記錄 */ public void write(List<Object> data) throws IllegalArgumentException, IllegalAccessException, IOException{ for(Object o : data){ StringBuilder sb = new StringBuilder(); sb.append("\n"); for(String field : fields.keySet()){ Field f = fieldMap.get(field); f.setAccessible(true); Object value = f.get(o); if(value == null || StringUtils.isBlank(value.toString())){ sb.append(" ,"); } else if (f.getType() == Date.class) { sb.append(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(value) + ","); } else if (f.getType() == DateTime.class) { sb.append(((DateTime)value).toString("yyyy-MM-dd HH:mm:ss") + ","); } else { String tmp = value.toString(); if(tmp.contains(",")) tmp = tmp.replace(",", "\",\""); sb.append(value.toString() + ","); } } stream.write(sb.substring(0, sb.length() - 1).getBytes("GBK")); stream.flush(); } } public void close() throws IOException{ stream.close(); } private static <T extends Object> void getFieldMap(Class<T> clz, Map<String, Field> result) { for (Field field : clz.getDeclaredFields()) { result.put(field.getName(), field); } if (clz.getSuperclass() != null) { getFieldMap(clz.getSuperclass(), result); } } }
web開(kāi)發(fā)中常見(jiàn)的準(zhǔn)備Excel數(shù)據(jù)需要從數(shù)據(jù)庫(kù)查詢(xún)數(shù)據(jù),或者跨系統(tǒng)調(diào)用接口查詢(xún)數(shù)據(jù),耗費(fèi)大量時(shí)間,因此未及時(shí)向?yàn)g覽器返回?cái)?shù)據(jù),導(dǎo)致504超時(shí)。
本工具使用ServletOutputStream分段的往瀏覽器flush數(shù)據(jù)。調(diào)用方式:先new CSV(),傳入指定參數(shù),不斷的調(diào)用wirte()方法往瀏覽器寫(xiě)入數(shù)據(jù),最后調(diào)用close方法關(guān)閉流。
本工具導(dǎo)出的文件格式為.csv文件,windows office工具默認(rèn)編碼為ASCI,wps會(huì)匹配各種編碼,libreOffice calc可以指定編碼,故此設(shè)置編碼為GBK,兼容三種Excel軟件,也可根據(jù)自身需求設(shè)置編碼。
本工具只處理了CSV中”,”的轉(zhuǎn)碼,對(duì)于雙引號(hào)并未處理。
希望本文能夠?qū)τ龅酱藛?wèn)題的朋友能有所幫助
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。