Class ListObjectCollection
Represents collection of table in the worksheet.
Inheritance
Implements
Namespace: Syncfusion.XlsIO.Implementation.Tables
Assembly: Syncfusion.XlsIO.Base.dll
Syntax
public class ListObjectCollection : List<IListObject>, IListObjects, IList<IListObject>, ICollection<IListObject>, IEnumerable<IListObject>, IEnumerable
Constructors
ListObjectCollection(WorksheetImpl)
Initializes a new instance of ListObjectCollection class.
Declaration
public ListObjectCollection(WorksheetImpl sheet)
Parameters
Type | Name | Description |
---|---|---|
WorksheetImpl | sheet | Sheet to which this collection belongs to. |
Properties
Item[String]
Returns table object with the given name. Read-only.
Declaration
public IListObject this[string name] { get; }
Parameters
Type | Name | Description |
---|---|---|
System.String | name | Name of the table. |
Property Value
Type | Description |
---|---|
IListObject | Table object with the given name. |
Methods
AddDefineName(IListObject)
Adds the table name to the defined names in the workbook.
Declaration
public void AddDefineName(IListObject list)
Parameters
Type | Name | Description |
---|---|---|
IListObject | list | Table object. |
AddEx(ExcelListObjectSourceType, IConnection, IRange)
Creates a query table with specified connection and its source type. Adds it to the IListObjects collection.
Declaration
public IListObject AddEx(ExcelListObjectSourceType type, IConnection connection, IRange Destinaion)
Parameters
Type | Name | Description |
---|---|---|
ExcelListObjectSourceType | type | Represents connection source type. |
IConnection | connection | Represents the connection of the table. |
IRange | Destinaion |
Returns
Type | Description |
---|---|
IListObject | Returns a List object with the specified connections. |
Examples
Following code illustrates how to create a table with an external connections.
ExcelEngine excelEngine = new ExcelEngine();
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet worksheet = workbook.Worksheets[0];
// Database path
string dataPath = Path.GetFullPath(@"c:\company\DB\TestDB.mdb");
// Connection string for DataSource
string ConnectionString = "OLEDB;Provider=Microsoft.JET.OLEDB.4.0;Password=\"\";User ID=Admin;Data Source=" + dataPath;
// Adding a connection to the workbook
IConnection Connection = workbook.Connections.Add("Connection1", "Sample connection with MsAccess", ConnectionString, "", ExcelCommandType.Sql);
// Adding a QueryTable to sheet object
worksheet.ListObjects.AddEx(ExcelListObjectSourceType.SrcQuery, Connection, worksheet.Range["A1"]);
// Command Text for the Connection
worksheet.ListObjects[0].QueryTable.CommandText = "Select * from tblTest";
// The Query performs Asynchronous action
worksheet.ListObjects[0].QueryTable.BackgroundQuery = true;
// The Query Table is refreshed when the Workbook is opened
worksheet.ListObjects[0].QueryTable.RefreshOnFileOpen = true;
// Represents the connection description
Connection.Description = "Sample Connection";
// Import data to the sheet from the database
worksheet.ListObjects[0].Refresh();
// Auto-fits the columns
worksheet.UsedRange.AutofitColumns();
string fileName = "Output.xlsx";
workbook.SaveAs(fileName);
workbook.Close();
excelEngine.Dispose();
Create(String, IRange)
Creates a table with specified name and its data range. Adds it to the IListObjects collection.
Declaration
public IListObject Create(string name, IRange range)
Parameters
Type | Name | Description |
---|---|---|
System.String | name | Represents the name of the table. |
IRange | range | Represents the data range of the table. |
Returns
Type | Description |
---|---|
IListObject | Returns a List object with the specified name and range. |
Examples
Following code illustrates how to create a table.
ExcelEngine excelEngine = new ExcelEngine();
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Excel2013;
IWorkbook workbook = application.Workbooks.Open("Sample.xlsx", ExcelOpenType.Automatic);
IWorksheet worksheet = workbook.Worksheets[0];
// Create Table with data in the given range
IListObject table = worksheet.ListObjects.Create("Table1", worksheet["A1:C8"]);
string fileName = "Output.xlsx";
workbook.SaveAs(fileName);
workbook.Close();
excelEngine.Dispose();
Remove(IListObject)
Removes a table from the worksheet.
Declaration
public bool Remove(IListObject listObject)
Parameters
Type | Name | Description |
---|---|---|
IListObject | listObject | ListObject to be removed |
Returns
Type | Description |
---|---|
System.Boolean | true if table is successfully removed; otherwise, false. |
Examples
Following code illustrates how to remove a table from the worksheet.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
IWorkbook workbook = application.Workbooks.Open("Sample.xlsx");
IWorksheet worksheet = workbook.Worksheets["Database"];
IListObjects listObjects = worksheet.ListObjects;
IListObject table = listObjects[0];
listObjects.Remove(table);
workbook.SaveAs("output.xlsx");
}
RemoveAt(Int32)
Removes a table from the worksheet at the specified index.
Declaration
public void RemoveAt(int index)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | index | Index of the ListObject to be removed |
Examples
Following code illustrates how to remove a table from the worksheet from using specified index.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
IWorkbook workbook = application.Workbooks.Open("Sample.xlsx");
IWorksheet worksheet = workbook.Worksheets["Database"];
IListObjects listObjects = worksheet.ListObjects;
listObjects.RemoveAt(0);
workbook.SaveAs("output.xlsx");
}