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

Free Support Forum - aspose.com

Some texts are hidden in the result of saving a Excel file into HTML format

Hi there


We are testing saving Excel files into HTML format for cross-platform viewing.
Here is our code:

@Test
public void asposeSaveStream() throws Exception {
String fileName = “wallie.xlsx”;
String password = “”;

Workbook book = new Workbook(“custom/input/xlsx/”+fileName);

File file = new File(“custom/output/xlsx/” + fileName);
file.mkdirs();

for (int page = 0; page < book.getWorksheets().getCount(); page++) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
book.getWorksheets().get(page).setVisible(true);

addEmptyColumn(book, page);
addColRowHeader(book, page);
addColRowHeaderStyle(book, page);
fixCellStyle(book, page);
for (int i = 0; i < book.getWorksheets().getCount(); i++) {
if (i != page) {
book.getWorksheets().get(i).setVisible(false);
}
}
HtmlSaveOptions saveOps = new HtmlSaveOptions();
saveOps.setClearData(false);
saveOps.setCreateDirectory(false);
saveOps.setExportActiveWorksheetOnly(false);
saveOps.setExportHiddenWorksheet(false);
saveOps.setParseHtmlTagInCell(true);
saveOps.setEncoding(Encoding.getUTF8());
saveOps.setHiddenRowDisplayType(HtmlHiddenRowDisplayType.REMOVE);
saveOps.setHiddenColDisplayType(HtmlHiddenColDisplayType.REMOVE);
saveOps.setExportImagesAsBase64(true);
saveOps.setStreamProvider(new IStreamProvider() {

@Override
public void initStream(StreamProviderOptions arg0) throws Exception {
arg0.setStream(new ByteArrayOutputStream());
}

@Override
public void closeStream(StreamProviderOptions arg0) throws Exception {
System.out.println(arg0.getDefaultPath());
OutputStream stream = arg0.getStream();
if (stream instanceof ByteArrayOutputStream) {
ByteArrayOutputStream bb = (ByteArrayOutputStream) stream;
IOUtils.write(bb.toByteArray(),
new FileOutputStream(“custom/output/xlsx/” + arg0.getDefaultPath()));
}
}
});
book.save(baos, saveOps);
IOUtils.write(baos.toByteArray(),
new FileOutputStream(“custom/output/xlsx/” + fileName + “/” + (page + 1) + “.html”));
}
}

protected void addEmptyColumn(Workbook book, int index) {
Worksheet sheet = book.getWorksheets().get(index);
int columns = sheet.getCells().getMaxDisplayRange().getColumnCount();
if (columns > 0) {
sheet.getCells().insertColumn(columns);
sheet.getCells().clearRange(0, columns,
sheet.getCells().getMaxDisplayRange().getRowCount() - 1,
columns);
sheet.getCells().getColumns().get(columns).setWidth(8.5);
}
}

protected void addColRowHeader(Workbook book, int index) {
Worksheet sheet = book.getWorksheets().get(index);
StyleFlag styleFlagGrid = new StyleFlag();
styleFlagGrid.setBorders(true);
sheet.getCells().insertColumn(0);
sheet.getCells().insertRow(0);

sheet.getCells().getRows().get(0).setHeight(15.75);
sheet.getCells().getColumns().get(0).setWidth(4.75);
int maxRow =
// sheet.getCells().getMaxRow();
sheet.getCells().getMaxDisplayRange().getRowCount();
int maxCol =
// sheet.getCells().getMaxColumn();
sheet.getCells().getMaxDisplayRange().getColumnCount();
for (int j = 1; j < maxRow; j++) {
sheet.getCells().get(j, 0).putValue(j);
}
for (int k = 1; k < maxCol; k++) {
sheet.getCells().get(0, k)
.putValue(CellsHelper.columnIndexToName(k - 1));
}
}


protected void addColRowHeaderStyle(Workbook book, int index)
throws Exception {
Worksheet sheet = book.getWorksheets().get(index);
StyleFlag styleFlagHeader = new StyleFlag();
styleFlagHeader.setAll(true);
Style headerStyle = book.createStyle();
headerStyle.setForegroundColor(Color.fromArgb(238, 238, 238));
headerStyle.setPattern(BackgroundType.SOLID);
headerStyle.getFont().setBold(true);
headerStyle.setVerticalAlignment(1);
headerStyle.setHorizontalAlignment(1);
headerStyle.setBorder(BorderType.LEFT_BORDER, CellBorderType.THIN,
Color.fromArgb(204, 204, 204));
headerStyle.setBorder(BorderType.RIGHT_BORDER, CellBorderType.THIN,
Color.fromArgb(204, 204, 204));
headerStyle.setBorder(BorderType.BOTTOM_BORDER, CellBorderType.THIN,
Color.fromArgb(204, 204, 204));
headerStyle.setBorder(BorderType.TOP_BORDER, CellBorderType.THIN,
Color.fromArgb(204, 204, 204));

int maxRow = sheet.getCells().getMaxRow();
int maxCol = sheet.getCells().getMaxColumn();
for (int k = 0; k <= maxRow; k++) {
sheet.getCells().get(k, 0).setStyle(headerStyle, styleFlagHeader);
}
for (int j = 0; j <= maxCol; j++) {
sheet.getCells().get(0, j).setStyle(headerStyle, styleFlagHeader);
}
}


protected void fixCellStyle(Workbook book, int index) {
StyleFlag styleFlagGrid = new StyleFlag();
styleFlagGrid.setBorders(true);
Worksheet sheet = book.getWorksheets().get(index);
int maxRow = sheet.getCells().getMaxRow();
int maxCol = sheet.getCells().getMaxColumn();
for (int datai = 0; datai <= maxRow; datai++) {
for (int datay = 0; datay <= maxCol; datay++) {
Cell dataCell = sheet.getCells().get(datai, datay);
Style dataStyle = dataCell.getStyle();
if (dataStyle.getBorders()
.getByBorderType(BorderType.LEFT_BORDER).getLineStyle() == 0) {
dataStyle.setBorder(BorderType.LEFT_BORDER,
CellBorderType.THIN, Color.fromArgb(204, 204, 204));
}
if (dataStyle.getBorders()
.getByBorderType(BorderType.RIGHT_BORDER)
.getLineStyle() == 0) {
dataStyle.setBorder(BorderType.RIGHT_BORDER,
CellBorderType.THIN, Color.fromArgb(204, 204, 204));
}
if (dataStyle.getBorders()
.getByBorderType(BorderType.BOTTOM_BORDER)
.getLineStyle() == 0) {
dataStyle.setBorder(BorderType.BOTTOM_BORDER,
CellBorderType.THIN, Color.fromArgb(204, 204, 204));
}
if (dataStyle.getBorders()
.getByBorderType(BorderType.TOP_BORDER).getLineStyle() == 0) {
dataStyle.setBorder(BorderType.TOP_BORDER,
CellBorderType.THIN, Color.fromArgb(204, 204, 204));
}
dataCell.setStyle(dataStyle, styleFlagGrid);
dataCell.getStyle().setNumber(49);
}
}
}


In the result a segment of text is hidden in the html by style, and i would be like this:

<span style=‘display:none’>d;kfjsdlkfjksdfjsdlkfosaefpoikjfoi[aweflawue

Is there a way to make it not hidden when the API generating html?

I have uploaded the result and this Excel file.
Please check the attachment and help us solve this issue, thank you~


Craig

Hi,


Thanks for the template file and sample code segment.

After an initial test, I observed the issue as you mentioned by using your sample code with your template Excel file. I found that some text is hidden in the resultant HTML file in Excel to HTML rendering as per your attached screenshot. I have logged a ticket with an id “CELLSJAVA-42183” for your issue. We will look into it soon.

Once we have an update on it, we will let you know here.

Thank you.

Hi,


I have evaluated your issue further. Could you try to add a line to your code segment and check if it figures out your issue. I have tested by adding the line (in bold) and it works fine.
e.g
Sample code:

@Test
public void asposeSaveStream() throws Exception {
String fileName = “wallie.xlsx”;
String password = “”;

Workbook book = new Workbook(“custom/input/xlsx/”+fileName);

File file = new File(“custom/output/xlsx/” + fileName);
file.mkdirs();

for (int page = 0; page < book.getWorksheets().getCount(); page++) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
book.getWorksheets().get(page).setVisible(true);

addEmptyColumn(book, page);
addColRowHeader(book, page);
addColRowHeaderStyle(book, page);
fixCellStyle(book, page);
for (int i = 0; i < book.getWorksheets().getCount(); i++) {
if (i != page) {
book.getWorksheets().get(i).setVisible(false);
}
}
HtmlSaveOptions saveOps = new HtmlSaveOptions();
saveOps.setClearData(false);
saveOps.setCreateDirectory(false);
saveOps.setExportActiveWorksheetOnly(false);
saveOps.setExportHiddenWorksheet(false);
saveOps.setParseHtmlTagInCell(true);
saveOps.setEncoding(Encoding.getUTF8());
saveOps.setHiddenRowDisplayType(HtmlHiddenRowDisplayType.REMOVE);
saveOps.setHiddenColDisplayType(HtmlHiddenColDisplayType.REMOVE);
saveOps.setExportImagesAsBase64(true);
saveOps.setHtmlCrossStringType(HtmlCrossType.CROSS);
saveOps.setStreamProvider(new IStreamProvider() {

@Override
public void initStream(StreamProviderOptions arg0) throws Exception {
arg0.setStream(new ByteArrayOutputStream());
}

@Override
public void closeStream(StreamProviderOptions arg0) throws Exception {
System.out.println(arg0.getDefaultPath());
OutputStream stream = arg0.getStream();
if (stream instanceof ByteArrayOutputStream) {
ByteArrayOutputStream bb = (ByteArrayOutputStream) stream;
IOUtils.write(bb.toByteArray(),
new FileOutputStream(“custom/output/xlsx/” + arg0.getDefaultPath()));
}
}
});
book.save(baos, saveOps);
IOUtils.write(baos.toByteArray(),
new FileOutputStream(“custom/output/xlsx/” + fileName + “/” + (page + 1) + “.html”));
}
}


Let us know if you still have any issue.

Thank you.