当我使用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"
答案 0 :(得分:3)
尝试
"sdsdlkfjdslk" + (char)11 + "slkdfjslj"
或
"sdsdlkfjdslk \v slkdfjslj"
答案 1 :(得分:0)
(char)11和\ v对我不起作用。我收到“十六进制值0x0B,是一个无效字符”。以下解决方案为我做了诀窍。
Paragraph paragraph = new Paragraph();
string[] lines = Value.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
Run run = new Run(new Text(lines[0]));
if (lines.Length > 1)
{
run.AppendChild(new Break());
run.AppendChild(new Text(lines[1]));
}
paragraph.Append(run);
我最后保留字符串,示例中的变量Value,以“\ n”作为换行符进入单元格。我将字符串拆分为换行符中的数组。我的代码中只有一次中断的可能性,但如果你有多个换行符,你可以使用foreach循环。