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

Free Support Forum - aspose.com

Populate Table with Smart Marker

I have a template that I want to populate a table using smart markers. Unfortunately, when using the smart marker in the table it inserts rows in the entire sheet which damages other rows outside of the table.

Using the “noadd” option prevents the other tables from being damaged, but it does not expand the table range so it does not solve the issue.

Please advise how to accomplish this.

private class SampleData
{
	public string Col1 { get; set; }
	public string Col2 { get; set; }
	public string Col3 { get; set; }
}

private static string tableSample()
{
	WorkbookDesigner wbd = new WorkbookDesigner();

	var templatePath = @"table_sample.xlsx";
	var testPath = $@"table_sample_{DateTime.Now.ToString("ddMMMyy_hhmmss")}.xlsx";

	var testList = new List<SampleData>();

	for (int i = 0; i < 5; i++)
	{
		testList.Add(new SampleData
		{
			Col1 = i.ToString(),
			Col2 = i.ToString(),
			Col3 = i.ToString()
		});
	}

	Workbook workbook = new Workbook(templatePath);
	wbd.Workbook = workbook;

	wbd.SetDataSource("Test", testList);
	wbd.Process();
	wbd.Workbook.Save(testPath);
	return testPath;
}

actual.png (3.2 KB)
template.PNG (2.6 KB)
desired.PNG (2.7 KB)

@derek.jarvis,

Thanks for the sample code and screenshots.

Well, you need to perform two things so, the markers in the underlying table do not disturb other sections or tables:

  1. You will use “noadd” when specifying the markers, see the screenshot of the template file:
    https://i.imgur.com/hdNKXUt.png

  2. You have to resize your underlying ListObject/Table accordingly based on number of records you are adding. I have added a few lines to your code. See the following code segment (in bold) for your reference:
    e.g
    Sample code:


var testList = new List();

        for (int i = 0; i < 5; i++)
            	{
            		testList.Add(new SampleData
                    		{
                                			Col1 = i.ToString(),
                                          	Col2 = i.ToString(),
                                            Col3 = i.ToString()
            });

}

Workbook workbook = new Workbook(templatePath);
wbd.Workbook = workbook;

//Get the first worksheet in the workbook.
Worksheet worksheet = workbook.Worksheets[0];
//Resize the ListObject/Table based on number of records you are adding to the list
worksheet.ListObjects[0].Resize(0, 0, 5, 2, true);

wbd.SetDataSource(“Test”, testList);
wbd.Process();
wbd.Workbook.Save(“e:\test2\out1.xlsx”);

See the screenshot of the output file for your reference:

Hope, this helps a bit.

Thanks for your clear and prompt response!