Advice: learn to properly indent your code, it helps reading as it shows the structure.
int i=0
string str1=BarCode[i].substring[0,1];
if(str1=="R")
{
for(int j=0; j<i;j++)
{
if(xlworksheet.cells [j+1,2]==null)
{
xlworksheet.cells[j+1,2]=BarCode[i];
break;
else
xlworksheet.cells[j+2,2]=BarCode[i];
}
}
else if (str=="S")
{
for(int k=0; k<i; k++)
if(xlworksheet.cells[k+1,3]==null)
{
xlworksheet.cells[k+1,3]=BarCode[i];
break;
}
else
xlworksheet.cells[k+2,3]=BarCode[i];
}
}
}
This do not compile, the structure is wrong.
Advice: do not pack things in a single line of code, any line saved is at the expense of readability.