可以由Excel打开的制表符分隔值的文件扩展名?

时间:2013-12-16 14:21:59

标签: spring excel download file-extension tab-delimited

我正在从我的webapp输出制表符分隔文件,该文件应该在Excel中打开。问题是.xls似乎不适合打开和编辑它,然后Excel需要一些其他格式,如果我将扩展名更改为.tsv,那么Excel的文件(在Windows 7上)将变为未知,而.csv则用于逗号 - 分离。你能告诉我文件扩展名应该是什么吗?

这是输出文件的代码,它可以工作。只是我应该为制表符分隔值选择最合适的扩展名。

@RequestMapping(value = "/export", method = RequestMethod.GET)
@ResponseBody
public ModelAndView export(HttpServletResponse response) {
    try {
        String str = "";
        Iterator<Individual> iterator = customerAccountService.getAllIndividuals().iterator();
        while(iterator.hasNext()){
            Individual individual = iterator.next();
            str = str + individual.getId() + "\t" +individual.getIndividualName().getName() + "\t" + individual.getAddress().getStreetName() + "\n";
        }
        InputStream is = new ByteArrayInputStream(str.getBytes());
        IOUtils.copy(is, response.getOutputStream());
        response.setContentType("application/xls");
        response.setHeader("Content-Disposition","attachment; filename=export.tsv");
        response.flushBuffer();
    } catch (IOException ex) {
        //logger.info("Error writing file to output stream. Filename was '" + fileName + "'");
        throw new RuntimeException("IOError writing file to output stream");
    }
    ModelAndView modelAndView = new ModelAndView(ViewName.MENU);
    modelAndView.addObject(ObjectName.ADD_FORM, new LoginForm());
    return modelAndView;
}

1 个答案:

答案 0 :(得分:3)

sep=\t

作为.csv文件的第一行(是的,你可以将其命名为.csv)。这告诉excel分隔字符应该是什么。

请注意,实际上如果您使用文本编辑器打开.csv,它应该像

一样
sep=     (an actual tabulator character here, it's just not visible...)