如何使用java创建Excel文件并将其保存到DAM
问题描述:
我想创建一个Excel表格并将其保存到DAM中。我能够在DAM中创建Excel,但在该Excel中没有数据。我正在使用以下代码来创建它。如何使用java创建Excel文件并将其保存到DAM
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);
文件在此路径 “/内容/大坝/公司/ fileFolder /” 文件名创建为 “updatedMetadata.xls”
在此先感谢 马赫什·纳拉亚南
答
我发现它我。我正在分享代码,以便将来有人可以使用它。
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);