Writing specific HTML to xls throws java.lang.NullPointerException

We have a basic code like:

cell.setHtmlString(cellStr);

And cellStr contains:

⊃∆µ

        £✓✗

                     →↔↑

We are getting a nullpointerexception and if we save the content to a html file, the browser display correctly the information.

are we missing an encoding configuration in this case? I’m attaching as well an image where the browser renders the informaton correctly.

Thanks for your help!

@aldog

Thanks for using Aspose APIs.

Please provide us your sample code as well as your image. It seems, you have forgotten to attach the image. Once, you will provide the sample code, we will look into this issue at our end and help you asap.

Thank you Guys!

Here is a sample way that we retrieve the data and try to convert to HTML:

String cellStr = convertToHtml(data[i].substring(1, data[i].lastIndexOf("") + 6).replaceAll("\r\n", “”)); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

							//In linux and Mac OS, an extra break line comes up at the beginning and end of the rich text, the following lines remove them
							if(cellStr.startsWith(ImportExportUtils.XML_NAMESPACE + "\n")) {
								cellStr = cellStr.replaceAll(ImportExportUtils.XML_NAMESPACE + "\n", ImportExportUtils.XML_NAMESPACE); //$NON-NLS-1$ 
							}
							if(cellStr.endsWith("\n</div>")) { //$NON-NLS-1$
								cellStr = cellStr.replaceAll("\n</div>", "</div>"); //$NON-NLS-1$  //$NON-NLS-2$
							}
							
							cell.setHtmlString(cellStr);

Basically we are parsing a document and put it into a cell via setHtmlString.

I can see that we have some styles with NaNpt, not sure if that might affect, see image.

Capture.PNG (27.3 KB)

@aldog

Thanks for using Aspose APIs.

Please see the following sample code, its output Excel file, comments and screenshot for a reference. It should help you fix your issue. Let us know your feedback.

Output Excel File.zip (5.7 KB)

Java

//Create workbook object
Workbook wb = new Workbook();

//Access first worksheet
Worksheet ws = wb.getWorksheets().get(0);

//Access cell D4 and assign it html
Cell cell = ws.getCells().get("D4");
cell.setHtmlString("<Font Style=\"FONT-FAMILY: Calibri;FONT-SIZE: 11pt;COLOR: #000000;\"><Br>⊃∆µ<Br><Br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;£✓✗<Br><Br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;→↔↑</Font>");

//Wrap the D4 text
Style st = cell.getStyle();
st.setTextWrapped(true);
cell.setStyle(st);

//Set the column width of cell D4
ws.getCells().setColumnWidth(cell.getColumn(), 17);

//Save the output Excel file
wb.save(dirPath + "output.xlsx");

C#

//Create workbook object
Workbook wb = new Workbook();

//Access first worksheet
Worksheet ws = wb.Worksheets[0];

//Access cell D4 and assign it html
Cell cell = ws.Cells["D4"];
cell.HtmlString = "<Font Style=\"FONT-FAMILY: Calibri;FONT-SIZE: 11pt;COLOR: #000000;\"><Br>⊃∆µ<Br><Br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;£✓✗<Br><Br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;→↔↑</Font>";

//Wrap the D4 text
Style st = cell.GetStyle();
st.IsTextWrapped = true;
cell.SetStyle(st);

//Set the column width of cell D4
ws.Cells.SetColumnWidth(cell.Column, 17);
            
//Save the output Excel file
wb.Save("output.xlsx");

Screenshot

Hey Guys! It worked, really appreciate!

@aldog

Thanks for your feedback and using Aspose.Cells.

It is good to know that your issue is resolved now. Let us know if you encounter any other issue, we will be glad to look into it and help you further.