打开/编辑/保存.Docx文件 [英] Open/Edit/Save .Docx file
问题描述
您好b $ b
我正在开发一个Web应用程序,我在其中打开模板文件,该文件在temp.Docx中查找标签并将其替换为其值。 />
我使用Microsoft.Office.Interop.Word实现了这个目的。它正在我的本地机器上运行。
但是在服务器上没有安装此应用程序将要部署的MS办公室。
如果我能,请告诉我achivethis任务没有使用Microsoft.Office.Interop.Word和第三方DLL?
以下是我的工作代码
public void GenerateWordReport(System.Data.DataTable dt,String sTempPath)
{
Microsoft.Office.Interop.Word.Application objWordApp = new Microsoft。 Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document objDoc = new Microsoft.Office.Interop.Word.Document();
对象缺失= System.Reflection.Missing.Value;
StringBuilder wordstring = new StringBuilder();
尝试
{
objWordApp.Documents.Open(sTempPath);
objDoc = objWordApp.ActiveDocument;
// MatchCollection bodyTagColl =;
foreach(Regex.Matches中的匹配标记((objDoc.Content).Text,@[[] [[] \\ \\ * * \ w + \ * *。*?\ * * []] []]))
{
String exp =Parent_NAME =' '+ tag.Value.Substring(2,tag.Value.Length - 4).ToString()+'';
DataRow [] foundRows;
foundRows = dt.Select(exp);
// if(foundRows)
if(foundRows.Length> 0)
{
foreach(dataRow dr in foundRows)
{
String Value = dr [Value ] .ToString()+ dr [计量单位]。ToString()。修剪();
if(([[+ dr [Parent_NAME]。ToString() +]])。长度< = 255)
FindnReplace([[+ dr [Parent_NAME]。ToString()+]],Value,objDoc);
else
FindLargeChar([[+ dr [Parent_NAME]。ToString()+]],Value,objDoc);
}
}
其他
{
if(([+ tag.Value)。长度< = 255)
FindnReplace(tag.Value, - ,objDoc);
else
FindLargeChar(tag.Value, - ,objDoc);
}
}
objDoc.Save();
}
catch (例外e)
{
}
终于
{
objWordApp。 Documents.Close(Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges);
objWordApp.Quit();
objWordApp = null;
KillProcess(winword);
}
}
public void FindLargeChar(string tofind,string toreplace,Microsoft.Office.Interop。 Word.Document objDoc)
{
int StartIndex = 0;
int EndIndex = 254;
int length = tofind.Length;
if(tofind.Length> 255)
{
十进制Dec = tofind.Length%255;
FindnReplace(tofind.Substring(StartIndex,EndIndex),toreplace,objDoc );
}
while(EndIndex< = tofind.Length)
{
length = length - 254;
int l = length;
StartIndex = StartIndex + 254;
EndIndex = EndIndex + 254 ;
if(长度> 255)
{
FindnReplace(tofind.Substring(StartIndex,EndIndex) ),,objDoc);
}
其他
{
FindnReplace(tofind.Substring(StartIndex) ,l),,objDoc);
}
}
}
public void KillProcess(string ProcessName)
{
try
{
// Process [] procs = Process.GetProcessesByName(ProcessName);
// foreach(Process.GetProcessesByName中的Process process__1( ProcessName))
// {
// process__1.Kill();
// process__1.WaitForExit();
//}
}
catch(例外情况)
{
}
终于
{
}
}
public void FindnReplace(string tofind,string toreplace,Microsoft.Office.Interop.Word.Document objDoc)
{
// if(toreplace = - 999)然后
// toreplace =None
//结束如果
//''''''''''''EventLog.WriteEntry(在FindnReplace中)
//'''''''''''''EventLog.WriteEntry(查找+ tofind)
int y = 0 ;
int x = 0;
// Dim replace As Object = toreplace.Replace(\\\\ n,Chr(10))
对象格式= Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML;
尝试
{
if((toreplace.Length< 200))
{
objDoc.Content.Find.Execute(FindText:tofind,ReplaceWith:toreplace,Replace:Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll,格式:格式);
}
其他
{
int j = 0;
//objDoc.Content.Find.Execute(FindText:=tofind,_
// ReplaceWith:= toreplace,_
//替换:= Word.WdReplace.wdReplaceAll)
// if(toreplace.Length()> 250)然后
int i = toreplace.Length;
while(j< = toreplace.Length)
{
string str = toreplace.Substring(x,x + 100);
toreplace = toreplace.Substring(x + 100);
objDoc.Content.Find.Execute(FindText:tofind,ReplaceWith:str ++ tofind,替换:Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll,格式:格式);
}
}
}
catch(exception ex)
{
string str = toreplace.Substring(x,x + toreplace.Length);
toreplace = toreplace.Substring(x + toreplace.Length);
objDoc.Content.Find.Execute(FindText:tofind,ReplaceWith:str,替换:Microsoft。 Office.Interop.Word.WdReplace.wdReplaceAll,格式:格式);
}
//'''EventLog.WriteEntry(To找到+ tofind +取代成功)
}
谢谢你的赞助...
问候,
Sampada
Hi
I am working on a web application in which I am opening the template file which is in temp.Docx finding tags in it and replacing it with its value.
I have achieved this using "Microsoft.Office.Interop.Word". It is working on my local machine.
But on server MS office is not installed on which this application is going to deploy.
Can you please let me know if I can achivethis task without using "Microsoft.Office.Interop.Word" and third party DLL ?
Following is my working code
public void GenerateWordReport(System.Data.DataTable dt, String sTempPath)
{
Microsoft.Office.Interop.Word.Application objWordApp = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document objDoc = new Microsoft.Office.Interop.Word.Document();
Object missing = System.Reflection.Missing.Value;
StringBuilder wordstring = new StringBuilder();
try
{
objWordApp.Documents.Open(sTempPath);
objDoc = objWordApp.ActiveDocument;
//MatchCollection bodyTagColl = ;
foreach (Match tag in Regex.Matches((objDoc.Content).Text, @"[[][[]\s*\w+\s*.*?\s*[]][]]"))
{
String exp = "Parent_NAME=''" + tag.Value.Substring(2, tag.Value.Length - 4).ToString() + "''";
DataRow[] foundRows;
foundRows = dt.Select(exp);
//if (foundRows)
if (foundRows.Length > 0)
{
foreach (DataRow dr in foundRows)
{
String Value = dr["Value"].ToString() + dr["Unit of Measure"].ToString().Trim();
if (("[[" + dr["Parent_NAME"].ToString() + "]]").Length <= 255)
FindnReplace("[[" + dr["Parent_NAME"].ToString() + "]]", Value, objDoc);
else
FindLargeChar("[[" + dr["Parent_NAME"].ToString() + "]]", Value, objDoc);
}
}
else
{
if (("[" + tag.Value).Length <= 255)
FindnReplace(tag.Value, "-", objDoc);
else
FindLargeChar(tag.Value, "-", objDoc);
}
}
objDoc.Save();
}
catch (Exception e)
{
}
finally
{
objWordApp.Documents.Close(Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges);
objWordApp.Quit();
objWordApp = null;
KillProcess("winword");
}
}
public void FindLargeChar(string tofind, string toreplace, Microsoft.Office.Interop.Word.Document objDoc)
{
int StartIndex = 0;
int EndIndex =254;
int length = tofind.Length;
if (tofind.Length > 255)
{
Decimal Dec=tofind.Length%255;
FindnReplace(tofind.Substring(StartIndex, EndIndex), toreplace, objDoc);
}
while (EndIndex <= tofind.Length)
{
length = length - 254;
int l = length;
StartIndex = StartIndex + 254;
EndIndex = EndIndex + 254;
if (length > 255)
{
FindnReplace(tofind.Substring(StartIndex, EndIndex), "", objDoc);
}
else
{
FindnReplace(tofind.Substring(StartIndex, l), "", objDoc);
}
}
}
public void KillProcess(string ProcessName)
{
try
{
//Process[] procs = Process.GetProcessesByName(ProcessName);
//foreach (Process process__1 in Process.GetProcessesByName(ProcessName))
//{
// process__1.Kill();
// process__1.WaitForExit();
//}
}
catch (Exception ex)
{
}
finally
{
}
}
public void FindnReplace(string tofind, string toreplace, Microsoft.Office.Interop.Word.Document objDoc)
{
//If (toreplace = "-999") Then
// toreplace = "None"
//End If
// '''' '''' '''' ''EventLog.WriteEntry("In FindnReplace")
// '''' '''' '''' ''EventLog.WriteEntry("To find " + tofind)
int y = 0;
int x = 0;
// Dim replace As Object = toreplace.Replace("\r\n", Chr(10))
object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML;
try
{
if ((toreplace.Length < 200))
{
objDoc.Content.Find.Execute(FindText: tofind, ReplaceWith: toreplace, Replace: Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll, Format: format);
}
else
{
int j = 0;
//objDoc.Content.Find.Execute(FindText:=tofind, _
// ReplaceWith:=toreplace, _
// Replace:=Word.WdReplace.wdReplaceAll)
//If (toreplace.Length() > 250) Then
int i = toreplace.Length;
while (j <= toreplace.Length)
{
string str = toreplace.Substring(x, x + 100);
toreplace = toreplace.Substring(x + 100);
objDoc.Content.Find.Execute(FindText: tofind, ReplaceWith: str + " " + tofind, Replace:Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll, Format: format);
}
}
}
catch (Exception ex)
{
string str = toreplace.Substring(x, x + toreplace.Length);
toreplace = toreplace.Substring(x + toreplace.Length);
objDoc.Content.Find.Execute(FindText: tofind, ReplaceWith: str, Replace: Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll, Format: format);
}
// ''EventLog.WriteEntry("To find " + tofind + " ReplacedSuccessfully")
}
Thanks in adavance...
Regards,
Sampada
推荐答案
使用ano第三方工具
ie
Spire.doc.dll文件
并进行操作
Use the another third party tool
i.e
Spire.doc.dll file
and doing operations
这篇关于打开/编辑/保存.Docx文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!