java 导出 Excel 中文乱码
这样字导出的文件里面的中文是乱码的服务器上的文件publicvoidservice(HttpServletRequestrequest,HttpServletRespon...
这样字导出的文件里面的中文是乱码的
服务器上的文件
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String ids = request.getParameter("ids").toString(); //获取所有的供应商 String path = request.getSession().getServletContext().getRealPath("");
String name = UUID.randomUUID().toString().toUpperCase().replace("-","");
response.setCharacterEncoding("UTF-8");
// OutputStream os = response.getOutputStream();// 取得输出流
// response.reset();// 清空输出流
// response.setHeader("Content-disposition", "attachment; filename=fine.xls","utf-8");// 设定输出文件头
// response.setHeader("Content-Disposition" ,"attachment;filename="+new String(("tre"+".xls").getBytes(),"utf-8"));
// response.setContentType("application/msexcel;");// 定义输出类型
// response.setContentType("application/vnd.ms-excel");
File f = null;
try {
f = ExcelDo.createExcel(path,name,ids);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InputStream fis = new BufferedInputStream(new FileInputStream(new File(path+"\\"+"abc.xls")));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// response.reset(); response.setContentType(contentType);
response.setHeader("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode("测试.xls", "UTF-8")+ "\"");
OutputStream toClient = new BufferedOutputStream(response.getOutputStream()); // 得到向客户端输出二进制数据的对象
toClient.write(buffer); // 输出数据
toClient.flush();
toClient.close();
} 展开
服务器上的文件
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String ids = request.getParameter("ids").toString(); //获取所有的供应商 String path = request.getSession().getServletContext().getRealPath("");
String name = UUID.randomUUID().toString().toUpperCase().replace("-","");
response.setCharacterEncoding("UTF-8");
// OutputStream os = response.getOutputStream();// 取得输出流
// response.reset();// 清空输出流
// response.setHeader("Content-disposition", "attachment; filename=fine.xls","utf-8");// 设定输出文件头
// response.setHeader("Content-Disposition" ,"attachment;filename="+new String(("tre"+".xls").getBytes(),"utf-8"));
// response.setContentType("application/msexcel;");// 定义输出类型
// response.setContentType("application/vnd.ms-excel");
File f = null;
try {
f = ExcelDo.createExcel(path,name,ids);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InputStream fis = new BufferedInputStream(new FileInputStream(new File(path+"\\"+"abc.xls")));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// response.reset(); response.setContentType(contentType);
response.setHeader("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode("测试.xls", "UTF-8")+ "\"");
OutputStream toClient = new BufferedOutputStream(response.getOutputStream()); // 得到向客户端输出二进制数据的对象
toClient.write(buffer); // 输出数据
toClient.flush();
toClient.close();
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询