相关疑难解决方法(0)

为excel文档设置mime类型

MS Excel具有以下观察到的MIME类型:

  • application/vnd.ms-excel (官方)
  • application/msexcel
  • application/x-msexcel
  • application/x-ms-excel
  • application/x-excel
  • application/x-dos_ms_excel
  • application/xls
  • application/x-xls
  • application/vnd.openxmlformats-officedocument.spreadsheetml.sheet (XLSX)

是否有任何一种适用于所有版本的类型?如果没有,我们是否需要response.setContentType()单独设置这些mime类型中的每一个?

此外,我们在应用程序中使用文件流来显示文档(不仅仅是excel - 任何类型的文档).这样做,如果用户选择保存文件,我们如何保留文件名 - 目前,呈现文件的servlet名称显示为默认名称.

excel mime content-type

340
推荐指数
5
解决办法
40万
查看次数

从Spring返回Excel可下载文件

所以我有一个Spring控制器,我想创建一个Excel文件并将其返回,以便浏览器下载它.

我正在使用JEXcelApi.

这是我的控制器代码

@RequestMapping(value="/excel/cols/{colString}/rows/{rowString}/", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<String> exportExcel(HttpServletResponse response,
    @PathVariable List<String> colString,
    @PathVariable List<String> rowString) throws JSONException, IOException, WriteException {
    WritableWorkbook workbook = Workbook.createWorkbook(new File("exported.xls"));
    WritableSheet sheet = workbook.createSheet("Exported",0);
    String[] cols = colString.get(0).split(",");
    String[] rows = rowString.get(0).split(","); 
    for(int i = 0; i < cols.length;i++){
        Label label = new Label(i,0, cols[i]);
        sheet.addCell(label);
    }
    int excelCol = 0;
    int excelRow = 1;
    for(int i = 0; i < rows.length;i++){
        Label label = new Label(excelCol,excelRow, rows[i]);
        sheet.addCell(label);
        excelCol++;
        if((i+1) % …
Run Code Online (Sandbox Code Playgroud)

java excel spring spring-mvc

8
推荐指数
1
解决办法
3万
查看次数

标签 统计

excel ×2

content-type ×1

java ×1

mime ×1

spring ×1

spring-mvc ×1