Hi Varsha,
Thank you for explaining the scenario further.
As discussed earlier, you must have a mapping mechanism to map the columns from DataTable to the Worksheet in order to get the correct results. Moreover, this feature has to be implemented outside the Aspose.Cells for .NET APIs because these APIs do not provide such facility.
Please check the attached spreadsheets and the following piece of code. Please note, the code first extracts the column names from the source DataTable and build a mapping using the Dictionary. Based on the mapping, the Aspose.Cells APIs import only the columns that are present in the source Workbook (book1.xlsx) in same sequence as they occur.
Dim table As New DataTable()
table.Columns.Add(“Id”, GetType(Integer))
table.Columns.Add(“Patient”, GetType(String))
table.Columns.Add(“Father Name”, GetType(String))
table.Columns.Add(“Address”, GetType(String))
table.Columns.Add(“Age”, GetType(Integer))
table.Columns.Add(“Dosage”, GetType(Integer))
table.Columns.Add(“Drug”, GetType(String))
table.Columns.Add(“Date”, GetType(Date))
table.Rows.Add(1, “David”, “Mike”, “123, Paris Road”, 36, 12, “Incidol”, Date.Now)
table.Rows.Add(3, “Harrison”, “Ford”, “12, Parkin Road”, 24, 1, “Rupar”, Date.Now)
table.Rows.Add(4, “Jessica”, “Jones”, “209, Silver line Street”, 22, 4, “Mxzim”, Date.Now)
table.Rows.Add(6, “David”, “Harp”, “2, Kingston Street”, 45, 1, “Disprin”, Date.Now)
table.Rows.Add(7, “John”, “Doe”, “23, Kings Road”, 11, 3, “Febrol”, Date.Now)
table.Rows.Add(9, “Jenny”, “Leon”, “123, South Alberta”, 34, 2, “Novidet”, Date.Now)
Dim dict As Dictionary(Of Integer, String) = New Dictionary(Of Integer, String)()
Dim index As Integer = 0
For Each tableColumn As DataColumn In table.Columns
dict(index) = tableColumn.ColumnName
index += 1
Next tableColumn
Dim book = New Workbook(Dir & “book1.xlsx”)
Dim sheet = book.Worksheets(0)
Dim cells = sheet.Cells
Dim indices(cells.MaxDataColumn) As Integer
Dim i As Integer = 0
Do While i < cells.MaxDataColumn + 1
Dim columnHeaderName As String = cells(0, i).StringValue
Dim keyMatch = dict.Where(Function§ p.Value = columnHeaderName).Select(Function§ p.Key)
For Each key As Integer In keyMatch
Dim colunmIndex As Integer = CInt(key)
indices(i) = colunmIndex
Next key
i += 1
Loop
Dim importOptions = New ImportTableOptions()
importOptions.IsFieldNameShown = False
importOptions.ColumnIndexes = indices
book.Worksheets(0).Cells.ImportData(table, 1, 0, importOptions)
book.Save(Dir & “output2.xlsx”)
Please note, the code shared above is for demonstration purposes. You may amend it to suit your application requirements.