C#word文件未打开
我试图从word文件中读取一个文本,然后替换其中的一些文本,然后用c#中相同格式的另一个名称保存它。但是当我打开生成的文档时,它不是用ms word打开的,而是用notepad++打开的。我要word文件。代码有什么问题? 提前谢谢C#word文件未打开,c#,.net,C#,.net,我试图从word文件中读取一个文本,然后替换其中的一些文本,然后用c#中相同格式的另一个名称保存它。但是当我打开生成的文档时,它不是用ms word打开的,而是用notepad++打开的。我要word文件。代码有什么问题? 提前谢谢 string path = Server.MapPath("~/CustomerDocument/SampleNDA4.docx"); TextExtractor extractor = new TextExtractor(path);
string path = Server.MapPath("~/CustomerDocument/SampleNDA4.docx");
TextExtractor extractor = new TextExtractor(path);
string text = extractor.ExtractText();
//System.IO.File.Create(path).Close();
text = text.Replace("<var_Date>", DateTime.Now.ToString("MMM dd,yyyy"));
//System.IO.File.Create(Server.MapPath("~/CustomerDocument/GeneratedNDA3.docx"));
System.IO.File.WriteAllText(Server.MapPath("~/CustomerDocument/GeneratedNDA3.docx"), text);
stringpath=Server.MapPath(“~/CustomerDocument/SampleNDA4.docx”);
TextExtractor提取器=新的TextExtractor(路径);
string text=提取器.ExtractText();
//System.IO.File.Create(path.Close();
text=text.Replace(“,DateTime.Now.ToString”(“MMM dd,yyyy”);
//System.IO.File.Create(Server.MapPath(“~/CustomerDocument/GeneratedNDA3.docx”);
System.IO.File.writealText(Server.MapPath(“~/CustomerDocument/GeneratedNDA3.docx”),text);
您可以使用Microsoft.Office.Interop.Word库将文档另存为Word文件。请检查以下代码是否适用于您:
object oMissing = System.Reflection.Missing.Value;
object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */
//Start Word and create a new document.
Microsoft.Office.Interop.Word._Application oWord;
Microsoft.Office.Interop.Word._Document oDoc;
oWord = new Microsoft.Office.Interop.Word.Application();
oWord.Visible = false;
oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
oDoc.Content.SetRange(0, 0);
oDoc.Content.Text = "String text;
object filename = @"c:\temp1.docx";
oDoc.SaveAs2(ref filename);
oDoc.Close(ref oMissing, ref oMissing, ref oMissing);
oDoc = null;
oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
oWord = null;
注意:MS Word应安装在执行此代码的服务器上。根据您的要求考虑以下选项。所有选项都有一个要安装在项目上的nuget包
使用(WordprocessingDocument myDocument=WordprocessingDocument.Open(@“C:\Git\source.docx”,true))
{
foreach(myDocument.MainDocumentPart.Document.Body.Elements()中的段落objParagraph)
{
字符串innerText=objParagraph.innerText;
string modifiedString=innerText.Replace(“,DateTime.Now.ToString(“MMM-dd,yyyy”);
如果(modifiedString!=innerText)
{
运行newRun=新运行();
AppendChild(新文本(modifiedString));
objParagraph.RemoveAllChildren();
objParagraph.AppendChild(newRun);
}
}
}
Application app = new Application();
Document doc = app.Documents.Open(@"C:\Git\source.docx");
foreach (Paragraph objParagraph in doc.Paragraphs)
{
objParagraph.Range.Text = objParagraph.Range.Text.Trim().Replace("<var_Date>", DateTime.Now.ToString("MMM dd,yyyy")) + "\r\n";
}
doc.SaveAs2(@"C:\Git\finalFile.docx");
doc.Close();
app.Quit();
Application-app=新应用程序();
Document doc=app.Documents.Open(@“C:\Git\source.docx”);
foreach(文件段落中的段落objParagraph)
{
objParagraph.Range.Text=objParagraph.Range.Text.Trim().Replace(“,DateTime.Now.ToString”(“mm-dd,yyyy”)+“\r\n”;
}
doc.SaveAs2(@“C:\Git\finalFile.docx”);
doc.Close();
app.Quit();
- 需要在服务器上安装MSOffice
- 它有点慢,所以不适合高使用率的场景
FindReplaceOptions=newfindreplaceoptions(){MatchCase=true};
Document doc=新文档(@“C:\Git\source.docx”);
doc.Range.Replace(“”,DateTime.Now.ToString(“MMM-dd,yyyy”),选项);
doc.Save(@“C:\Git\finalFile.docx”);
- Aspose需要付费许可证,但在服务器上使用相对更快
Word文档不是文本。您不能这样做。请尝试使用openxml库Word文档不是文本。使用
writealText
创建一个是行不通的。你需要使用类似Office对象模型的东西:除非你将默认应用程序设置为notepad++for doc(x)文件,否则你不能这样做。我的意思是*.zip而不是*.xml:-(
Application app = new Application();
Document doc = app.Documents.Open(@"C:\Git\source.docx");
foreach (Paragraph objParagraph in doc.Paragraphs)
{
objParagraph.Range.Text = objParagraph.Range.Text.Trim().Replace("<var_Date>", DateTime.Now.ToString("MMM dd,yyyy")) + "\r\n";
}
doc.SaveAs2(@"C:\Git\finalFile.docx");
doc.Close();
app.Quit();
FindReplaceOptions options = new FindReplaceOptions() { MatchCase = true };
Document doc = new Document(@"C:\Git\source.docx");
doc.Range.Replace("<var_Date>", DateTime.Now.ToString("MMM dd,yyyy"), options);
doc.Save(@"C:\Git\finalFile.docx");