如下是用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();
}
}