我正在尝试加载my SPDocumentLibrary
设置的模板文件,在OpenXML中进行一些转换,然后将其作为文档保存回库中。除了阅读模板之外,我想我已经完成了所有步骤。我不知道打开它的“正确”方法(我可以使用WebClient来下载它,但它让我感觉很脏,只需输入它)。这是我到目前为止所拥有的。
public string GetOrGenerateChecklist(string PracticeName, string ContractID, string EducationDate, string MainContactInfo, string Address)
{
using (SPWeb web = SPContext.Current.Web)
{
SPDocumentLibrary list = (SPDocumentLibrary)web.Lists["Educator Checklists"];
var templetAddr = String.Concat(web.Url, '/', list.DocumentTemplateUrl);
SPQuery query = new SPQuery();
query.Query = string.Concat(
//Snip
);
var items = list.GetItems(query);
//if document exists return existing document.
if (items.Count > 0)
return String.Concat(web.Url, "/Educator Checklists/", PracticeName, " - ", ContractID, ".docx");
MemoryStream documentStream;
//copy the stream to memory
using (Stream tplStream = ????????) //<------- my problem
{
documentStream = new MemoryStream((int)tplStream.Length);
CopyStream(tplStream, documentStream);
documentStream.Position = 0L;
}
using (WordprocessingDocument template = WordprocessingDocument.Open(documentStream, true))
{
template.ChangeDocumentType(DocumentFormat.OpenXml.WordprocessingDocumentType.Document);
MainDocumentPart mainPart = template.MainDocumentPart;
mainPart.DocumentSettingsPart.AddExternalRelationship(
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/attachedTemplate",
new Uri(templetAddr, UriKind.Absolute));
ReplaceText(mainPart, "#PracticeName#", PracticeName);
ReplaceText(mainPart, "#EducationDate#", EducationDate);
ReplaceText(mainPart, "#MainContactInfo#", MainContactInfo);
ReplaceText(mainPart, "#Address#", Address);
}
documentStream.Position = 0L;
list.RootFolder.Files.Add(String.Concat(PracticeName, " - ", ContractID, ".docx"), documentStream);
return String.Concat(web.Url, "/Educator Checklists/", PracticeName, " - ", ContractID, ".docx");
}
}
所以我的问题是如何将存储在templetAddr
的模板加载到内存流中?
此外,我对sharepoint相当新,所以如果你看到我做过的任何其他重大错误,请告诉我。
答案 0 :(得分:4)
web.GetFile(templetAddr).OpenBinaryStream()
http://msdn.microsoft.com/en-us/library/ms476063.aspx(SPWeb.GetFile)
http://msdn.microsoft.com/en-us/library/ms470901.aspx(SPFile.OpenBinaryStream)
答案 1 :(得分:1)
这是一篇博客文章,讨论了使用SharePoint对象模型获取Open XML文档,修改它并将其放回时的一些注意事项。
以下是执行此操作的最小代码:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using Microsoft.SharePoint;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
class Program
{
static void Main(string[] args)
{
string siteUrl = "http://localhost";
using (SPSite spSite = new SPSite(siteUrl))
{
Console.WriteLine("Querying for Test.docx");
SPList list = spSite.RootWeb.Lists["Shared Documents"];
SPQuery query = new SPQuery();
query.ViewFields = @"<FieldRef Name='FileLeafRef' />";
query.Query =
@"<Where>
<Eq>
<FieldRef Name='FileLeafRef' />
<Value Type='Text'>Test.docx</Value>
</Eq>
</Where>";
SPListItemCollection collection = list.GetItems(query);
if (collection.Count != 1)
{
Console.WriteLine("Test.docx not found");
Environment.Exit(0);
}
Console.WriteLine("Opening");
SPFile file = collection[0].File;
byte[] byteArray = file.OpenBinary();
using (MemoryStream memStr = new MemoryStream())
{
memStr.Write(byteArray, 0, byteArray.Length);
using (WordprocessingDocument wordDoc =
WordprocessingDocument.Open(memStr, true))
{
Document document = wordDoc.MainDocumentPart.Document;
Paragraph firstParagraph = document.Body.Elements<Paragraph>()
.FirstOrDefault();
if (firstParagraph != null)
{
Paragraph testParagraph = new Paragraph(
new Run(
new Text("Test")));
firstParagraph.Parent.InsertBefore(testParagraph,
firstParagraph);
}
}
Console.WriteLine("Saving");
string linkFileName = file.Item["LinkFilename"] as string;
file.ParentFolder.Files.Add(linkFileName, memStr, true);
}
}
}
}