C # problems with. Replace () in an excel sheet

0

I am trying to remove the commas from a cell inside an excel file, but I have the situation that after applying my code in the first row everything is fine I have no problem, but from the second round my code will be stop completely in the same line where I apply the Replace() . It does not send me an error, it simply freezes and stays loaded forever. The variable x13 I use to know that if the method removes the commas and everything goes well, until the next round of for .

Application excel = new Application();
Workbook sheet = excel.Workbooks.Open(filePatht);
Worksheet x = excel.ActiveSheet as Worksheet;
_Worksheet xlWorksheet = sheet.Sheets[1];
Excel.Range xlRange = xlWorksheet.UsedRange;

int rowCount = xlRange.Rows.Count;
for (int ix = 12; ix < rowCount; ix++) {

  //xlRange.NumberFormat = "0.00";

  xlRange.Cells[ix, 2].Replace(",", " ");
  var x13 = xlRange.Cells[ix, 2].Text;
  xlRange.Cells[ix, 4].NumberFormat = "0.00";
  xlRange.Cells[ix, 5].NumberFormat = "0.00";
  xlRange.Cells[ix, 6].NumberFormat = "0.00";
}

sheet.Save();

Here is an image of the cell I am trying to modify.

    
asked by E.Rawrdríguez.Ophanim 29.08.2018 в 18:32
source

1 answer

0

ok I solved it in this way, if someone has a better or some observation, you are welcome to post it.

for (int ix = 12; ix <= rowCount; ix++) {


  var x12 = xlRange.Cells[ix, 2].Text;
  x12 = x12.Replace(",", " ");
  xlRange.Cells[ix, 2].Value = x12;
  var x13 = xlRange.Cells[ix, 2].Text;
  xlRange.Cells[ix, 4].NumberFormat = "0.00";
  xlRange.Cells[ix, 5].NumberFormat = "0.00";
  xlRange.Cells[ix, 6].NumberFormat = "0.00";


}
    
answered by 29.08.2018 в 20:24