I know my question may seem common. There are lot of knowledge shared out there in this forum itself. But I'm unable to get solution for my particular requirement.
In my case I have an excel workbook(ver 2016) contains 10 sheets with 1500 rows in each. Column may vary from 15 to 20 in each sheets. I want to read all the data from all the sheets and validate their datatype and insert it into a sql server database table.
But I've tried with 2 sheets with 100 rows in each.
Excel.Worksheet ofWs;
Excel.Range range;
ofWs = (Excel.Worksheet)ofShe.get_Item("Sales");
range = ofWs.UsedRange;
for (int i = 2; i <= range.Rows.Count; i++)
{
string var1 = "";
double var2 = 0;
//validation for column1
if ((ofWs.Cells[i, 1] as Excel.Range).Value2 != null)
{
if ((ofWs.Cells[i, 1] as Excel.Range).Value2.GetType().ToString() == "System.String")
var1 = (string)(ofWs.Cells[i, 1] as Excel.Range).Value2;
else
{
sale_comm_column += "COLUMN A, ";
sale_errFlag = false;
}
}
else
{
sale_comm_column += "COLUMN A, ";
sale_errFlag = false;
}
//validation for column2
if ((ofWs.Cells[i, 2] as Excel.Range).Value2 != null)
{
if ((ofWs.Cells[i, 2] as Excel.Range).Value2.GetType().ToString() == "System.Double")
var2 = (double)(ofWs.Cells[i, 2] as Excel.Range).Value2;
else
{
sale_comm_column += "COLUMN B, ";
sale_errFlag = false;
}
}
else
{
sale_comm_column += "COLUMN B, ";
sale_errFlag = false;
}
}
This for loop will thru all the rows and I'm validating each column in "if" statement. Here I've shown the validation part of 2 columns of 1st sheet only. For 100 rows itself it's taking too much time. However if I remove all these "if", it's taking less time. If I want to try this on my actual requirement of 10 sheets with 1500 rows in each, what is the best way accomplish this??