如何使用java创建和保存Excel文件到DAM

时间:2016-07-26 08:26:22

标签: java adobe cq5 hssfworkbook

我正在尝试创建一个excel表并将其保存到DAM中。我能够在DAM中创建excel,但该excel中没有数据。我正在使用以下代码来创建它。

String filename = "updatedMetadata.xls";
FileOutputStream fos = new FileOutputStream(filename);
WritableWorkbook workbook = Workbook.createWorkbook(new File(filename));
WritableSheet writableSheet = workbook.createSheet("Updated Metadata", 0);
Label label = null;
label = new Label(0, 0, "Header1");
writableSheet.addCell(label);
label = new Label(0, 1, "Header2");
writableSheet.addCell(label);
label = new Label(0, 2, "Header3");
writableSheet.addCell(label);
label = new Label(0, 3, "Header4");
writableSheet.addCell(label);
label = new Label(0, 4, "Header5");
writableSheet.addCell(label);
label = new Label(0, 5, "Header6");
writableSheet.addCell(label);
label = new Label(0, 6, "Header7");
writableSheet.addCell(label);
label = new Label(0, 7, "Header8");
writableSheet.addCell(label);                       
label = new Label(1, 0, "col val");
writableSheet.addCell(label);
label = new Label(1, 1, "col val");
writableSheet.addCell(label);
label = new Label(1, 2, "col val");
writableSheet.addCell(label);
label = new Label(1, 3, "col val");
writableSheet.addCell(label);
label = new Label(1, 4, "col val");
writableSheet.addCell(label);
label = new Label(1, 5, "col val");
writableSheet.addCell(label);
label = new Label(1, 6, "col val");
writableSheet.addCell(label);
label = new Label(1, 7, "col val");
writableSheet.addCell(label);                       
label = new Label(2, 0, "col val");
writableSheet.addCell(label);
label = new Label(2, 1, "col val");
writableSheet.addCell(label);
label = new Label(2, 2, "col val");
writableSheet.addCell(label);
label = new Label(2, 3, "col val");
writableSheet.addCell(label);
label = new Label(2, 4, "col val");
writableSheet.addCell(label);
label = new Label(2, 5, "col val");
writableSheet.addCell(label);
label = new Label(2, 6, "col val");
writableSheet.addCell(label);
label = new Label(2, 7, "col val");
writableSheet.addCell(label);
workbook.write();
workbook.close();
ResourceResolver resourceResolver = request.getResourceResolver();
AssetManager manager = resourceResolver.adaptTo(AssetManager.class);
InputStream is = this.getClass().getResourceAsStream("/content/dam/company/fileFolder/" + filename);
Asset asset = manager.createAsset("/content/dam/company/fileFolder/" + filename, is, "application/vnd.ms-excel", true);

文件正在此路径中创建" / content / dam / company / fileFolder /" 文件名是" updatedMetadata.xls"

提前致谢 Mahesh Narayanan

1 个答案:

答案 0 :(得分:0)

我自己找到了。我正在共享代码,以便将来其他人可以使用它。

    String filename = "updatedMetadata.xls";
File excelFile = new File(filename);
WritableWorkbook workbook = Workbook.createWorkbook(excelFile);
WritableSheet writableSheet = workbook.createSheet("Updated Metadata", 0);

Label label = null;
label = new Label(0, 0, "Header1");
writableSheet.addCell(label);
label = new Label(0, 1, "Header2");
writableSheet.addCell(label);
label = new Label(0, 2, "Header3");
writableSheet.addCell(label);
label = new Label(0, 3, "Header4");
writableSheet.addCell(label);
label = new Label(0, 4, "Header5");
writableSheet.addCell(label);
label = new Label(0, 5, "Header6");
writableSheet.addCell(label);
label = new Label(0, 6, "Header7");
writableSheet.addCell(label);
label = new Label(0, 7, "Header8");
writableSheet.addCell(label);                       
label = new Label(1, 0, "col val");
writableSheet.addCell(label);
label = new Label(1, 1, "col val");
writableSheet.addCell(label);
label = new Label(1, 2, "col val");
writableSheet.addCell(label);
label = new Label(1, 3, "col val");
writableSheet.addCell(label);
label = new Label(1, 4, "col val");
writableSheet.addCell(label);
label = new Label(1, 5, "col val");
writableSheet.addCell(label);
label = new Label(1, 6, "col val");
writableSheet.addCell(label);
label = new Label(1, 7, "col val");
writableSheet.addCell(label);                       
label = new Label(2, 0, "col val");
writableSheet.addCell(label);
label = new Label(2, 1, "col val");
writableSheet.addCell(label);
label = new Label(2, 2, "col val");
writableSheet.addCell(label);
label = new Label(2, 3, "col val");
writableSheet.addCell(label);
label = new Label(2, 4, "col val");
writableSheet.addCell(label);
label = new Label(2, 5, "col val");
writableSheet.addCell(label);
label = new Label(2, 6, "col val");
writableSheet.addCell(label);
label = new Label(2, 7, "col val");
writableSheet.addCell(label);
workbook.write();
workbook.close();
ResourceResolver resourceResolver = request.getResourceResolver();
AssetManager manager = resourceResolver.adaptTo(AssetManager.class);
InputStream stream = new FileInputStream(excelFile);
Asset asset = manager.createAsset("/content/dam/company/fileFolder/" + filename, is, "application/vnd.ms-excel", true);