前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >导出的Excel名字乱码_恢复的excel文件乱码

导出的Excel名字乱码_恢复的excel文件乱码

作者头像
全栈程序员站长
发布2022-09-27 10:33:40
1.2K0
发布2022-09-27 10:33:40
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

因为经常要用到导出功能,之前因为文件名乱码,所以都用英文或是拼音缩写来代替,今天特意要接觉下这个问题。顺便记录下。望对小白有用。

/**

* 导出结算详情列表

* @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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
腾讯云 BI
腾讯云 BI(Business Intelligence,BI)提供从数据源接入、数据建模到数据可视化分析全流程的BI能力,帮助经营者快速获取决策数据依据。系统采用敏捷自助式设计,使用者仅需通过简单拖拽即可完成原本复杂的报表开发过程,并支持报表的分享、推送等企业协作场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档