csv文件导出数据缺失问题
csv文件导出是以字符串的形式以“,”号默认分割的,如果你的导出数据中有int,long等数值类型,需要将其转化为String类型,如果不将其转化为String,数据可能会缺失 以下为参考代码:
public void archiveMutiLuaLogCSV(ServletOutputStream outputStream, Application app, int errorID, int errorType, DateRange range, AppVersion appVer) { OutputStreamWriter out = new OutputStreamWriter(outputStream,utf8); try { Calendar startDate = genStartDate(range); Calendar stopDate = genStopDate(range); long startTime = stopDate.getTimeInMillis(); int random = 0; int size = 1024; List<LuaErrorLog> luaErrorLogs = analyClient.queryLuaLog(app, errorID, errorType, startTime, random, size, appVer); String[] titles=new String[]{ "发生时间","应用版本","设备型号","操作系统","用户名","文件名","代码行号"}; //首行为标题行 for(String title : titles){ out.write(title); out.write(","); } //写完文件头后换行 out.write(" "); for (LuaErrorLog luaErrorLog : luaErrorLogs) { try { out.write(df.get().format(luaErrorLog.getTimestamp()).toString()); out.write(","); out.write(luaErrorLog.getAppVersion()); out.write(","); out.write(luaErrorLog.getModel()); out.write(","); out.write(luaErrorLog.getOs()); out.write(","); out.write(luaErrorLog.getUserID()); out.write(","); out.write(luaErrorLog.getFileName()); out.write(","); **//为了防止数据缺失需要将int类型转化为String** out.write(String.valueOf(luaErrorLog.getCodeLine())); out.write(" "); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { try { out.flush(); out.close(); } catch (IOException ex) { ex.printStackTrace(); } } }
上一篇:
IDEA上Java项目控制台中文乱码