Exporting columns by index

I'm attempting to export only certain columns into a datatable. Even if I pass the correct indexs (in an array) it still only returns the the first n columns (n = length of my array) into my datatable. Below is my code. Please help.

int[] columnsToExport = new int[8];
Workbook wb = new Workbook(strTemplateFileName);
Worksheet ws = wb.Worksheets[0];
int trows = ws.Cells.MaxDataRow;
Cell cell = null

cell = ws.Cells.FindString("Employee ID", null);
columnsToExport[0] = cell.Column;
cell = ws.Cells.FindString("Full Name", null);
columnsToExport[1] = cell.Column;
cell = ws.Cells.FindString("Product Line Code", null);
columnsToExport[2] = cell.Column;
cell = ws.Cells.FindString("Sub Product Line Code", null);
columnsToExport[3] = cell.Column;
cell = ws.Cells.FindString("Location ID", null);
columnsToExport[4] = cell.Column;
cell = ws.Cells.FindString("Department Description", null);
columnsToExport[5] = cell.Column;
cell = ws.Cells.FindString("Position Title", null);
columnsToExport[6] = cell.Column;
cell = ws.Cells.FindString("Reports To", null);
columnsToExport[7] = cell.Column;

ws.Cells.ExportDataTable(dt, 0, columnsToExport, trows, true);

Hi,


I have tested your scenario with my sample template file and code (given below), it works fine. I have also attached my output file here.

Sample code:

DataTable dt = new DataTable();
dt.Columns.Add(“ID”, typeof(string));
dt.Columns.Add(“Class”, typeof(string));


int[] columnsToExport = new int[2];
Workbook wb = new Workbook(“e:\test2\bk_export1.xlsx”);
Worksheet ws = wb.Worksheets[0];
int trows = ws.Cells.MaxDataRow;
Aspose.Cells.Cell cell = null;

FindOptions findOptions = new FindOptions();
findOptions.CaseSensitive = false;
findOptions.LookInType = LookInType.Values;
cell = ws.Cells.Find(“ID”, null, findOptions);
columnsToExport[0] = cell.Column;
cell = ws.Cells.Find(“Class”, null, findOptions);
columnsToExport[1] = cell.Column;
ws.Cells.ExportDataTable(dt, 1, columnsToExport, trows, false);

Aspose.Cells.Worksheet worksheet2 = wb.Worksheets[1];
worksheet2.Cells.ImportDataTable(dt, true, “A1”);
wb.Save(“e:\test2\outputexportedtable.xlsx”);


If you still find the issue, kindly do post your template file and code (as above), we will check your issue soon.

Also, kindly use our latest version/fix: Aspose.Cells for .NET (Latest Version)

Thank you.