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

Free Support Forum - aspose.com

Table formatting error

I am using cells.ImportDataTable() to set 2 tables in a worksheet.

I am getting error when i use,

worksheet.Cells.ImportDataTable(table1, true, "A1") then

worksheet.Cells.ImportDataTable(table2, true, "G1")

I need to insert these tables in the same row. One table inserts the row below the other. Could you help me with this matter.

Kind regards,

Dinesh


This message was posted using Aspose.Live 2 Forum

Hi Dinesh,

For your situation, you should use the overloaded version of ImportDataTable method with insertRows parameter to set to “False”. See the following example below:

[C#]
Workbook excel = new Workbook();
Cells cells = excel.Worksheets[0].Cells;

DataTable dt = new DataTable(“Products”);
dt.Columns.Add(“Product_ID”, typeof(Int32));
dt.Columns.Add(“Product_Name”, typeof(string));
dt.Columns.Add(“Units_In_Stock”, typeof(Int32));
DataRow dr = dt.NewRow();
dr[0] = 1;
dr[1] = “Aniseed Syrup”;
dr[2] = 15;
dt.Rows.Add(dr);

dr = dt.NewRow();
dr[0] = 2;
dr[1] = “Boston Crab Meat”;
dr[2] = 123;
dt.Rows.Add(dr);


DataTable dt2 = new DataTable(“Products”);
dt2.Columns.Add(“ID”, typeof(Int32));
dt2.Columns.Add(“Name”, typeof(string));
dt2.Columns.Add(“Units”, typeof(Int32));
dr = dt2.NewRow();
dr[0] = 121;
dr[1] = “ABC”;
dr[2] = 215;
dt2.Rows.Add(dr);

dr = dt2.NewRow();
dr[0] = 234;
dr[1] = “BCM”;
dr[2] = 423;
dt2.Rows.Add(dr);



cells.ImportDataTable(dt, true, 0, 0, false);
cells.ImportDataTable(dt2, true,0, 6, false);


excel.Save(“e:\test\Book1.xls”);


[VB]

Dim excel As New Workbook()
Dim cells As Cells = excel.Worksheets(0).Cells

Dim dt As New DataTable(“Products”)
dt.Columns.Add(“Product_ID”, GetType(Int32))
dt.Columns.Add(“Product_Name”, GetType(String))
dt.Columns.Add(“Units_In_Stock”, GetType(Int32))
Dim dr As DataRow = dt.NewRow()
dr(0) = 1
dr(1) = “Aniseed Syrup”
dr(2) = 15
dt.Rows.Add(dr)

dr = dt.NewRow()
dr(0) = 2
dr(1) = “Boston Crab Meat”
dr(2) = 123
dt.Rows.Add(dr)


Dim dt2 As New DataTable(“Products”)
dt2.Columns.Add(“ID”, GetType(Int32))
dt2.Columns.Add(“Name”, GetType(String))
dt2.Columns.Add(“Units”, GetType(Int32))
dr = dt2.NewRow()
dr(0) = 121
dr(1) = “ABC”
dr(2) = 215
dt2.Rows.Add(dr)

dr = dt2.NewRow()
dr(0) = 234
dr(1) = “BCM”
dr(2) = 423
dt2.Rows.Add(dr)



cells.ImportDataTable(dt, True, 0, 0, False)
cells.ImportDataTable(dt2, True,0, 6, False)


excel.Save(“e:\test\Book1.xls”)


Thank you.