gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 23:14:15 26 4
gpt4 key购买 nike

我使用此代码从 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);

我收到一条消息:

"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."

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

最佳答案

如果在 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);
}

关于c# - 如何删除excel中每个单元格的回车?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27889664/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com