大家好,又见面了,我是你们的朋友全栈君。
因为经常要用到导出功能,之前因为文件名乱码,所以都用英文或是拼音缩写来代替,今天特意要接觉下这个问题。顺便记录下。望对小白有用。
/**
* 导出结算详情列表
* @return
*/
public void depositLogExport()
{
String title = “押金流水报表”;
String excelName = “押金流水报表.xls”;
OutputStream os = null;
try
{
int shopId=new Long(getShopLoginer().getShop().getId()).intValue();
if(date==null){
date=new DateBean();
}
int count=service.getCount(shopId, date);
Page pageResult =new Page();
if(count>0){
pageResult = service.list(1, count, shopId, date);
}
List> list = new ArrayList>();
double shopDeposit=getShopLoginer().getShop().getDeposit();
for (DepositLog s : pageResult.getData())
{
Map map = new HashMap();
map.put(“id”, s.getId());
map.put(“depositSn”, s.getDepositSn());
map.put(“createTime”, StringUtils.timestampToString(s.getCreateTime()));
map.put(“type”, type[s.getType()]);
map.put(“amount”, s.getAmount());
map.put(“depositRest”, s.getDepositRest());
map.put(“depositGap”, shopDeposit-s.getDepositRest());
map.put(“userName”, s.getUserName());
map.put(“remarks”, s.getRemarks());
list.add(map);
}
// 表格表头
String[] header = {“流水号”,”流水时间”,”类型”,”流水金额”,”押金余额”,”押金缺口”,”操作人”,”备注”};
// 对应Map中的key
String[] key = { “depositSn”, “createTime”,”type”,
“amount”, “depositRest”, “depositGap”, “userName”, “remarks”};
HSSFWorkbook workbook = null;
HttpServletResponse response = ServletActionContext
.getResponse();
workbook = ExportUtil.workPaperExport(list, header, key,
title, true, null, null, null);
os = response.getOutputStream();
response.setHeader(“Content-disposition”,
“p_w_upload; filename=” toUtf8String(excelName));
response.setContentType(“application/msexcel;charset=UTF-8”);
workbook.write(os);
os.flush();
} catch (Exception e) {
if (logger.isDebugEnabled())
logger.error(e.getMessage(), e);
e.printStackTrace();
} finally {
try {
if (os != null)
os.close();
} catch (IOException e) {
if (logger.isInfoEnabled())
logger.error(e.getMessage(), e);
e.printStackTrace();
}
}
//return null;
}
//文件名乱码处理
public static String toUtf8String(String s){
StringBuffer sb = new StringBuffer();
for (int i=0;i
char c = s.charAt(i);
if (c >= 0 && c <= 255){sb.append(c);}
else{
byte[] b;
try { b = Character.toString(c).getBytes(“utf-8”);}
catch (Exception ex) {
System.out.println(ex);
b = new byte[0];
}
for (int j = 0; j < b.length; j ) {
int k = b[j];
if (k < 0) k = 256;
sb.append(“%” Integer.toHexString(k).toUpperCase());
}
}
}
return sb.toString();
}
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/179109.html原文链接:https://javaforall.cn