Read data into datatable

  • Thread starter Thread starter VA_er
  • Start date Start date
V

VA_er

Guest
I feel that my question is more about C#, although it deals with Excel, but it is not VBA question.

I don't know why some of my threads had been moved to Excel forum (I feel there are much fewer viewers there), maybe I should remove keyword Excel from thread title.

Here is my original thread, which was posted here then moved to Excel forum. How should I incorporate below logic into the sample code (It is just the logic I want to go with, the syntax is definitely wrong).

Thank you very much!

for (int i=1; i <= colCount; i++)
{
if (string) (xlWorksheet.Cells[1, i]) = "store_key"
table.Columns.Add("store_key", typeof(int));
elseif (string) (xlWorksheet.Cells[1, i]) = "region_key"
table.Columns.Add("region_key", typeof(int));
elseif(string)(xlWorksheet.Cells[1, i]) = "store_name"
table.Columns.Add("store_name", typeof(string));
elseif(string)(xlWorksheet.Cells[1, i]) = "store_type"
table.Columns.Add("store_type", typeof(string));
elseif(string)(xlWorksheet.Cells[1, i]) = "street_address"
table.Columns.Add("street_adress", typeof(string));
elseif(string)(xlWorksheet.Cells[1, i]) = "city"
table.Columns.Add("city", typeof(string));
else(string)(xlWorksheet.Cells[1, i]) = "state_province"
table.Columns.Add("state_province", typeof(string));

}


for (int i = 2; i <=rowCount; i++)
{
table.Rows.Add(xlRange.Cells[i, 1].Value, xlRange.Cells[i, 2].Value, xlRange.Cells[i, 3].Value, xlRange.Cells[i, 4].Value, xlRange.Cells[i, 5].Value, xlRange.Cells[i, 6].Value, xlRange.Cells[i, 7].Value);
}



1418984.png

Continue reading...
 
Back
Top