症状: 生成Word格式的报告,调试过程中总是出现"服务器出现意外情况。 (异常来自 HRESULT:0x80010105 (RPC_E_SERVERFAULT))"的错误
原因: 代码
WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing,ref Visible)
,
最后一个参数Visibale设置成false了
解决方法:Visible=true
using
System;
using
System.Collections.Generic;
using
System.Text;
namespace
Business
public
class
WordDocument
Microsoft.Office.Interop.Word.Application WordApp;
Microsoft.Office.Interop.Word.Document WordDoc;
object
SaveChanges
=
true
;
object
Visible
=
true
;
//
问题就在这里,改成true后就能正常运行了
object
Nothing
=
System.Reflection.Missing.Value;
object
WordDocPath;
public
WordDocument(
string
wordDocPath)
this
.WordDocPath
=
wordDocPath;
this
.Open();
private
void
Open()
if
(System.IO.File.Exists(
this
.WordDocPath.ToString()))
System.IO.File.Delete(
this
.WordDocPath.ToString());
WordApp
=
new
Microsoft.Office.Interop.Word.ApplicationClass();
WordDoc
=
WordApp.Documents.Add(
ref
Nothing,
ref
Nothing,
ref
Nothing,
ref
Visible);
public
void
Append(
string
Content,
int
Level)
WordDoc.Paragraphs.Last.Range.Text
=
WordDoc.Paragraphs.Last.Range.Text
+
Content;
switch
(Level)
case
1
:
WordDoc.Paragraphs.Last.Range.Font.Bold
=
3
;
WordDoc.Paragraphs.Last.Range.Font.Name
=
"
宋体
"
;
WordDoc.Paragraphs.Last.Range.Font.Size
=
16
;
break
;
case
2
:
WordDoc.Paragraphs.Last.Range.Font.Bold
=
3
;
WordDoc.Paragraphs.Last.Range.Font.Name
=
"
宋体
"
;
WordDoc.Paragraphs.Last.Range.Font.Size
=
14
;
break
;
case
3
:
WordDoc.Paragraphs.Last.Range.Font.Bold
=
3
;
WordDoc.Paragraphs.Last.Range.Font.Name
=
"
宋体
"
;
WordDoc.Paragraphs.Last.Range.Font.Size
=
12
;
//
Content = " " + Content;
break
;
default
:
WordDoc.Paragraphs.Last.Range.Font.Bold
=
0
;
WordDoc.Paragraphs.Last.Range.Font.Size
=
12
;
WordDoc.Paragraphs.Last.Range.Font.Name
=
"
宋体
"
;
//
Content = " " + Content;
break
;
//
*/
public
void
SaveDoc()
WordDoc.SaveAs(
ref
this
.WordDocPath,
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);
public
void
CloseDoc()
WordDoc.Close(
ref
SaveChanges,
ref
Nothing,
ref
Nothing);
WordApp.Quit(
ref
SaveChanges,
ref
Nothing,
ref
Nothing);
private
void
Create(
string
wordDocPath)
System.IO.FileInfo file
=
new
System.IO.FileInfo(wordDocPath);
file.Create();