HtmlString getting 'Index was outside the bounds of the array' without \n at the end

blockCell.HtmlString =

<table>
  <tr>
    <th>name</th>
    <th>current date</th>
    <th>variable</th>
  </tr>
  <tr>
    <td><div class="output-control-container">ALRT-23-10-908</div></td>
    <td><div class="output-control-container">2025-03-04T15:43:07.810+03:00</div></td>
    <td><div class="output-control-container">123</div></td>
  </tr>
  <tr>
    <td>Centro comercial Moctezuma</td>
    <td>Francisco Chang</td>
    <td>Mexico</td>
  </tr>
</table>

Giving me error: {“Index was outside the bounds of the array.”}

But this does work properly:

<table>
  <tr>
    <th>name</th>
    <th>current date</th>
    <th>variable</th>
  </tr>
  <tr>
    <td><div class='output-control-result-compiled'><div class="output-control-container"><div class="simple-text">ALRT-23-10-908</div></div></div></td>
    <td><div class='output-control-result-compiled'><div class="output-control-container"><div class="simple-text">2025-03-04T15:47:19.581+03:00</div></div></div></td>
    <td><div class='output-control-result-compiled'><div class="output-control-container"><div>123</div></div></div></td>
  </tr>
  <tr>
    <td>Centro comercial Moctezuma</td>
    <td>Francisco Chang</td>
    <td>Mexico</td>
  </tr>
</table>

Why?

@bloodboilAaromatic

The error you are encountering, {"Index was outside the bounds of the array."}, when using the HTML string <td><div>text</div></td> is likely due to the way Aspose.Cells handles HTML content. The library has specific requirements for the HTML structure it can process, and it appears that the simpler structure with just <div> tags inside <td> is not supported.

In contrast, the more complex structure you provided, which includes nested <div> elements, works correctly. This suggests that Aspose.Cells may require certain formatting or additional elements to properly parse and render the HTML content.

To resolve this issue, you can try the following approaches:

  1. Use Nested Divs: As you have already discovered, wrapping your content in additional <div> tags seems to work. You can continue using this method to ensure compatibility.

  2. Check HTML Structure: Ensure that your HTML is well-formed and adheres to the standards that Aspose.Cells expects. Sometimes, adding more structure (like additional <div> or <span> tags) can help.

  3. Use HtmlLoadOptions: If you are loading HTML from a string or file, consider using HtmlLoadOptions with the SupportDivTag property set to true. This can help the library better understand how to handle <div> tags.

Here is a sample code snippet that demonstrates how to set up HtmlLoadOptions:

import com.aspose.cells.*;

public class HtmlStringExample {
    public static void main(String[] args) throws Exception {
        // Create a new workbook
        Workbook workbook = new Workbook();
        Worksheet worksheet = workbook.getWorksheets().get(0);
        
        // Define HTML string
        String htmlString = "<table><tr><td><div class='output-control-container'>Your Text Here</div></td></tr></table>";
        
        // Set HTML load options
        HtmlLoadOptions loadOptions = new HtmlLoadOptions(LoadFormat.HTML);
        loadOptions.setSupportDivTag(true);
        
        // Load HTML into the workbook
        workbook = new Workbook(new ByteArrayInputStream(htmlString.getBytes()), loadOptions);
        
        // Save the workbook
        workbook.save("output.xlsx");
    }
}

By following these suggestions, you should be able to avoid the error and successfully render your HTML content in the cells. If you continue to experience issues, consider checking the Aspose.Cells documentation for more details on supported HTML features.

@bloodboilAaromatic,

Which version of Aspose.Cells you are using? I tested your scenario/case with Aspose.Cells for .NET v25.2 using the following sample code, it works for your both strings without any issue/error.
e.g.,
Sample code:

// Create a new workbook
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
      
// Define HTML string
String htmlString1 = "<table>\n" +
                "  <tr>\n" +
                "    <th>name</th>\n" +
                "    <th>current date</th>\n" +
                "    <th>variable</th>\n" +
                "  </tr>\n" +
                "  <tr>\n" +
                "    <td><div class=\"output-control-container\">ALRT-23-10-908</div></td>\n" +
                "    <td><div class=\"output-control-container\">2025-03-04T15:43:07.810+03:00</div></td>\n" +
                "    <td><div class=\"output-control-container\">123</div></td>\n" +
                "  </tr>\n" +
                "  <tr>\n" +
                "    <td>Centro comercial Moctezuma</td>\n" +
                "    <td>Francisco Chang</td>\n" +
                "    <td>Mexico</td>\n" +
                "  </tr>\n" +
                "</table>";
worksheet.Cells["B2"].HtmlString = htmlString1;

//another html string
String htmlString2 = "<table>\n" +
                "  <tr>\n" +
                "    <th>name</th>\n" +
                "    <th>current date</th>\n" +
                "    <th>variable</th>\n" +
                "  </tr>\n" +
                "  <tr>\n" +
                "    <td><div class='output-control-result-compiled'><div class=\"output-control-container\"><div class=\"simple-text\">ALRT-23-10-908</div></div></div></td>\n" +
                "    <td><div class='output-control-result-compiled'><div class=\"output-control-container\"><div class=\"simple-text\">2025-03-04T15:47:19.581+03:00</div></div></div></td>\n" +
                "    <td><div class='output-control-result-compiled'><div class=\"output-control-container\"><div>123</div></div></div></td>\n" +
                "  </tr>\n" +
                "  <tr>\n" +
                "    <td>Centro comercial Moctezuma</td>\n" +
                "    <td>Francisco Chang</td>\n" +
                "    <td>Mexico</td>\n" +
                "  </tr>\n" +
                "</table>";
worksheet.Cells["C2"].HtmlString = htmlString2;

// Save the workbook
workbook.Save("e:\\test2\\out1.xlsx");

Please find attached the output Excel file for your reference.
out1.zip (6.1 KB)

Please try the latest version/fix, i.e., Aspose.Cells for .NET v25.2 and in case you still find the issue with latest version, please share your exact (runnable) code to reproduce the issue on our end. We will check your issue soon.

yeah, i guess its my fault
/n/n at start of both html will cause result of Index error
Additionally want to ask you confirm that
If html gonna look like that

/n/n
<table>
  <tr>
    <th>name</th>
    <th>current date</th>
    <th>variable</th>
  </tr>
  <tr>
    <td><div class='output-control-result-compiled'><div class="output-control-container"><div class="simple-text">ALRT-23-10-908</div></div></div></td>
    <td><div class='output-control-result-compiled'><div class="output-control-container"><div class="simple-text">2025-03-04T15:47:19.581+03:00</div></div></div></td>
    <td><div class='output-control-result-compiled'><div class="output-control-container"><div>123</div></div></div></td>
  </tr>
  <tr>
    <td>Centro comercial Moctezuma</td>
    <td>Francisco Chang</td>
    <td>Mexico</td>
  </tr>
</table>

It will cause Index outside of bounds

@bloodboilAaromatic,

I tested and it works fine. Here is the html string:

String htmlString1 = "/n/n<table>\n" +
                "  <tr>\n" +
                "    <th>name</th>\n" +
                "    <th>current date</th>\n" +
                "    <th>variable</th>\n" +
                "  </tr>\n" +
                "  <tr>\n" +
                "    <td><div class='output-control-result-compiled'><div class=\"output-control-container\"><div class=\"simple-text\">ALRT-23-10-908</div></div></div></td>\n" +
                "    <td><div class='output-control-result-compiled'><div class=\"output-control-container\"><div class=\"simple-text\">2025-03-04T15:47:19.581+03:00</div></div></div></td>\n" +
                "    <td><div class='output-control-result-compiled'><div class=\"output-control-container\"><div>123</div></div></div></td>\n" +
                "  </tr>\n" +
                "  <tr>\n" +
                "    <td>Centro comercial Moctezuma</td>\n" +
                "    <td>Francisco Chang</td>\n" +
                "    <td>Mexico</td>\n" +
                "  </tr>\n" +
                "</table>";

even I tried as following and it still works fine:

String htmlString1 = "/n/n\n" +
               "<table>\n" +
               "  <tr>\n" +
               "    <th>name</th>\n" +
               "    <th>current date</th>\n" +
               "    <th>variable</th>\n" +
               "  </tr>\n" +
               "  <tr>\n" +
               "    <td><div class='output-control-result-compiled'><div class=\"output-control-container\"><div class=\"simple-text\">ALRT-23-10-908</div></div></div></td>\n" +
               "    <td><div class='output-control-result-compiled'><div class=\"output-control-container\"><div class=\"simple-text\">2025-03-04T15:47:19.581+03:00</div></div></div></td>\n" +
               "    <td><div class='output-control-result-compiled'><div class=\"output-control-container\"><div>123</div></div></div></td>\n" +
               "  </tr>\n" +
               "  <tr>\n" +
               "    <td>Centro comercial Moctezuma</td>\n" +
               "    <td>Francisco Chang</td>\n" +
               "    <td>Mexico</td>\n" +
               "  </tr>\n" +
               "</table>";

Works fine:

"<table style=\"width: 100%\">\n<tbody><tr>\n<th>1</th>\n<th>2</th>\n<th>3</th>\n</tr>\n<tr>\n<td><div>2025-03-05T11:49:26.238+03:00</div></td>\n<td><div>22</div></td>\n<td></td>\n</tr>\n<tr>\n<td></td>\n<td></td>\n<td></td>\n</tr>\n</tbody></table>\n"

Error:

"<table style=\"width: 100%\">\n<tbody><tr>\n<th>1</th>\n<th>2</th>\n<th>3</th>\n</tr>\n<tr>\n<td><div>2025-03-05T11:50:21.454+03:00</div></td>\n<td><div>22</div></td>\n<td></td>\n</tr>\n<tr>\n<td></td>\n<td></td>\n<td></td>\n</tr>\n</tbody></table>"

If \n missing at the end of html

@bloodboilAaromatic,

It works fine. Here is my smaple code and please find attached the output Excel file.

Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
        
// Define HTML string
String htmlString1 = "<table style=\"width: 100%\">\n<tbody><tr>\n<th>1</th>\n<th>2</th>\n<th>3</th>\n</tr>\n<tr>\n<td><div>2025-03-05T11:50:21.454+03:00</div></td>\n<td><div>22</div></td>\n<td></td>\n</tr>\n<tr>\n<td></td>\n<td></td>\n<td></td>\n</tr>\n</tbody></table>";
worksheet.Cells["B2"].HtmlString = htmlString1;

String htmlString2 = "<table>\n" +
                "  <tr>\n" +
                "    <th>name</th>\n" +
                "    <th>current date</th>\n" +
                "    <th>variable</th>\n" +
                "  </tr>\n" +
                "  <tr>\n" +
                "    <td><div class='output-control-result-compiled'><div class=\"output-control-container\"><div class=\"simple-text\">ALRT-23-10-908</div></div></div></td>\n" +
                "    <td><div class='output-control-result-compiled'><div class=\"output-control-container\"><div class=\"simple-text\">2025-03-04T15:47:19.581+03:00</div></div></div></td>\n" +
                "    <td><div class='output-control-result-compiled'><div class=\"output-control-container\"><div>123</div></div></div></td>\n" +
                "  </tr>\n" +
                "  <tr>\n" +
                "    <td>Centro comercial Moctezuma</td>\n" +
                "    <td>Francisco Chang</td>\n" +
                "    <td>Mexico</td>\n" +
                "  </tr>\n" +
                "</table>";
worksheet.Cells["C2"].HtmlString = htmlString2;

// Save the workbook
workbook.Save("e:\\test2\\output1.xlsx");

output1.zip (6.1 KB)

Give me example code (same as above) so we could evaluate it.

Yeah
24.10 error is reproduced
updated to last stable: 25.2.0 - no issues found related to this thread

@bloodboilAaromatic,

Thanks for your feedback and providing further details.

Please try using latest version (Aspose.Cells for .NET v25.2) as it has more enhancements and fixes.