Free Support Forum - aspose.com

Adjacent tables in AsPose Cells


#1

Hi there,
Below is a code spinet (in C#) for creating two adjacent tables.
Please note that the first table has no rows.
The code runs fine if (any) single table is generated only,
that is if you comment out code for “topTable” or “bottomTable” the resulting Excel file is fine.
However, if both tables are created then styling is messed up and the file becomes corrupt.
(It still opens though just style is bad).
Is the problem caused by the fact that the “topTable” has not rows?
Is there any way to achieve this (that is, two adjacent tables with one of them without rows) by using different syntax?
Of course I could “simulate” a table header manually in case there are no rows, but I would rather
use table style enumeration via “TableStyleType” property.
Thank you for your help.

Code:
Workbook wb = new Workbook();
Worksheet ws = wb.Worksheets[0];

        //topTable
        //ws.Cells[2, 2].PutValue("First Name");
        //ws.Cells[2, 3].PutValue("Last Name");
        //ws.Cells[2, 4].PutValue("State");
        //var topTable = ws.ListObjects[ws.ListObjects.Add(2, 2, 2, 4, true)];
        //topTable.TableStyleType = TableStyleType.TableStyleLight8;

        //bottomTable
        ws.Cells[3, 2].PutValue("First Name");
        ws.Cells[3, 3].PutValue("Last Name");
        ws.Cells[3, 4].PutValue("State");
        ws.Cells[4, 2].PutValue("John");
        ws.Cells[4, 3].PutValue("Doe");
        ws.Cells[4, 4].PutValue("USA");
        var bottomTable = ws.ListObjects[ws.ListObjects.Add(3, 2, 4, 4, true)];
        bottomTable.TableStyleType = TableStyleType.TableStyleLight8;

        var myDir = @"yourDirHere";
        var aformat = Aspose.Cells.SaveFormat.Xlsx;
        wb.Save(myDir + "TestAsPoseExcel_5.xlsx", aformat);

#2

@bogdanw,

Thanks for your query.

Your code was creating table where one row was overlapping. You may please try following sample code and provide your feedback.

Workbook wb = new Workbook();
Worksheet ws = wb.Worksheets[0];
//topTable
ws.Cells[2, 2].PutValue("First Name");
ws.Cells[2, 3].PutValue("Last Name");
ws.Cells[2, 4].PutValue("State");
var topTable = ws.ListObjects[ws.ListObjects.Add(2, 2, 2, 4, true)];
topTable.TableStyleType = TableStyleType.TableStyleLight8;

//bottomTable
ws.Cells[4, 2].PutValue("First Name");
ws.Cells[4, 3].PutValue("Last Name");
ws.Cells[4, 4].PutValue("State");
ws.Cells[5, 2].PutValue("John");
ws.Cells[5, 3].PutValue("Doe");
ws.Cells[5, 4].PutValue("USA");
var bottomTable = ws.ListObjects[ws.ListObjects.Add(4, 2, 5, 4, true)];
bottomTable.TableStyleType = TableStyleType.TableStyleLight8;

var myDir = @"";
var aformat = Aspose.Cells.SaveFormat.Xlsx;
wb.Save(myDir + "TestAsPoseExcel_5.xlsx", aformat);

#3

No.
The code was intended to be like that …
I wanted the first table to have no rows (only header)
and the second table to follow directly the first one.
It appears that a table has to have at least one row with data?!


#4

@bogdanw,

Could you please share template file created with MS Excel and then we will analyse your requirements further.


#5

@bogdanw,

I have tested your scenario/ case a bit in MS Excel manually. It seems at least two rows are needed when you create a table or List object in MS Excel (one for header row and other for details section), you may confirm this in MS Excel. As your second table is overriding first one, so you are getting this issue. Moreover, I tried to add second table on 4th row (which is actually the details section of first table), it does not work good. Also, I tried to insert the table starting from 5th row and then tried to remove 4th row, this also does not work. The best way to cope with it (as Ahsan Iqbal mentioned), you should make sure both tables should not override. You may insert the second table starting from 5th row. Finally you may hide the details section of the first table (4th row) for your needs, see the sample code for your reference:
e.g
Sample code:

Workbook wb = new Workbook();
Worksheet ws = wb.Worksheets[0];

    //topTable
    ws.Cells[2, 2].PutValue("First Name");
    ws.Cells[2, 3].PutValue("Last Name");
    ws.Cells[2, 4].PutValue("State");
    var topTable = ws.ListObjects[ws.ListObjects.Add(2, 2, 2, 4, true)];
    topTable.TableStyleType = TableStyleType.TableStyleLight8;
    
    //bottomTable
    ws.Cells[4, 2].PutValue("First Name");
    ws.Cells[4, 3].PutValue("Last Name");
    ws.Cells[4, 4].PutValue("State");
    ws.Cells[5, 2].PutValue("John");
    ws.Cells[5, 3].PutValue("Doe");
    ws.Cells[5, 4].PutValue("USA");
    var bottomTable = ws.ListObjects[ws.ListObjects.Add(4, 2, 5, 4, true)];
    bottomTable.TableStyleType = TableStyleType.TableStyleLight8;

//Hide the 4th row
ws.Cells.HideRow(3);

If this does not work for your needs, as requested earlier, kindly share a sample file created in MS Excel manually to demonstrate on how to accomplish the task, we will check it soon.


#6

OK, so this is how it works in Excel - a table must have at least one row.
Even if there is no data there must be an empty row below header.
Weird, but I guess we can not do anything about it.
Thank you for your help.


#7

@bogdanw,

Yes, your understanding is correct. Feel free to contact us any time if you need further help or have some other issue or queries, we will be happy to assist you soon.