我被指派为Word文档创建一个TOC(目录),它根据从数据库和特定设计获得的几个信息动态地填充HTML代码,最后这个操作必须在ASP中完成。 NET服务器端使用在C#中编程的3.5 .NET Framework,它必须以生成的TOC和Word格式发送到客户端。
我也可以告诉你,服务器的操作系统是Windows 2008 Server,它安装了Microsoft Office 2010。
所以,首先我尝试使用Microsoft.Interoffice.Word将它集成到服务器端代码中,但之后我意识到微软因为安全问题而不支持这类事情。 然后我尝试创建一个控制台应用程序来创建此功能(使用Microsoft.Office.Interop.Word)并将HTML代码导出到doc文档,它们都位于同一文件夹中,并且IIS_USERS组具有对此位置的完全访问权限。 但是,当我尝试启动在网站代码中创建目录的控制台应用程序时,它会向我显示与控制台应用程序相关的错误。
我已经使用生成的HTML代码测试了控制台应用程序,它运行顺畅,所以我不明白发生了什么... IIS是否检测到控制台应用程序使用的是Microsoft.Office.Interop.Word,并且它没有允许它执行? 我的想法是,当控制台应用程序完成后,我会将整个文档再次带回网站,并向客户端生成HTTP响应,以便显示典型的“打开/保存”弹出对话框。
我一直在尝试使用外部库而没有像GemBox(HTML代码的FileFormat问题),NPOI(它的alpha版本中的HWPF库),OpenSDK(无法评估分页,因此它无法创建TOC与文档填充的结果)信息),AsPosed Words(相当昂贵的库,我无法在免费许可中尝试这种功能)
我告诉你你知道控制台应用程序代码:
ArrayList arrValoresIndices = new ArrayList(); Application wordApp = new Application(); object missing = System.Type.Missing; try { Document wordDocument = wordApp.Documents.Open(docFileSource); //Applying style to headers System.Drawing.Color colorNecesario = System.Drawing.ColorTranslator.FromHtml("#1F497D"); WdColor coloraplicado = (Microsoft.Office.Interop.Word.WdColor)(colorNecesario.R + 0x100 * colorNecesario.G + 0x10000 * colorNecesario.B); wordApp.ActiveDocument.Styles[WdBuiltinStyle.wdStyleHeading1].Font.Name = "Calibri"; wordApp.ActiveDocument.Styles[WdBuiltinStyle.wdStyleHeading1].Font.Size = 14; wordApp.ActiveDocument.Styles[WdBuiltinStyle.wdStyleHeading1].Font.Bold = -1; wordApp.ActiveDocument.Styles[WdBuiltinStyle.wdStyleHeading1].Font.Color = coloraplicado; int numPalabras = wordDocument.Words.Count; for (int i = 1; i < numPalabras; i++) { string texto = wordDocument.Words[i].Text; if (texto.Equals("") == false && wordDocument.Words[i].Font.Size == 14 && wordDocument.Words[i].Font.Name == "Calibri" && wordDocument.Words[i].Font.Bold == -1 && wordDocument.Words[i].Font.Color == coloraplicado) { wordDocument.Words[i].set_Style(WdBuiltinStyle.wdStyleHeading1); } } object gotoPage = WdGoToItem.wdGoToPage; object gotoNext = WdGoToDirection.wdGoToNext; object gotoCount = null; object gotoName = "2"; wordApp.Selection.GoTo(ref gotoPage, ref gotoNext, ref gotoCount, ref gotoName); wordApp.Selection.InsertBreak(WdBreakType.wdPageBreak); gotoName = "2"; Range indexPage= wordApp.Selection.GoTo(ref gotoPage, ref gotoNext, ref gotoCount, ref gotoName); object oTrue = true; TableOfContents toc = wordDocument.TablesOfContents.Add(indexPage, ref oTrue, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref oTrue); wordDocument.TablesOfContents.Format = WdTocFormat.wdTOCModern; toc.Update(); wordDocument.SaveAs2(docFileSource,WdSaveFormat.wdFormatFilteredHTML); wordDocument.Close(); wordApp.Quit(); } catch (COMException ce) { wordApp.Application.Quit(ref missing, ref missing, ref missing); throw new COMException("Process has failed ...\n"); } }启动代码的网站代码:
String nombreDoc = "C:\\tempDocs\\JGA.doc"; //i will store the doc in this folder, in this folder IIS_USRS have full access File.WriteAllText(nombreDoc, strCabecera.ToString()); //strCabecera contains the whole html code Process p = new Process(); p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; p.StartInfo.FileName = @"C:\\tempDocs\\TOCIndexer\\TOCIndexer.exe -nombreDoc"; p.StartInfo.UseShellExecute = false; p.StartInfo.Verb = "runas"; using (Process exeProcess = Process.Start(p)) { exeProcess.WaitForExit(); } //When the process has finished I import againg the content to a new string builder StringBuilder strCabeceraVolcado = new StringBuilder(); using (var sr = new StreamReader(nombreDoc)) { strCabeceraVolcado.Append(sr.ReadToEnd()); } if (File.Exists(@nombreDoc)) { File.Delete(@nombreDoc); } strCabecera = strCabeceraVolcado; HttpContext.Current.Response.Write(strCabecera); HttpContext.Current.Response.Flush(); HttpContext.Current.Response.End();你能给我一些建议吗?
谢谢
I've been assigned to create a TOC (Table of contents) for Word document which it's filled with HTML code dynamically base on several information obtained from database and with a specific design, and finally this operation has to be done in a ASP. NET server side using 3.5 .NET Framework programmed in C# and it has to be sent to the client side with the TOC generated and in Word format.
I can tell you too that the OS of the server is Windows 2008 Server, and it has Microsoft Office 2010 installed.
So, firstly I try using Microsoft.Interoffice.Word integrating it in the server side code, but afterwards I realised that Microsoft didn't support this sort of things because of security matters. Then I try to create a console application which make this function (using Microsoft.Office.Interop.Word) and exporting the HTML code to doc document, both of them located in the same folder, and IIS_USERS group have full access to this location. But, when I try to launch the console application which creates the table of contents in the website code, it shows me an error related to the console application.
I have tested the console application with doc generated filled with HTML code and it runs smooth, so I don't understand what's going on...does IIS detects that the console application uses Microsoft.Office.Interop.Word, and it doesn't allow it to execute? The idea was that when the console application finish, I would bring back the whole document again to website and generate an HTTP response to the client side so it would show the typical Open/Save popup dialog.
I have been trying to use external libraries too without results like GemBox (FileFormat problem with HTML code), NPOI (its HWPF library it's in alpha version), OpenSDK (cannot evalute paging so it can't create a TOC wih the doc filled with info), AsPosed Words (quite expensive library, i can't try this kind of functions in free license)
I show you know the console application code:
ArrayList arrValoresIndices = new ArrayList(); Application wordApp = new Application(); object missing = System.Type.Missing; try { Document wordDocument = wordApp.Documents.Open(docFileSource); //Applying style to headers System.Drawing.Color colorNecesario = System.Drawing.ColorTranslator.FromHtml("#1F497D"); WdColor coloraplicado = (Microsoft.Office.Interop.Word.WdColor)(colorNecesario.R + 0x100 * colorNecesario.G + 0x10000 * colorNecesario.B); wordApp.ActiveDocument.Styles[WdBuiltinStyle.wdStyleHeading1].Font.Name = "Calibri"; wordApp.ActiveDocument.Styles[WdBuiltinStyle.wdStyleHeading1].Font.Size = 14; wordApp.ActiveDocument.Styles[WdBuiltinStyle.wdStyleHeading1].Font.Bold = -1; wordApp.ActiveDocument.Styles[WdBuiltinStyle.wdStyleHeading1].Font.Color = coloraplicado; int numPalabras = wordDocument.Words.Count; for (int i = 1; i < numPalabras; i++) { string texto = wordDocument.Words[i].Text; if (texto.Equals("") == false && wordDocument.Words[i].Font.Size == 14 && wordDocument.Words[i].Font.Name == "Calibri" && wordDocument.Words[i].Font.Bold == -1 && wordDocument.Words[i].Font.Color == coloraplicado) { wordDocument.Words[i].set_Style(WdBuiltinStyle.wdStyleHeading1); } } object gotoPage = WdGoToItem.wdGoToPage; object gotoNext = WdGoToDirection.wdGoToNext; object gotoCount = null; object gotoName = "2"; wordApp.Selection.GoTo(ref gotoPage, ref gotoNext, ref gotoCount, ref gotoName); wordApp.Selection.InsertBreak(WdBreakType.wdPageBreak); gotoName = "2"; Range indexPage= wordApp.Selection.GoTo(ref gotoPage, ref gotoNext, ref gotoCount, ref gotoName); object oTrue = true; TableOfContents toc = wordDocument.TablesOfContents.Add(indexPage, ref oTrue, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref oTrue); wordDocument.TablesOfContents.Format = WdTocFormat.wdTOCModern; toc.Update(); wordDocument.SaveAs2(docFileSource,WdSaveFormat.wdFormatFilteredHTML); wordDocument.Close(); wordApp.Quit(); } catch (COMException ce) { wordApp.Application.Quit(ref missing, ref missing, ref missing); throw new COMException("Process has failed ...\n"); } }Web site code which launches the code:
String nombreDoc = "C:\\tempDocs\\JGA.doc"; //i will store the doc in this folder, in this folder IIS_USRS have full access File.WriteAllText(nombreDoc, strCabecera.ToString()); //strCabecera contains the whole html code Process p = new Process(); p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; p.StartInfo.FileName = @"C:\\tempDocs\\TOCIndexer\\TOCIndexer.exe -nombreDoc"; p.StartInfo.UseShellExecute = false; p.StartInfo.Verb = "runas"; using (Process exeProcess = Process.Start(p)) { exeProcess.WaitForExit(); } //When the process has finished I import againg the content to a new string builder StringBuilder strCabeceraVolcado = new StringBuilder(); using (var sr = new StreamReader(nombreDoc)) { strCabeceraVolcado.Append(sr.ReadToEnd()); } if (File.Exists(@nombreDoc)) { File.Delete(@nombreDoc); } strCabecera = strCabeceraVolcado; HttpContext.Current.Response.Write(strCabecera); HttpContext.Current.Response.Flush(); HttpContext.Current.Response.End();Could you give me some advice?
Thanks
最满意答案
我在研究后意识到整个应用程序是正确的,问题在于服务器设置。
此解决方案仅适用于Windows Server 2008 64位,如果遇到Microsoft.Office.Interop.Word问题,则必须执行后续步骤:
1º您必须在服务器中安装Microsoft.Office.Word。
2º您必须在生成文档的文件夹中为“IIS用户组”和“匿名登录”用户分配“完全控制”权限。
3º您必须修改组件服务(C:\ Windows \ System32 \ comexp.msc)并将标识值“交互式用户”设置为“Office许可COM服务器14属性”(可以是12而不是14,具体取决于版本您安装的Office)
4º在位于C:\ Windows \ SysWOW64 \ config \ systemprofile \的路径中创建文件夹“Desktop”
如果您按照此步骤操作,ASP.NET将允许您使用Microsof.Office.Interop.Word库修改文档。
I have realised after researching, that the whole application was right and the problem resides in server settings.
This solution only works for Windows Server 2008 64 bits, you have to follow the next steps if you encounter problems with Microsoft.Office.Interop.Word:
1º You have to install Microsoft.Office.Word in the server.
2º You have to assigned "Full Control" permissions in the folder in which the documents are generated to the "IIS group of users", and "Anonymous Logon" user.
3º You have to modify Component Services (C:\Windows\System32\comexp.msc) and set Identity the value "The Interactive User" to "Office Licensing COM Server 14 Properties" (can be 12 instead of 14 depending on the version of Office you install)
4º Create folder "Desktop" in the path located at C:\Windows\SysWOW64\config\systemprofile\
If you follow this steps, ASP.NET will allow you modify documents using Microsof.Office.Interop.Word library.
更多推荐
发布评论