为什么这个代码写一个“;损坏的文件“;Word无法打开(甚至“打开并修复”)
本文关键字:甚至 打开并修复 代码 一个 文件 为什么 损坏 Word | 更新日期: 2023-09-27 18:25:48
我是个新手,可以和我谈谈。抱歉这么含糊;代码执行并写入损坏的文件。我无法进入损坏的文件来观察任何错误/异常。大小看起来确实像是要合并的文件的总和。
我在谷歌上搜索了一下,找不到任何我能理解如何实现的东西。
打开时的Word错误为:无法打开文件,因为内容有问题
和
Word发现无法读取的内容,是否继续?
当我单击"是"时,我再次得到第一个错误&那我就出局了。
这些文档有相同名称的内容控件,但我修改了它,只添加了一个文档,以查看重复的内容类型是否会有问题;同样的损坏文件结果。
顺便说一句。。。。我的最终目的是覆盖"模板"(…Aggregate Report.dotx)。但我无法在任何地方保存有效的文件,所以……:-/
using System;
using System.IO;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Linq;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using Word = DocumentFormat.OpenXml.Wordprocessing;
namespace BobsDocMerger.VisualWebPart1
{
[ToolboxItemAttribute(false)]
public class VisualWebPart1 : WebPart
{
// Visual Studio might automatically update this path when you change the Visual Web Part project item.
private const string _ascxPath = @"~/_CONTROLTEMPLATES/BobsDocMerger/VisualWebPart1/VisualWebPart1UserControl.ascx";
protected override void CreateChildControls()
{
System.Web.UI.Control control = this.Page.LoadControl(_ascxPath);
Controls.Add(control);
base.CreateChildControls();
Button btnSubmit = new Button();
btnSubmit.Text = "Assemble Documents";
btnSubmit.Click += new EventHandler(btnSubmit_Click);
Controls.Add(btnSubmit);
}
void btnSubmit_Click(object sender, EventArgs e)
{
SPFolder folder = SPContext.Current.ListItem.Folder;
char[] splitter = { '/' };
string[] folderName = folder.Name.Split(splitter);
string filePrefix = @"Weekly/" + folderName[0] + "/" + folderName[0];
SPFile template = folder.Files[filePrefix + " - Aggregate Report.dotx"];
SPFile file;
byte[] byteArray = template.OpenBinary();
using (MemoryStream mem = new MemoryStream())
{
mem.Write(byteArray, 0, (int)byteArray.Length);
using (WordprocessingDocument myDoc = WordprocessingDocument.Open(mem, true))
{
MainDocumentPart mainPart = myDoc.MainDocumentPart;
//Loop thru content controls
foreach (Word.SdtElement sdt in mainPart.Document.Descendants<Word.SdtElement>().ToList())
{
Word.SdtAlias alias = sdt.Descendants<Word.SdtAlias>().FirstOrDefault();
if (alias != null)
{
//The 2 tags in the Report are AggregateHeader and AggregateBody
string sdtTitle = alias.Val.Value;
string sdtTag = sdt.GetFirstChild<SdtProperties>().GetFirstChild<Tag>().Val;
if (sdtTitle == "Merge")
{
for (int i = 0; i < folder.Files.Count; i++)
{
file = folder.Files[i];
//Do all files that are NOT the Aggregate Report
if (file.Name.IndexOf("Aggregate Report") == -1)
{
if (i == folder.Files.Count-1)
{
AddAltChunk(mainPart, sdt, file, true);
}
else
{
AddAltChunk(mainPart, sdt, file, false);
}
}
}
}
}
}
HttpResponse resp = HttpContext.Current.Response;
resp.ClearContent();
resp.ClearHeaders();
resp.AddHeader("Content-Disposition", "attachment; filename=Assembled Document.docx");
//resp.ContentEncoding = System.Text.Encoding.UTF8;
resp.ContentType = "application/msword";
resp.OutputStream.Write(mem.ToArray(), 0, (int)mem.Length);
resp.Flush();
resp.Close();
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
}
}
protected int id = 1;
void AddAltChunk(MainDocumentPart mainPart, Word.SdtElement sdt, SPFile filename,bool LastPass)
{
string altChunkId = "AltChunkId" + id;
id++;
byte[] byteArray = filename.OpenBinary();
AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(
AlternativeFormatImportPartType.WordprocessingML, altChunkId);
using (MemoryStream mem = new MemoryStream())
{
mem.Write(byteArray, 0, (int)byteArray.Length);
mem.Seek(0, SeekOrigin.Begin);
chunk.FeedData(mem);
}
Word.AltChunk altChunk = new Word.AltChunk();
altChunk.Id = altChunkId;
//Replace content control with altChunk information
DocumentFormat.OpenXml.OpenXmlElement parent = sdt.Parent;
parent.InsertBefore(altChunk, sdt);
if (LastPass) { sdt.Remove(); }
}
}
}
看起来您没有在主内存流上正确调用.Seek()
,而且您似乎同时将单个内存流用于输入和输出。(也许这是正确的,但当它是混合模式时,我很困惑)
我假设您无法访问原始文件名和文件系统:
using(Stream t = template.OpenBinaryStream())
{
using (WordprocessingDocument myDoc = WordprocessingDocument.Open(t, true))
{
using (XmlWriter writer = XmlWriter.Create(resp.OutputStream))
{
// TODO re-add merge logic here once it works
HttpResponse resp = HttpContext.Current.Response;
resp.ClearContent();
resp.ClearHeaders();
resp.AddHeader("Content-Disposition",
"attachment; filename=Assembled Document.docx");
//resp.ContentEncoding = System.Text.Encoding.UTF8;
resp.ContentType = "application/msword";
// resp.OutputStream.Write(mem.ToArray(), 0, (int)mem.Length);
/* new */ myDoc.MainDocumentPart.Document.WriteTo(writer);
resp.Flush();
resp.Close();
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
}
}
PS-我建议先获得一个正确输出的原始模板。然后,在重新添加合并逻辑之前,进行一个微小的更改并查看输出。