首页 / C# / c# – 指定的包无效.缺少主要部分
c# – 指定的包无效.缺少主要部分
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 指定的包无效.缺少主要部分,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1965字,纯文字阅读大概需要3分钟。
内容图文
![c# – 指定的包无效.缺少主要部分](/upload/InfoBanner/zyjiaocheng/781/7629a8143b2e428ab6ba7ca9d6d09118.jpg)
下面给出的是一个合并函数,用于合并文件夹中的所有docx文件并生成合并文件.
public void Merge()
{
try
{
string sid = Request.QueryString["studid"];
string stud = sid.ToString();
string ds = HttpContext.Current.Server.MapPath(("~\\StudentBinder") + "\\Temp4\\");
if (Directory.Exists(ds))
{
DirectoryInfo d = new DirectoryInfo(ds);
FileInfo[] Files = d.GetFiles("*" + stud + "*.docx");
// stud added for differentiating b/w users
string[] filepaths = new string[Files.Length];
int index = 0;
foreach (FileInfo file in Files)
{
filepaths[index] = file.Name;
index++;
}
using (WordprocessingDocument myDoc = WordprocessingDocument.Open(Server.MapPath(filepaths[0]), true))
{
for (int i = 1; i < filepaths.Length; i++)
{
MainDocumentPart mainPart = myDoc.MainDocumentPart;
string altChunkId = "AltChunkId" + i.ToString();
AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.WordprocessingML, altChunkId);
using (FileStream fileStream = File.Open(@filepaths[i], FileMode.Open))
{
chunk.FeedData(fileStream);
}
AltChunk altChunk = new AltChunk();
altChunk.Id = altChunkId;
mainPart.Document.Body.InsertAfter(altChunk, mainPart.Document.Body.Elements<DocumentFormat.OpenXml.Wordprocessing.Paragraph>().Last());
mainPart.Document.Save();
myDoc.Close();
}
}
}
}
catch (Exception ex)
{
}
}
但是这条线
using (WordprocessingDocument myDoc = WordprocessingDocument.Open(Server.MapPath(filepaths[0]), true))
引发错误指定的包无效.缺少主要部分.
我不知道那句话中出了什么问题.欢迎任何建议.提前致谢.
解决方法:
您可能在两次开始时执行Server.MapPath
string ds = HttpContext.Current.Server.MapPath(("~\\StudentBinder")+"\\Temp4\\")
并在行中
using (WordprocessingDocument myDoc = WordprocessingDocument.Open(Server.MapPath(filepaths[0]), true))
您可以将此行重写为
using (WordprocessingDocument myDoc = WordprocessingDocument.Open(filepaths[0], true))
你还需要从file.FullName填充filepaths数组,而不是file.Name:
foreach (FileInfo file in Files)
{
filepaths[index] = file.FullName;
index++;
}
内容总结
以上是互联网集市为您收集整理的c# – 指定的包无效.缺少主要部分全部内容,希望文章能够帮你解决c# – 指定的包无效.缺少主要部分所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。
来源:【匿名】