如下是用jxl下载excel的代码代码:
@RequestMapping(value = "downexceljxl")
@ResponseBody
public void downexceljxl(HttpServletRequest request, HttpServletResponse response) {
try {
response.setContentType("application/vnd.ms-excel");
OutputStream os=response.getOutputStream();
WritableWorkbook workbook = Workbook.createWorkbook(os);
WritableSheet sheet = workbook.createSheet("First Sheet",0);
Label xuexiao = new Label(0,0,"学校");//列,行,值
sheet.addCell(xuexiao);
Label zhuanye = new Label(1,0,"专业");
sheet.addCell(zhuanye);
Label jingzhengli = new Label(2,0,"专业竞争力");
sheet.addCell(jingzhengli);
Label qinghua = new Label(0,1,"清华大学");
sheet.addCell(qinghua);
Label jisuanji = new Label(1,1,"计算机专业");
sheet.addCell(jisuanji);
Label gao = new Label(2,1,"高");
sheet.addCell(gao);
workbook.write();
workbook.close();
response.setHeader("Content-disposition", "attachment;filename="+java.net.URLEncoder.encode("excel.xls", "UTF-8"));
os.close();
}catch (Exception e){
e.printStackTrace();
}
}
简单的springmvc代码,用jxl下载excel, 发现谷歌浏览器左下角下载下来的文件名是
downexceljxl.do,为什么不是excel.xls,可以像poi下载excel一样指定文件名吗,望技术牛人帮忙解答一下,谢谢
如下是poi下载excel的例子可以生成指定文件名(**希望能对需要的人带来帮助**),
@RequestMapping(value = "downexcelpoi")
@ResponseBody
public void downexcelpoi(HttpServletRequest request, HttpServletResponse response) {
try {
//response.reset();//重置输出流
response.setContentType("application/vnd.ms-excel");
final String sheetname = "sheet";
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFCellStyle style = workbook.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
HSSFFont font = workbook.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
style.setFont(font);
HSSFSheet sheet;
sheet = workbook.createSheet(sheetname);
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell(0);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(new HSSFRichTextString("姓名"));
cell = row.createCell(1);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(new HSSFRichTextString("年龄"));
for (int i=1;i<10;i++){
row = sheet.createRow(i);
cell = row.createCell(0);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(new HSSFRichTextString("姓名"+i));
cell = row.createCell(1);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(new HSSFRichTextString("年龄"+i));
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
workbook.write(bos);
} catch (IOException e) {
// TODO Auto-generated catch block
throw new Exception("导出失败");
}
byte[] ba = bos.toByteArray();
bos.close();
ByteArrayInputStream bis = new ByteArrayInputStream(ba);
HSSFWorkbook xwb = (HSSFWorkbook) WorkbookFactory.create(bis);
OutputStream out=response.getOutputStream();
xwb.write(bos);
response.setHeader("Content-disposition", "attachment;filename="+java.net.URLEncoder.encode("excel.xls", "UTF-8"));
}catch (Exception e){
e.printStackTrace();
}
}
具体没太理解你的意思;但是springMVC用jxl导出excel就相当于jxl生成一个excel子啊你的某个目录下;再用下载的方式下载下来;生成的路径无所谓
只要你能够读取到那个生成的excel就行
这是我写的;希望能帮到你
//导出
@RequestMapping(value = "/dingdanExcel")
public void downloadExcel(HttpServletRequest request,HttpServletResponse response, OrderQueryParms orderParam) throws IOException {
//导出文件内容到到项目下
String savePath=request.getSession().getServletContext().getRealPath("/webpage/device");
String fileName="123.csv";
ResultDto<DataGridVo<OrderDto>> queryOrder = orderService.queryOrder(orderParam);
DingdanExcel.toDingdanExcel(queryOrder.getData().getList(),savePath+"/123.csv");
//下载
response.setContentType("application/vnd.ms-excel");
File file = new File(savePath+System.getProperty("file.separator")+fileName);
// 清空response
response.reset();
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename="+new String(fileName.getBytes("gbk"),"iso-8859-1")); //转码之后下载的文件不会出现中文乱码
response.addHeader("Content-Length", "" + file.length());
try{
//以流的形式下载文件
InputStream fis = new BufferedInputStream(new FileInputStream(savePath+System.getProperty("file.separator")+fileName));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
toClient.write(buffer);
toClient.flush();
toClient.close();
}catch(Exception e){
e.printStackTrace();
}
}
具体下载 下来的文件名你是可以设置的;比如我的代码中我就写成123.csv
楼上回答正确无误,希望能解决困惑。
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename="+URLEncoder.encode("这里就是你要指定的文件名称.xls","UTF-8"));