因為經常要用到匯出功能,之前因為檔名亂碼,所以都用英文或是拼音縮寫來代替,今天特意要接覺下這個問題。順便記錄下。望對小白有用。
/**
* 匯出結算詳情列表
* @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<DepositLog> pageResult =new Page<DepositLog>();
if(count>0){
pageResult = service.list(1, count, shopId, date);
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
double shopDeposit=getShopLoginer().getShop().getDeposit();
for (DepositLog s : pageResult.getData())
Map<String, Object> map = new HashMap<String, Object>();
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",
"attachment; 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())
//return null;
//檔名亂碼處理
public static String toUtf8String(String s){
StringBuffer sb = new StringBuffer();
for (int i=0;i<s.length();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();
因為經常要用到匯出功能,之前因為檔名亂碼,所以都用英文或是拼音縮寫來代替,今天特意要接覺下這個問題。順便記錄下。望對小白有用。
/**
* 匯出結算詳情列表
* @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<DepositLog> pageResult =new Page<DepositLog>();
if(count>0){
pageResult = service.list(1, count, shopId, date);
}
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
double shopDeposit=getShopLoginer().getShop().getDeposit();
for (DepositLog s : pageResult.getData())
{
Map<String, Object> map = new HashMap<String, Object>();
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",
"attachment; 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<s.length();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();
}