【问题标题】:make specific path on Excel POI在 Excel POI 上制作特定路径
【发布时间】:2014-10-13 11:32:49
【问题描述】:

我想创建一个保存在数据库中的路径。
这是我的代码。

    @RequestMapping("import_excel")
    @ResponseBody
    public Map<String, ? extends Object> import_excel() {
        modelMap.clear();

        try {
            HttpSession session = req.getSession();
            HSSFWorkbook wb = new HSSFWorkbook();
            HSSFSheet sheet = wb.createSheet("Export");

            Row row1 = sheet.createRow((short) 3);

            row1.createCell(0).setCellValue("Asset Number");
            row1.createCell(1).setCellValue("Asset Barcode");
            row1.createCell(2).setCellValue("Asset Name"); 

            short i = 4;

            Row row2 = sheet.createRow(i++);
            row2.createCell(0).setCellValue(as.getCdas());
            row2.createCell(1).setCellValue(as.getBcd());
            row2.createCell(2).setCellValue(as.getNm());

            FileOutputStream fileOut = new FileOutputStream(src + "/Export.xls");
            wb.write(fileOut);

            fileOut.close();
            modelMap.put("success", true);
            modelMap.put("rows", obj);
            modelMap.put("count", count);
        } catch (IOException ex) {
            modelMap.put("success", false);
            modelMap.put("msg", ex.getMessage());
        }
        return modelMap;
    }


我要制作 FileOutputStream fileOut = new FileOutputStream(src + "/Export.xls");
使用我保存在 db 中的路径。所以如果我想更改路径,我只需在 db.xml 中更改即可。
我已经尝试了上面的代码,但似乎不起作用。
请帮我。谢谢:)

【问题讨论】:

  • src 的值是多少?这不是你想要的吗?
  • @Gagravarr src 是我要在数据库中调用的路径。
  • 那么与您的数据库通信的代码在哪里?我没有看到任何 JDBC 调用或类似调用...看起来您错过了代码的关键部分!

标签: java excel apache-poi


【解决方案1】:

我建议以编程方式使用它...

使用

String PathTillProject = System.getProperty("user.dir");

这将为您提供最高一级的系统路径 src 即直到您的项目(我假设您的 src 就在您的项目目录中)。例如,如果您的项目名称是TestProject,那么位置或src 就是TestProject/src。现在你可以像这样使用它:

String PathTillProject = System.getProperty("user.dir");
FileOutputStream fileOut = new FileOutputStream(PathTillProject + "/src/Export.xls"); 

【讨论】:

  • system.getProperty有什么用?那是直接到我的数据库吗?我在哪里调用我的映射器?
猜你喜欢
  • 1970-01-01
  • 2017-04-26
  • 1970-01-01
  • 2016-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多