下载文件时,弹出保存框时 文件名是乱码 如何解决
显示的是这种样子的名字 C4%BF%CA%
显示的是这种样子的名字 C4%BF%CA%
解决方案 »
- sql语句:insert into c (name,age) values(?,?);id自增,然后给他赋值,但是一直报错
- spring注入类型不匹配?:org.springframework.beans.factory.BeanCreationException
- 在线 = ,java.lang.NoClassDefFoundError: javax/servlet/jsp/JspContext
- aix +tomcat5.0.28+jdk1.42的内存问题?
- 当我把数据(或者叫元素)封装在ArrayList里面之后,我要把它们取出来,代码怎么写。
- 如何将一个int类型的变量转换成String类型?
- 求助,ibatis动态表及参数传入
- 100分-肯定加--求动态菜单原吗
- 请问怎么隐藏resin的目录列表?
- Hibernate 中save问题
- xwiki superadmin问题
- win7能安装iReport吗
HttpServletResponse response=ServletActionContext.getResponse();
OutputStream out = response.getOutputStream();
InputStream in = new FileInputStream(file);
DataInputStream din = new DataInputStream(new BufferedInputStream(in));
DataOutputStream dout = new DataOutputStream(new BufferedOutputStream(out));
//response.setContentType("application/octet-stream");
String displayname = new String("新建文本文档 (2).txt".getBytes("GBK"),"ISO-8859-1");
response.setContentType("application/x-download");
response.setHeader("Content-Disposition", "attachment;filename="+ displayname);在IE下,用utf-8好像还是乱码,所以就用GBK,就可以了
//弹出下载对话框的关键代码
response.setContentType("application/x-download");
response.setHeader("Content-Disposition","attachment;filename="+ filename);设置了编码方式没?
2、gb2312
3、UTF-8
public class util2 {
public static void excel(HttpServletResponse response, List list,
String[] firstLine, String sheetName, String fileName) {
String[] array1 = null;
try {
//格式化时间
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
String nowDate=sdf.format(new Date());
String filename=fileName+nowDate+".xls";
short i = 0;// row行标
response.setContentType("application/vnd.ms-excel");//设置生成的文件类型
response.setHeader("Content-Disposition", "filename="+ new String(filename.getBytes("gb2312"), "iso8859-1"));//设置文件头编码方式和文件名
HSSFWorkbook wb = new HSSFWorkbook();//excel文件,一个excel文件包含多个表
HSSFSheet sheet = wb.createSheet();//表,一个表包含多个行
wb.setSheetName(0, sheetName, HSSFWorkbook.ENCODING_UTF_16);// 设置sheet中文编码;
//设置字体等样式
HSSFFont font = wb.createFont();
font.setFontHeightInPoints((short) 12);
font.setFontName("Courier New");
HSSFCellStyle style = wb.createCellStyle();
style.setFont(font);
style.setWrapText(true);
style.setAlignment(HSSFCellStyle.ALIGN_LEFT);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
HSSFRow row;//行,一行包括多个单元格
HSSFCell cell;//单元格
row = sheet.createRow(i);//由HSSFSheet生成行
row.setHeightInPoints((float) 30);
//生成首行
for (short j = 0; j < firstLine.length; j++) { cell = row.createCell(j);//由行生成单元格 cell.setCellStyle(style); cell.setEncoding(HSSFCell.ENCODING_UTF_16);// 设置cell中文编码; cell.setCellValue(firstLine[j]);
sheet.setColumnWidth(j, (short) (5000)); }
//生成所有行的单元格内容,如果测试list设为null即可,或者将这一段代码注释掉
if (null == list || list.size() == 0) {
// do nothing
} else {
for (int k = 0; k < list.size(); k++) {
row = sheet.createRow(++i);
row.setHeightInPoints((float) 20);
array1 = (String[]) list.get(k);
if (null != array1 && array1.length != 0) {
for (int f = 0; f < array1.length; f++) {
cell = row.createCell((short) f);
cell.setCellStyle(style);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
System.out.println("==========="+array1[f]);
cell.setCellValue(array1[f]);
}
}
// row = sheet.createRow((int)++i);
// Student stu = (Student) list.get(k);
// //第四步,创建单元格,并设置值
// cell = row.createCell((short) k);
// cell.setCellStyle(style);
// row.createCell((short)0).setCellValue((double)stu.getId());
// cell.setEncoding(HSSFCell.ENCODING_UTF_16);
// row.createCell((short)1).setCellValue(stu.getName());
// row.createCell((short)2).setCellValue((double)stu.getAge());
// row.createCell((short)3).setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(stu.getBrith()));
}
}
//输出
OutputStream out = response.getOutputStream();
wb.write(out);
out.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return;
}
//导出Excel文件
public static List importExcels(String filename) {
List list=new ArrayList();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
File f = new File(filename);
FileInputStream is;
LvyouTopic emp=null;
int count = 0;
try {
is = new FileInputStream(new File(filename));
HSSFWorkbook wb = new HSSFWorkbook(is);
int sheetNum = wb.getNumberOfSheets();
for (int i = 0; i < sheetNum; i++) {
HSSFSheet childSheet = wb.getSheetAt(i);
int rowNum = childSheet.getLastRowNum();
if (rowNum > 0) {
for (int j = 1; j <= rowNum; j++) {
for (int k = 0; k < 1; k++) {
emp=new LvyouTopic();
HSSFCell b = childSheet.getRow(j).getCell(
(short) (k+0));//No
if (b != null) {
emp.setId(Integer.parseInt(b.toString()));
} else {
count++;
continue;
}
b = childSheet.getRow(j).getCell((short) (k+1));// Name
if (b != null) {
emp.setStuName(b.toString());
} else {
count++;
continue;
}
b=childSheet.getRow(j).getCell((short)(k+2));
if(b!=null){
try {
emp.setStuAddress(b.toString());
} catch (Exception e) {
e.printStackTrace();
}
}else{
count++;
continue;
}
// b = childSheet.getRow(j).getCell((short) (k+2));// Birthday
// if (b != null) {
// try{
// Date date=sdf.parse(b.toString());
// if(date!=null){
// emp.setBirthday(date);
// }else{
// count++;
// continue;
// }
// }catch(Exception e){
// count++;
// continue;
// }
// } else {
// count++;
// continue;
// }
// b = childSheet.getRow(j).getCell((short) (k+3));// CardType
// if (b != null) {
// emp.setCardType(b.toString());
// } else {
// emp.setCardType("");
// }
//
// b = childSheet.getRow(j).getCell((short) (k+4));// CardNO
// if (b != null) {
// emp.setCardNo(b.toString());
// } else {
// emp.setCardNo("");
// }
// b = childSheet.getRow(j).getCell((short) (k+5)); //Sex
// if (b != null&&!"".equals(b.toString().trim())) {
// if(b.toString().equals("男")){
// emp.setSex(SexEnum.Man.getValue());
// }else if(b.toString().equals("女")){
// emp.setSex(SexEnum.Woman.getValue());
// }else{
// emp.setSex(SexEnum.Unknow.getValue());
// }
// } else {
// emp.setSex(0);
// }
// b = childSheet.getRow(j).getCell((short) (k+6));// Telַ
// if (b != null) {
// emp.setTel(b.toString());
// } else {
// emp.setTel("");
// }
// b = childSheet.getRow(j).getCell((short) (k+7));//Mobile
// if (b != null) {
// emp.setMobile(b.toString());
// } else {
// count++;
// continue;
// }
// if(emp.getNo()!=null&&!"".equals(emp.getNo().trim())){
// if(this.isHasNo(emp.getNo())){
// count++;
// continue;
// }
// }else{
// count++;
// continue;
// }
// emp.setDepartment(dep.getUid());
// emp.setExpertType("");
// emp.setStatex(EmployeeStateEnum.Normal.getValue());
// emp.setUser(GeneralUtil.getLoginUser().getUid());
//
// ServiceUtil.getInstance().getEmployeeService().create(emp);
}
}
}
list.add(emp);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
response.reset();
response.setHeader("Content-Disposition", "attachment; filename=\""
+ URLEncoder.encode("名称", "UTF8") + ".xls"
+ "\"");// 设定输出文件头
response.setContentType("application/msexcel");// 设定输出类型
out.flush();
out.close();试试