protected void Page_Load(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(Request["ReportID"]))
{
string NewfileName = Server.MapPath("/Admin/WordPrint/Templet/合并附件.doc");
VIVE.CNUOutlay.BLL.FileInfo file = new VIVE.CNUOutlay.BLL.FileInfo();
string fileName=string.Empty;
DataSet ds = file.GetAllDynamic("ReportID='" + Request["ReportID"] + "'", "");
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
fileName+=ds.Tables[0].Rows[i]["FilePath"].ToString()+',';
}
string [] getFileNames=fileName.Split(',');
string path2 = UIHelper.GetReportFileUrl(Convert.ToInt32(Request["ReportID"]));
InsertMerge(NewfileName, getFileNames, path2);
UIHelper.DownLoadFile(path2);
}
} /// <summary>
/// 循环合并多个文件(插入合并文件)
/// </summary>
/// <param name="tempDoc">模板文件</param>
/// <param name="arrCopies">需要合并的文件</param>
/// <param name="outDoc">合并后的输出文件</param>
public void InsertMerge(string tempDoc, string[] arrCopies, string outDoc)
{
ApplicationClass objApp = new ApplicationClass();
object objMissing = Missing.Value;
object objFalse = false;
object confirmConversion = false;
object link = false;
object objTempDoc = tempDoc;
object attachment = false;
try
{
objApp.Documents.Open(
ref objTempDoc, //FileName
ref objMissing, //ConfirmVersions
ref objMissing, //ReadOnly
ref objMissing, //AddToRecentFiles
ref objMissing, //PasswordDocument
ref objMissing, //PasswordTemplate
ref objMissing, //Revert
ref objMissing, //WritePasswordDocument
ref objMissing, //WritePasswordTemplate
ref objMissing, //Format
ref objMissing, //Enconding
ref objMissing, //Visible
ref objMissing, //OpenAndRepair
ref objMissing, //DocumentDirection
ref objMissing, //NoEncodingDialog
ref objMissing //XMLTransform
);
foreach (string strCopy in arrCopies)
{
if (!string.IsNullOrEmpty(strCopy))
{
if (File.Exists(strCopy))
{
objApp.Selection.InsertFile(
strCopy,
ref objMissing,
ref objMissing,
ref objMissing,
ref objMissing
);
}
} } object objOutDoc = outDoc;
Word._Document objDocLast = objApp.ActiveDocument;
objDocLast.SaveAs(
ref objOutDoc, //FileName
ref objMissing, //FileFormat
ref objMissing, //LockComments
ref objMissing, //PassWord
ref objMissing, //AddToRecentFiles
ref objMissing, //WritePassword
ref objMissing, //ReadOnlyRecommended
ref objMissing, //EmbedTrueTypeFonts
ref objMissing, //SaveNativePictureFormat
ref objMissing, //SaveFormsData
ref objMissing, //SaveAsAOCELetter,
ref objMissing, //Encoding
ref objMissing, //InsertLineBreaks
ref objMissing, //AllowSubstitutions
ref objMissing, //LineEnding
ref objMissing //AddBiDiMarks
);
//保存到输出文件
//SaveAs(outDoc);
foreach (Document objDocument in objApp.Documents)
{
objDocument.Close(
ref objFalse, //SaveChanges
ref objMissing, //OriginalFormat
ref objMissing //RouteDocument
);
}
}
finally
{
objApp.Quit(
ref objMissing, //SaveChanges
ref objMissing, //OriginalFormat
ref objMissing //RoutDocument
);
objApp = null;
}
}
在SaveAs 的时候抛异常,看不懂什么意思,有那个知道的帮帮忙异常信息:
“/”应用程序中的服务器错误。
--------------------------------------------------------------------------------HINESENUM1C (195_441_2011_0.doc)
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Runtime.InteropServices.COMException: HINESENUM1C (195_441_2011_0.doc)源错误:
行 104: object objOutDoc = outDoc;
行 105: Word._Document objDocLast = objApp.ActiveDocument;
行 106: objDocLast.SaveAs(
行 107: ref objOutDoc, //FileName
行 108: ref objMissing, //FileFormat
源文件: e:\WorkPlace\首都师范大学\CNUOutlay\Code\Web\Admin\WordPrint\Print_Attachment.aspx.cs 行: 106 堆栈跟踪:
[COMException (0x800a13e8): HINESENUM1C
(195_441_2011_0.doc)]
Microsoft.Office.Interop.Word.DocumentClass.SaveAs(Object& FileName, Object& FileFormat, Object& LockComments, Object& Password, Object& AddToRecentFiles, Object& WritePassword, Object& ReadOnlyRecommended, Object& EmbedTrueTypeFonts, Object& SaveNativePictureFormat, Object& SaveFormsData, Object& SaveAsAOCELetter, Object& Encoding, Object& InsertLineBreaks, Object& AllowSubstitutions, Object& LineEnding, Object& AddBiDiMarks) +0
Admin_WordPrint_Print_Attachment.InsertMerge(String tempDoc, String[] arrCopies, String outDoc) in e:\WorkPlace\首都师范大学\CNUOutlay\Code\Web\Admin\WordPrint\Print_Attachment.aspx.cs:106
Admin_WordPrint_Print_Attachment.Page_Load(Object sender, EventArgs e) in e:\WorkPlace\首都师范大学\CNUOutlay\Code\Web\Admin\WordPrint\Print_Attachment.aspx.cs:39
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +34
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +47
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1061
--------------------------------------------------------------------------------
版本信息: Microsoft .NET Framework 版本:2.0.50727.42; ASP.NET 版本:2.0.50727.42
{
if (!string.IsNullOrEmpty(Request["ReportID"]))
{
string NewfileName = Server.MapPath("/Admin/WordPrint/Templet/合并附件.doc");
VIVE.CNUOutlay.BLL.FileInfo file = new VIVE.CNUOutlay.BLL.FileInfo();
string fileName=string.Empty;
DataSet ds = file.GetAllDynamic("ReportID='" + Request["ReportID"] + "'", "");
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
fileName+=ds.Tables[0].Rows[i]["FilePath"].ToString()+',';
}
string [] getFileNames=fileName.Split(',');
string path2 = UIHelper.GetReportFileUrl(Convert.ToInt32(Request["ReportID"]));
InsertMerge(NewfileName, getFileNames, path2);
UIHelper.DownLoadFile(path2);
}
} /// <summary>
/// 循环合并多个文件(插入合并文件)
/// </summary>
/// <param name="tempDoc">模板文件</param>
/// <param name="arrCopies">需要合并的文件</param>
/// <param name="outDoc">合并后的输出文件</param>
public void InsertMerge(string tempDoc, string[] arrCopies, string outDoc)
{
ApplicationClass objApp = new ApplicationClass();
object objMissing = Missing.Value;
object objFalse = false;
object confirmConversion = false;
object link = false;
object objTempDoc = tempDoc;
object attachment = false;
try
{
objApp.Documents.Open(
ref objTempDoc, //FileName
ref objMissing, //ConfirmVersions
ref objMissing, //ReadOnly
ref objMissing, //AddToRecentFiles
ref objMissing, //PasswordDocument
ref objMissing, //PasswordTemplate
ref objMissing, //Revert
ref objMissing, //WritePasswordDocument
ref objMissing, //WritePasswordTemplate
ref objMissing, //Format
ref objMissing, //Enconding
ref objMissing, //Visible
ref objMissing, //OpenAndRepair
ref objMissing, //DocumentDirection
ref objMissing, //NoEncodingDialog
ref objMissing //XMLTransform
);
foreach (string strCopy in arrCopies)
{
if (!string.IsNullOrEmpty(strCopy))
{
if (File.Exists(strCopy))
{
objApp.Selection.InsertFile(
strCopy,
ref objMissing,
ref objMissing,
ref objMissing,
ref objMissing
);
}
} } object objOutDoc = outDoc;
Word._Document objDocLast = objApp.ActiveDocument;
objDocLast.SaveAs(
ref objOutDoc, //FileName
ref objMissing, //FileFormat
ref objMissing, //LockComments
ref objMissing, //PassWord
ref objMissing, //AddToRecentFiles
ref objMissing, //WritePassword
ref objMissing, //ReadOnlyRecommended
ref objMissing, //EmbedTrueTypeFonts
ref objMissing, //SaveNativePictureFormat
ref objMissing, //SaveFormsData
ref objMissing, //SaveAsAOCELetter,
ref objMissing, //Encoding
ref objMissing, //InsertLineBreaks
ref objMissing, //AllowSubstitutions
ref objMissing, //LineEnding
ref objMissing //AddBiDiMarks
);
//保存到输出文件
//SaveAs(outDoc);
foreach (Document objDocument in objApp.Documents)
{
objDocument.Close(
ref objFalse, //SaveChanges
ref objMissing, //OriginalFormat
ref objMissing //RouteDocument
);
}
}
finally
{
objApp.Quit(
ref objMissing, //SaveChanges
ref objMissing, //OriginalFormat
ref objMissing //RoutDocument
);
objApp = null;
}
}
在SaveAs 的时候抛异常,看不懂什么意思,有那个知道的帮帮忙异常信息:
“/”应用程序中的服务器错误。
--------------------------------------------------------------------------------HINESENUM1C (195_441_2011_0.doc)
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Runtime.InteropServices.COMException: HINESENUM1C (195_441_2011_0.doc)源错误:
行 104: object objOutDoc = outDoc;
行 105: Word._Document objDocLast = objApp.ActiveDocument;
行 106: objDocLast.SaveAs(
行 107: ref objOutDoc, //FileName
行 108: ref objMissing, //FileFormat
源文件: e:\WorkPlace\首都师范大学\CNUOutlay\Code\Web\Admin\WordPrint\Print_Attachment.aspx.cs 行: 106 堆栈跟踪:
[COMException (0x800a13e8): HINESENUM1C
(195_441_2011_0.doc)]
Microsoft.Office.Interop.Word.DocumentClass.SaveAs(Object& FileName, Object& FileFormat, Object& LockComments, Object& Password, Object& AddToRecentFiles, Object& WritePassword, Object& ReadOnlyRecommended, Object& EmbedTrueTypeFonts, Object& SaveNativePictureFormat, Object& SaveFormsData, Object& SaveAsAOCELetter, Object& Encoding, Object& InsertLineBreaks, Object& AllowSubstitutions, Object& LineEnding, Object& AddBiDiMarks) +0
Admin_WordPrint_Print_Attachment.InsertMerge(String tempDoc, String[] arrCopies, String outDoc) in e:\WorkPlace\首都师范大学\CNUOutlay\Code\Web\Admin\WordPrint\Print_Attachment.aspx.cs:106
Admin_WordPrint_Print_Attachment.Page_Load(Object sender, EventArgs e) in e:\WorkPlace\首都师范大学\CNUOutlay\Code\Web\Admin\WordPrint\Print_Attachment.aspx.cs:39
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +34
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +47
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1061
--------------------------------------------------------------------------------
版本信息: Microsoft .NET Framework 版本:2.0.50727.42; ASP.NET 版本:2.0.50727.42
不能用文件流吗?假设B的内容要合并到A里面,可以理解为在A的后面追加B。则分别将A和B以流文件的方式打开,其中A的模式为Append,然后将B按行读取后追加到A(按行读取可以减轻机器负担)。