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

Free Support Forum - aspose.com

Cells.ImportDataTable Question

Hello,

I am using the Cells.ImportDataTable function to import data into my spreadsheet… I noticed that there was an input parameter called “isFieldNameShown”… When I set it to “True”, it only shows the fields names if the data table I am trying to import has 1 or more rows… I would prefer the column names to be displayed regardless… Is there any way to do this???

Thanks,

-Dana

Dear Dana,

Aspose.Excel will support this feature in next hotfix. Thanks for your patience.

Now you can try this workaround.

if(dt.Rows.Count == 0)
{
for(int i = 0; i < dt.Columns.Count; i ++)
{
string columnName = dt.Columns[i].ColumnName;
cells[0, i].PutValue(columnName);
}
}
else
{
cells.ImportDataTable(…);
}

Thanks a lot…

I really appreciate you guys making an effort to upgrade your product…

-Dana

@cyrus10101,
Aspose.Excel is discontinued and replaced with a latest product Aspose.Cells which contains all the advanced features of MS Excel. You can use this new product also for importing data tables as shown in the following sample code:

//Instantiating a Workbook object
Workbook workbook = new Workbook();
 
//Adding a new worksheet to the Workbook object
int i = workbook.Worksheets.Add();
 
//Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
//Instantiating a "Products" DataTable object
DataTable dataTable = new DataTable("Products");
 
//Adding columns to the DataTable object
dataTable.Columns.Add("Product ID", typeof(Int32));
dataTable.Columns.Add("Product Name", typeof(string));
dataTable.Columns.Add("Units In Stock", typeof(Int32));
 
//Creating an empty row in the DataTable object
DataRow dr = dataTable.NewRow();
 
//Adding data to the row
dr[0] = 1;
dr[1] = "Aniseed Syrup";
dr[2] = 15;
 
//Adding filled row to the DataTable object
dataTable.Rows.Add(dr);
 
//Creating another empty row in the DataTable object
dr = dataTable.NewRow();
 
//Adding data to the row
dr[0] = 2;
dr[1] = "Boston Crab Meat";
dr[2] = 123;
 
//Adding filled row to the DataTable object
dataTable.Rows.Add(dr);
 
//Importing the contents of DataTable to the worksheet starting from "A1" cell,
//where true specifies that the column names of the DataTable would be added to
//the worksheet as a header row
worksheet.Cells.ImportDataTable(dataTable, true, "A1");
workbook.Save("Import From Data Table.xls");

For more information on opening different versions of Excel files, please follow the link below:
Importing from DataTable

Download the latest version of Aspose.Cells for .NET from the following link:
Aspose.Cells for .NET (Latest Version)

You can download the latest demos here.