0

我想像这样导出 odsfile

但我真的得到了出口是这个(标题有额外的词)

这是我的代码,感谢您的任何建议

@RequestMapping(value = "/export", method = RequestMethod.POST)
public @ResponseBody void exportOds(Long questionId,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    VoteQuestion q = voteManager.getQuestion(questionId);

    TableModel model = new DefaultTableModel(1000,1000);
    File file = File.createTempFile("temp", ".ods");

    model.setValueAt(q.getQuestion(), 0, 0);
    int row = 2;
    for (VoteOption opt : q.getOptions()) {
        model.setValueAt(opt.getDescription(), row, 0);
        model.setValueAt(opt.getVotes(), row++, 1);
    }
    SpreadSheet.createEmpty(model).saveAs(file);

    String fileName = "投票結果";
    String encodeFileName = FilenameEncoder.getInstance(request)
            .encodeFileName(fileName + ".ods");
    response.setHeader("Content-Disposition",
            "attachment; filename=" + encodeFileName);

    FileInputStream in = null;
    OutputStream out = null;
    try {
        out = response.getOutputStream();
        in = new FileInputStream(file);
        IOUtils.copy(in, out);
    } finally {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(out);
        file.deleteOnExit();
    }
}
4

1 回答 1

0

通过此代码解决

@RequestMapping(value = "/export", method = RequestMethod.POST)
public @ResponseBody void exportOds(Long questionId,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    VoteQuestion q = voteManager.getQuestion(questionId);

    String[] columName = new String[1000];
    columName[0] = q.getQuestion();
    for(int i = 1 ;i<1000;i++){
        columName[i] = "";
    }

    TableModel model = new DefaultTableModel(columName,1000);
    File file = File.createTempFile("temp", ".ods");

    int row = 1;
    for (VoteOption opt : q.getOptions()) {
        model.setValueAt(opt.getDescription(), row, 0);
        model.setValueAt(opt.getVotes(), row++, 1);
    }
    SpreadSheet.createEmpty(model).saveAs(file);

    String fileName = "投票結果";
    String encodeFileName = FilenameEncoder.getInstance(request)
            .encodeFileName(fileName + ".ods");
    response.setHeader("Content-Disposition",
            "attachment; filename=" + encodeFileName);

    FileInputStream in = null;
    OutputStream out = null;
    try {
        out = response.getOutputStream();
        in = new FileInputStream(file);
        IOUtils.copy(in, out);
    } finally {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(out);
        file.deleteOnExit();
    }
}
于 2017-07-11T04:31:42.467 回答