package com.kingdee.youshang.platform.bi.common.util;
import com.kingdee.youshang.platform.common.util.DateUtils;
import java.io.*;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
public class ExportUtil{
private static final Logger logger = Logger.getLogger(ExportUtil.class);
public static void exportCSV(String columnName[], List dataList, HttpServletRequest request, HttpServletResponse response) {
String columns = getTableColumnName(columnName);
String data = buildDataForCSV(columnName, dataList);
String fileName = (String)request.getAttribute("fileName");
if (fileName == null || "".equals(fileName))
fileName = DateUtils.convertDateToStr(new Date(), "yyyy-MM-dd");
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition",
(new StringBuilder()).append("attachment; filename=\"").append(fileName).append(".csv").append("\" ").toString());
response.setHeader("Content-Transfer-Encoding", "binary");
response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
try
{
java.io.OutputStream fos = response.getOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(fos, "gbk");
BufferedWriter bw = new BufferedWriter(writer);
bw.write((new StringBuilder()).append(columns).append(data).toString());
bw.newLine();
bw.flush();
bw.close();
}
catch (Exception e)
{
logger.error(e);
}
}
public static void exportCSV(String headName, String dataStr, HttpServletRequest request, HttpServletResponse response) {
String fileName = (String)request.getAttribute("fileName");
if (fileName == null || "".equals(fileName))
fileName = DateUtils.convertDateToStr(new Date(), "yyyy-MM-dd");
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition",
(new StringBuilder()).append("attachment; filename=\"").append(fileName).append(".csv").append("\" ").toString());
response.setHeader("Content-Transfer-Encoding", "binary");
response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
try
{
java.io.OutputStream fos = response.getOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(fos, "gbk");
BufferedWriter bw = new BufferedWriter(writer);
bw.write((new StringBuilder()).append(headName).append(dataStr).toString());
bw.newLine();
bw.flush();
bw.close();
}
catch (Exception e)
{
logger.error(e);
}
}
public static void saveDataCSV(String columnName[], List dataList, String csvFileName) {
String columns = getTableColumnName(columnName);
String data = buildDataForCSV(columnName, dataList);
csvFileName = csvFileName.replace('/', File.separatorChar);
File file = new File(csvFileName);
try
{
FileOutputStream stream = new FileOutputStream(file);
OutputStreamWriter writer = new OutputStreamWriter(stream, "gbk");
BufferedWriter bw = new BufferedWriter(writer);
bw.write((new StringBuilder()).append(columns).append(data).toString());
bw.newLine();
bw.flush();
bw.close();
}
catch (Exception e)
{
logger.error(e);
}
}
public static String getTableColumnName(String columnName[])
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < columnName.length; i++)
if (i != columnName.length - 1)
sb.append((new StringBuilder()).append("\"").append(columnName[i]).append("\",").toString());
else
sb.append((new StringBuilder()).append("\"").append(columnName[i]).append("\"").toString());
sb.append("\n");
return sb.toString();
}
private static String buildDataForCSV(String columnName[], List dataList) {
StringBuilder strb = new StringBuilder();
for (Iterator i$ = dataList.iterator(); i$.hasNext(); strb.append("\n"))
{
Map map = (Map)i$.next();
for (int i = 0; i < columnName.length; i++)
if (i != columnName.length - 1)
strb.append((new StringBuilder()).append("\"").append(map.get(columnName[i])).append("\",").toString());
else
strb.append((new StringBuilder()).append("\"").append(map.get(columnName[i])).append("\"").toString());
}
return strb.toString();
}
}
-----------------------------------------调用--------------------------------------------------
public ActionForward exportIncome(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
throws Exception {
fillqueryParamMap(request);
Pager pager = incomeService.queryIncome(paramMap, 0, 0);
List dataList = pager.getResult();
String headNameArr[] = {"编号", "日期", "业务部门", "业务类型", "合作伙伴", "收入金额", "备注"};
String headName = getTableHead(headNameArr);
String dataStr = buildDataForIncome(dataList);
ExportUtil.exportCSV(headName, dataStr, request, response);
return null;
}
private String buildDataForIncome(List dataList){
if (dataList == null || dataList.size() <= 0)
return "";
StringBuilder strb = new StringBuilder();
for (Iterator i$ = dataList.iterator(); i$.hasNext(); strb.append("\n")) {
IncomeDetailVO vo = (IncomeDetailVO)i$.next();
strb.append((new StringBuilder()).append("\"").append(vo.getFsk()).append("\",").toString());
strb.append((new StringBuilder()).append("\"").append(vo.getFdate()).append("\",").toString());
strb.append((new StringBuilder()).append("\"").append(vo.getServiceDeptName()).append("\",").toString());
strb.append((new StringBuilder()).append("\"").append(vo.getServiceType()).append("\",").toString());
strb.append((new StringBuilder()).append("\"").append(vo.getPartnerName()).append("\",").toString());
strb.append((new StringBuilder()).append("\"").append(vo.getMoney()).append("\",").toString());
strb.append((new StringBuilder()).append("\"").append(vo.getRemark()).append("\"").toString());
}
return strb.toString();
}