Java 實現導出CSV以及如何調用提示信息的配置文件及轉編碼

1 篇文章 0 订阅
1 篇文章 0 订阅
      /**
* 根據查詢條件日期區間和Function Name匯出系統錯誤紀錄CSV檔
* @param request Http請求
* @param response Http響應
* @throws Exception 拋出異常
*/
public void exportCSV(HttpServletRequest request,
HttpServletResponse response) throws Exception  {
//獲取匯出資料
String starDate = request.getParameter("startDate");
String endDate = request.getParameter("endDate");
String summary = request.getParameter("emailServer");
List<TestLog> listTestLog= iTestLogBo.query(starDate, endDate, summary);
//初始化OutputStream
OutputStream outdata = response.getOutputStream();
response.setContentType("application/csv;charset=big5");
response.setCharacterEncoding("big5");
response.setHeader("Content-Disposition", "attachment; filename=\"" + java.net.URLEncoder.encode("CsvName","big5") + ".csv" + "\"");
OutputStreamWriter osw = new OutputStreamWriter(outdata,"big5");
BufferedWriter out = new BufferedWriter(osw);

out.write(getHead());
//填充資料
String strWrite = "";
if (listTestLog!=null && listTestLog.size() > 0) {
for (TestLog testLog : listTestLog) {
String summaryOld =testLog.getSummary();
String newSummary=summaryOld.replace(",", ",");//避免在summary中有“,”的情況,而導致錯位
strWrite = newSummary.trim() + "," +testLog.getCreatedDate().trim() 
+ "," + testLog.getErrorCode().trim() 
+ "," + testLog.getCreatedUser().trim() + "\r\n";

// strWrite = testLog.getSummary().trim() + "," + "''"+testLog.getCreatedDate().trim() 
// + "," + testLog.getErrorCode().trim() 
// + "," + testLog.getCreatedUser().trim() + "\r\n";

out.write(strWrite);
}
}
out.close();
osw.close();
outdata.flush();
outdata.close();
response.flushBuffer();
LOGGER.error(this.getMessage("S012"));//匯出CSV成功
}


/**
* 獲取CSV頭
* @return String CSV頭
* @throws IOException 拋出異常
*/
private String getHead() throws IOException {
StringBuffer headStr = new StringBuffer();
headStr.append("Function Name,");
headStr.append(this.getMessage("M001"));//時間,
headStr.append(this.getMessage("M002"));//錯誤代碼,
headStr.append(this.getMessage("M003")).append("\r\n");//使用者帳號
return headStr.toString();
}

/**
* 獲取提示信息
* @param flag 提示信息
* @return String 中文信息
* @throws IOException 拋出異常
*/
public String getMessage(String flag) throws IOException{
String messages="";
messages=ResourceUtils.getProperty(Constant. APP_MESSAGE, flag);
return messages;

}

      /**
* 提示信息文件
*/
public static final String APP_MESSAGE="app-message.properties";

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值