如何删除excel中每个单元格的回车?

时间:2021-04-26 22:20:39

I use this code for deleting carriage return from every cell in excel:

我使用此代码从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 if your search formatting and criteria are defined correctly. If you are sure that matching data exists in this workbook, it may be on a protected sheet. Excel cannot replace data on a protected worksheet."

“Microsoft Office Excel找不到任何需要替换的数据。检查您的搜索格式和标准是否定义正确。如果您确定该工作簿中存在匹配的数据,那么它可能位于受保护的表上。Excel不能在受保护的工作表上替换数据。

Anybody know how to change carriage return in excel cell?

有人知道如何在excel单元格中更改回车吗?

1 个解决方案

#1


4  

If doing data entry in Excel and you press the Alt+Enter key, it will insert a linefeed character. However, if the input was written to the cell as a Windows line break (CR+LF), it will be displayed in Excel exactly the same way.

如果在Excel中进行数据输入,然后按Alt+Enter键,它将插入一个linefeed字符。但是,如果将输入写入单元格作为Windows换行符(CR+LF),它将以完全相同的方式显示在Excel中。

The safest way to ensure all line breaks are replaced is to cycle through all possibilities and replace each:

确保替换所有换行符的最安全的方法是循环遍历所有的可能性并替换每一个:

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);
}

#1


4  

If doing data entry in Excel and you press the Alt+Enter key, it will insert a linefeed character. However, if the input was written to the cell as a Windows line break (CR+LF), it will be displayed in Excel exactly the same way.

如果在Excel中进行数据输入,然后按Alt+Enter键,它将插入一个linefeed字符。但是,如果将输入写入单元格作为Windows换行符(CR+LF),它将以完全相同的方式显示在Excel中。

The safest way to ensure all line breaks are replaced is to cycle through all possibilities and replace each:

确保替换所有换行符的最安全的方法是循环遍历所有的可能性并替换每一个:

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);
}