1

私はSharePoint2010を使用しています。私がやろうとしているのは、Word文書テンプレートを取得し、いくつかのキーワードを置換して(例:##ClientID##クライアントのIDに置換)、特定の名前でライブラリに保存することです。共有ポイント。

ワード相互運用機能を備えたローカルコンピューターでこれを行う方法を理解しましたが、ワード相互運用機能ライブラリはサービスとして実行するようには設計されていません。次に、必要なことを実行しているように見えるWordAutomationServicesを発見しました。Microsoft.Office.Word.Server.Conversionsしかし、私がインターネット上で見つけたすべての例(ここSOを含む)は、名前空間を使用した「単語文書からxxxへの変換方法」です。Microsoft.Office.Word.Server.Service名前空間を使用してドキュメントの検索と置換を行う方法の例はまだ見つかりません。MSDNにはクラスの使用方法が非常に不足しており、どこから使用を開始すればよいかわかりません。

私がやりたいことをするためにサービスを使うことはできませんか?それができれば、誰かが私を正しい方向に向けて、私がやりたいことをすることができますか?

4

1 に答える 1

3

Word Automation Servicesは、私がやりたいことを実行するために使用したいものではないようです。必要なのはOpenXMLSDKです。

更新:これは、ドキュメントの置換を行う方法に関するコードです。私のテキストでは、リッチテキストボックスのどこを置換したかったのです。そのため、私はSdtRunの内部を見ています。

public FileDetails GetOrGenerateChecklist(string PracticeName, string ContractID, string EducationDate, string MainContactInfo, string Address)
{
    if (String.IsNullOrEmpty(PracticeName) || String.IsNullOrEmpty(ContractID))
        return null;
    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(
                            "<Where><Eq>",
                                "<FieldRef Name='FileLeafRef'/>",
                                "<Value Type='File'>", PracticeName, " - ", ContractID, ".docx</Value>",
                            "</Eq></Where>");
    var items = list.GetItems(query);

    //if document exists return existing document.
    if (items.Count > 0)
        return new FileDetails() { Address = String.Concat(web.Url, "/Educator Checklists/", PracticeName, " - ", ContractID, ".docx"), LastModified = (DateTime)items[0]["Modified"]};

    //Begin transforming form template to document.
    MemoryStream documentStream;

    //copy the stream to memory
    using (Stream tplStream = web.GetFile(templetAddr).OpenBinaryStream())
    {
        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);
        if(!String.IsNullOrEmpty(EducationDate))
            ReplaceText(mainPart, "#EducationDate#", EducationDate);
        if(!String.IsNullOrEmpty(MainContactInfo))
            ReplaceText(mainPart, "#MainContactInfo#", MainContactInfo);
        if(!String.IsNullOrEmpty(Address))
            ReplaceText(mainPart, "#Address#", Address);
    }
    documentStream.Position = 0L;
    try
    {
        list.RootFolder.Files.Add(String.Concat(PracticeName, " - ", ContractID, ".docx"), documentStream);
    }
    catch(SPException)
    {
        return null;
    }

    return new FileDetails() { Address = String.Concat(web.Url, "/Educator Checklists/", PracticeName, " - ", ContractID, ".docx"), LastModified = DateTime.Now };


}

private static void CopyStream(Stream source, Stream destination, int bufferSize = 0x1000)
{
    int num;
    byte[] buffer = new byte[bufferSize];
    while ((num = source.Read(buffer, 0, buffer.Length)) != 0)
    {
        destination.Write(buffer, 0, num);
    }

}

private static void ReplaceText(MainDocumentPart docPart, string match, string value)
{
    if (value == null)
        value = String.Empty;
    var sdtr = docPart.Document.Descendants<SdtRun>();
    foreach (var sdt in sdtr)
    {
        if (sdt.InnerText == match)
        {

            Text txt = new Text(value);
            //using the sdt.FirstChild.FirstChild.CloneNode(true) will copy the text formatting of the old text.
            var newtext = new SdtContentRun(new Run(sdt.FirstChild.FirstChild.CloneNode(true), txt));
            sdt.SdtContentRun.RemoveAllChildren();
            sdt.SdtContentRun.InsertAt(newtext, 0);
        }
    }
}
于 2011-03-30T19:18:50.280 に答える