Free Support Forum - aspose.com

Text not splitted into 2 lines in result of converting to HTML

Hi

I am using Aspose Cells 17.8 to convert Excel files to HTML format.

Here is my code for test:

String fileName = “施工預定進度表及要逕圖.xls”;

Workbook book = new Workbook("custom/input/xlsx/" + fileName);
    new File("custom/output/xlsx/" + fileName + "/").mkdirs();

for (int page = 0; page < book.getWorksheets().getCount(); page++) {

System.out.println(book.getWorksheets().get(page).getName());
//addHeader(book, page + 1, new ArrayList<Integer>());
ByteArrayOutputStream baos = new ByteArrayOutputStream();

for (int i = 0; i < book.getWorksheets().getCount(); i++) {
	if (i == page) {
		book.getWorksheets().get(i).setVisible(true);
	}
}
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"));

}

In the result of this Excel file, the text in B4 should be splitted into 2 lines in the result just like the origin, but it was not.

result.zip (15.9 KB)
施工預定進度表及要逕圖.xls.zip (6.7 KB)
image.jpg (71.6 KB)
Please check the attachment, and this issue.
Thank you~

Craig

@craig.w.su,

Thanks for the template file(s) and sample code.

This is not an issue with the product rather it is the limitation of MS Excel, Aspose.Cells follows MS Excel standards and specifications when rendering to HTML/PDF. You may confirm this by opening the template Excel file into MS Excel and save it as “Web page”. Now open the HTML file into browse and you will that text is also not splitted into two lines.

Thank you.

Hi

@Amjad_Sahi

Thanks for your reply.
There is some question I would like to ask:

  1. Is there some way to detect a cell like B4 if the text of it exceeded the edge,
    and then change the behavior of the cell, making the text of the cell wrap to lines in the result?

  2. In this Excel file, the text of the cells, like Q9, exceeded the edge of the its cell. The text of Q9 in the result generated by Aspose also exceeded of its edge. However the black grid lines of Q9, R9, S9, T9, U9, V9 are rendered out in the result. Is there some way not to render this grid lines, making it like what you see it in the MS Excel?

Craig

@craig.w.su,

  1. I am not sure if there could be any better way to cope with it. I think you may try to create a Style object, set wrapped text on and then apply the style to the specified cell (you may auto-fit row/column to extend it accordingly). I will still check with the concerned developer if there is any option available for it and will get back to if I find any.

  2. Well, I think you may try to apply auto-fit column operation on the specified columns (e.g Q9) before rendering to HTML to make the gridlines off.

Thank you.

Hi

@Amjad_Sahi

About 2.

Can we determine if a column needs to be applied with auto-fit programmatically?

@craig.w.su,

Well, I am afraid there is no proper way to do this. We do provide a method, i.e., Cell.GetWidthOfValue() to check the rough width needed by the cell value, so you may use this method to detect the width for all cells in one column, but it is not very inefficient approach.
You can compare the width with what you get with Cells.GetColumnWidth() for the particular column. So, if it is greater/less than column’s width, you may decide if it needs auto-fit operation for the particular column or not.
Mind you, GetColumnWidth will just give you rough width, not the exact same with the actual width required for auto-fit.