我已經被分配爲Word文檔創建一個TOC(目錄),它基於從數據庫獲得的幾個信息動態填充HTML代碼,具體的設計,最後這個操作必須在ASP中完成。 NET服務器端使用C#編程的3.5 .NET Framework,並且它必須以生成的TOC和Word格式發送到客戶端。在ASP.NET中使用html源文件生成TOC
我也可以告訴你,服務器的操作系統是Windows 2008 Server,並且它安裝了Microsoft Office 2010。因此,首先我嘗試使用Microsoft.Interoffice.Word將其集成到服務器端代碼中,但之後我意識到由於security matters,Microsoft不支持這類事情。然後我嘗試創建一個控制檯應用程序(使用Microsoft.Office.Interop.Word)並將HTML代碼導出到doc文檔,它們都位於同一文件夾中,並且IIS_USERS組可以完全訪問此位置。但是,當我嘗試啓動在網站代碼中創建目錄的控制檯應用程序時,它會向我顯示與控制檯應用程序有關的錯誤。
我測試了控制檯應用程序,生成的文檔中充滿了HTML代碼,它運行平穩,所以我不明白髮生了什麼... IIS是否檢測到控制檯應用程序使用Microsoft.Office.Interop.Word,它不允許它執行?這個想法是,當控制檯應用程序完成時,我會將整個文檔重新帶回網站,並向客戶端生成HTTP響應,以顯示典型的打開/保存彈出對話框。
我一直在試圖使用外部庫也沒有結果像GemBox(FileFormat與HTML代碼的問題),NPOI(它的HWPF庫它是在alpha版本),OpenSDK(無法評估分頁,所以它不能創建一個TOC wih充滿信息的文檔),AsPosed字(相當昂貴的圖書館,我不能嘗試這種功能在免費許可)
我告訴你知道的控制檯應用程序代碼:
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();
你能給我一些建議嗎?
感謝