Skip to main content
 首页 » 编程设计

c#之如何在Word中的表格单元格中插入换行符

2024年02月24日28jyk

当我使用 C# 将 '\r\n' 插入到 word 文档中表格的单元格中时,它不起作用,为什么?怎么做?

Word.Application wordApp;  
Word.Document wordDoc;  
filePath = @"" + strWorkPath + @"Uploads\Template\template.doc";  
saveFilePath = @"" + strWorkPath + @"Uploads\" + VersionStr + @"\" + fileName;  
object format = Word.WdSaveFormat.wdFormatDocument; wordApp = new Word.Application();  
Object nothing = Missing.Value;  
wordDoc = wordApp.Documents.Open(ref filePath, ref nothing, ref nothing, ref nothing, ref nothing,ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing);  
Word.Table table = wordDoc.Tables[1];  
table.Cell(1, 1).Range.Text = "sdsdlkfjdslk \r\n slkdfjslj";  

如最后一行所示。

文档中是"sdsdlkfjdslk\r\n slkdfjslj",不是"sdsdlkfjdslk slkdfjslj"

请您参考如下方法:

尝试

"sdsdlkfjdslk" + (char)11 + "slkdfjslj"  

或

"sdsdlkfjdslk \v slkdfjslj"