We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Xlsx to html conversion issue

I am using Aspose cell java API to convert XLSX file to HTML

<dependency>
  <groupId>com.aspose</groupId>
  <artifactId>aspose-cells</artifactId>
  <version>21.6</version>
</dependency>

I tried this by two different ways.

First Method (Failing)

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.

Second Method (Succeeded)

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.


(in image Second Method Result Left & First Method Result Right)

Appreciate if anyone can explain what is the issue in my code in first way.

OS : Ubuntu 20.4
Java Version : openjdk version “1.8.0_282”
Maven Version : Apache Maven 3.6.3

@hasithwijerathna,
We are working on your sample XLSX files and will share our feedback soon.

Thank you @ahsaniqbalsidiqui :slight_smile:

@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

1 Like

Hi @ahsaniqbalsidiqui, Is there any temporary solution until your are fixing it or can you guys suggest any older version this is working.

@hasithwijerathna,
We will update you once it is investigated and some details are ready to be shared. Please spare us some time.

@ahsaniqbalsidiqui Ok thanks for the update.

@hasithwijerathna,

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();

Please try it and let us know your feedback.

@hasithwijerathna,

Moreover, you can also implement the interface “IStreamProvider” to get the correct results:
The sample code is as follows:
e.g.
Sample code:

String filePath = "D:\\JAVA43533\\";
String outputPath = filePath + "output\\";

ByteArrayOutputStream out = new ByteArrayOutputStream();
Workbook workbook = new Workbook(filePath + "TestAspose.xlsx");

HtmlSaveOptions saveOptions = new HtmlSaveOptions();
HtmlSaveOptions options = new HtmlSaveOptions();
options.setPresentationPreference(true);        
saveOptions.setStreamProvider(new ExportStreamProvider(outputPath));        

FileOutputStream stream = new FileOutputStream(filePath + "out.html");
workbook.save(stream, saveOptions);

public class ExportStreamProvider implements IStreamProvider
{
    private String    outputDir;

    public ExportStreamProvider(String dir)
    {
        outputDir = dir;
    }

    @Override
    public void closeStream(StreamProviderOptions options) throws Exception
    {
        if (options != null && options.getStream() != null)
        {
            options.getStream().close();
        }
    }

    @Override
    public void initStream(StreamProviderOptions options) throws Exception
    {
        File file = new File(outputDir);
        if (!file.exists() && !file.isDirectory())
        {
            file.mkdirs();
        }
        String defaultPath = options.getDefaultPath();
        String path = outputDir + defaultPath.substring(defaultPath.lastIndexOf("/") + 1);
        options.setCustomPath(path);
        options.setStream(new FileOutputStream(path));
    }
}

Hope, this helps a bit.

@Amjad_Sahi I tried this also and it worked but In here I think we cannot get converted all sheets if multiple sheets available in the provided xlsx.

@hasithwijerathna,

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.