File file = new File("path/to/file/TestAspose.xlsx");
ByteArrayInputStream in = new ByteArrayInputStream(FileUtils.readFileToByteArray(file));
ByteArrayOutputStream out = new ByteArrayOutputStream();
Workbook workbook = new Workbook(in);
HtmlSaveOptions options = new HtmlSaveOptions();
options.setPresentationPreference(true);
workbook.save(out, options);
FileOutputStream fout = new FileOutputStream("TestAspose.html");
fout.write(out.toByteArray());
fout.flush();
This is what I can see when I open the converted file(.html) open in the chrome browser.
File file = new File("path/to/file/TestAspose.xlsx");
ByteArrayInputStream in = new ByteArrayInputStream(FileUtils.readFileToByteArray(file));
ByteArrayOutputStream out = new ByteArrayOutputStream();
Workbook workbook = new Workbook(in);
HtmlSaveOptions options = new HtmlSaveOptions();
options.setPresentationPreference(true);
workbook.save("TestAsposeWorking.html", options);
Both cases can convert the file without any error but first way the converted file not loading properly and in second case can load the generated html in the browser without any issue. But I need to use first way save method to do the task. Here I have attached the different of these generated files as a screenshot.
@hasithwijerathna,
We have observed this issue and logged it inn our database for further investigation. We will write back here once any update is ready for sharing.
This issue is logged as: CELLSJAVA-43533 - XLSX to Html creation issue in Ubuntu
We evaluated your issue further. This is not an issue with the APIs. When you export a file to HTML, it contains multiple files. If you write to a stream, an error would occur. You can export each sheet page in the file separately, and then combine them as needed. According to the example file, you can use the following code to fix your issue:
e.g. Sample code:
File file = new File("path/to/file/TestAspose.xlsx");
ByteArrayInputStream in = new ByteArrayInputStream(FileUtils.readFileToByteArray(file));
ByteArrayOutputStream out = new ByteArrayOutputStream();
Workbook workbook = new Workbook(in);
HtmlSaveOptions options = new HtmlSaveOptions();
options.setPresentationPreference(true);
//add this line to export current active worksheet only.
options.setExportActiveWorksheetOnly(true);
workbook.save(out, options);
FileOutputStream fout = new FileOutputStream("TestAspose.html");
fout.write(out.toByteArray());
fout.flush();
Yes, if you use the code segment (in the post), only active sheet would be converted to HTML. If a workbook contains multiple sheets, you got to activate each sheet (one by one) to generate separate HTMLs and then finally combine the HTMLs to single HTML.
Alternatively, you may try to use the code segment shared here.
Let us know if you still have any issue, we will check it soon.
Do you need to render single HTML file per worksheet? Please see the code segment in the post for your reference. Please share your sample code (runnable) and template file(s). Please zip the files prior attaching here.
I am not sure about your requirements. Could you elaborate and give us more details and samples for reference.
@hasithwijerathna,
1, Could you post a template file and your excepted file here? We will check it ASAP?
We can try to write all data to a single table , but we do not how to layout these sheets.
2, Now you can merge all workshes to one worksheet with Range.Copy() method, then remove other worksheets.
@Override
public byte[] convert(Source source) throws ConversionException {
UUID uuid = UUID.randomUUID();
String tmpFileName = String.format("%s.html", uuid);
Path filePath = Paths.get(tmpFileName);
String tmpFolder = String.format("%s_files", uuid);
try (
ByteArrayInputStream in = new ByteArrayInputStream(source.getStream())) {
Workbook workbook = new Workbook(in);
HtmlSaveOptions options = new HtmlSaveOptions();
options.setPresentationPreference(true);
workbook.save(tmpFileName, options);
log.info("Temp file crated with name {}, and related files inside the {} folder", tmpFileName, tmpFolder);
return Files.readAllBytes(filePath);
} catch (Exception e) {
throw new ConversionException("Error when converting document", e);
} finally {
try {
Files.delete(filePath);
log.info("File {} Deleted", tmpFileName);
FileUtils.deleteDirectory(new File(tmpFolder));
log.info("All created temp files deleted : {}, {}", tmpFileName, tmpFolder);
} catch (IOException e) {
log.error("Delete error : {}", e.getMessage());
}
}
}
Above is the current implementation and we are going to avoid the IO part here.
Currently we are uploading the generated tmpFolder(with files) to our aws S3 bucket. if we have a way to get a single stream for the whole content of the xlsx(Excel file with multiple work sheets) file we can upload it to s3 without an IO operation.
We think you may try to save your Excel workbook to .mht file format. This way there will be single file (having all sheet tabs in it) for your requirements. See the document on how to save to MHTL file format for reference.
We have recorded your requirements against your existing ticket “CELLSJAVA-43533” into our database for thorough investigations. We will look into it soon. Once we have an update on it, we will let you know.
@Amjad_Sahi Thank you for the update, I tried your M_HTML conversion thing even in early you mentioned but the thing is it cannot open in the browser directly.
here is the file I converted Students.zip (6.1 KB)
I was unable to open it in a browser with all the content. but in Internet Explorer only showing the first work sheet.