How to remove carriage return to each cell in excel?

I use this code to remove carriage returns from each cell in excel:

 Microsoft.Office.Interop.Excel.Range cells = reportSheet.Cells;
 cells.Replace("\n", "",
               Microsoft.Office.Interop.Excel.XlLookAt.xlWhole,
               Microsoft.Office.Interop.Excel.XlSearchOrder.xlByRows, false,
               false, true, false);

I get a message:

"Microsoft Office Excel cannot find any data to replace. Check that search formatting and criteria are defined correctly. If you are sure that the relevant data exists in this book, it can be protected by a worksheet. Excel cannot replace data by a protected worksheet.

Does anyone know how to change carriage return to excel cell?

+4
source share
1 answer

Excel Alt + Enter, . , Windows (CR + LF), Excel .

, , - :

Microsoft.Office.Interop.Excel.Range cells = reportSheet.Cells;
// Cycle through each newline code and replace.
foreach (var newline in new string[] { "\r\n", "\r", "\n" })
{
    cells.Replace(newline, "",
                  Microsoft.Office.Interop.Excel.XlLookAt.xlPart, // Use xlPart instead of xlWhole.
                  Microsoft.Office.Interop.Excel.XlSearchOrder.xlByRows, false,
                  false, true, false);
}
+3

Source: https://habr.com/ru/post/1570779/


All Articles