大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
因为经常要用到导出功能,之前因为文件名乱码,所以都用英文或是拼音缩写来代替,今天特意要接觉下这个问题。顺便记录下。望对小白有用。
/**
* 导出结算详情列表
* @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();
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/179109.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...