首页 > 解决方案 > 使用 open xml 2.0 在 word 文档表中插入重复数据

问题描述

我有用户填充某些字段的共享点列表。我制作了事件接收器,当用户更新文档库中的某些字段时,会创建在表中插入重复数据的文档。我有问题。我的代码是

public bool CreateDocument(string sFilename, string sContentType, string sList, string id, string broj, string datum, string opis, string ponudjac, string ponudjacadresa, string ponudjacemail, string sektor, string sektormestorada, string subjektrevizije,  string usluge, string akontacija, string udaljenostodsubjekta, string rukovodilac, string url)
        {
            try
            {
                SPSite site = new SPSite(url);
                using (SPWeb web = site.OpenWeb())
                {
    
                   
                    SPList list = web.Lists[sList];
                    // this always uses root folder
                    SPFolder folder = list.RootFolder;
                    SPFileCollection fcol = folder.Files;
                       
                    // find the template url and open
                    string sTemplate = list.ContentTypes[sContentType].DocumentTemplateUrl;
                    SPFile spf = web.GetFile(sTemplate);
                    byte[] binFile = spf.OpenBinary();
                    // Url for file to be created
                    string destFile = fcol.Folder.Url + "/" + sFilename;
    
                      
                    SPFile addedFile = fcol.Add(destFile, binFile);
                    SPItem newItem = addedFile.Item;
                    newItem.Update();
                    addedFile.Update();
                                        
                     SPQuery query = new SPQuery();
                     query.Query = "<Where><Eq>" + "<FieldRef Name='FileLeafRef' />" + "<Value Type='Text'>" + sFilename + "</Value>" + "</Eq>" + "</Where>";
                        
                    SPListItemCollection collection = list.GetItems(query);
                     SPFile file = collection[0].File;
                     byte[] byteArray = file.OpenBinary();
                     using (MemoryStream memStr = new MemoryStream())
                     {
                         memStr.Write(byteArray, 0, byteArray.Length);
                           
                         using (WordprocessingDocument doc = WordprocessingDocument.Open(memStr, true))
                         {
                             //create XML string matching custom XML part
                          
    
                             MainDocumentPart main = doc.MainDocumentPart;
                             Table TabelaZapisnik = main.Document.Body.Descendants<Table>().First();
                             // Get the last row in the table.
                             TableRow Red = TabelaZapisnik.Elements<TableRow>().Last();
                             TableRow rowCopy = (TableRow)Red.CloneNode(true);
                             rowCopy.Descendants<TableCell>().ElementAt(0).Append(new Paragraph(new Run(new Text("1"))));
                             rowCopy.Descendants<TableCell>().ElementAt(1).Append(new Paragraph(new Run(new Text("Test1"))));
                             rowCopy.Descendants<TableCell>().ElementAt(2).Append(new Paragraph(new Run(new Text("Test2"))));
                             rowCopy.Descendants<TableCell>().ElementAt(3).Append(new Paragraph(new Run(new Text("Test3"))));
                             rowCopy.Descendants<TableCell>().ElementAt(4).Append(new Paragraph(new Run(new Text("Test4"))));
                             rowCopy.Descendants<TableCell>().ElementAt(5).Append(new Paragraph(new Run(new Text("Test5"))));
                             TabelaZapisnik.AppendChild(rowCopy);
                             // Remove the empty placeholder row from the table.
                             TabelaZapisnik.RemoveChild(Red);
                             // Save the changes to the table back into the document.
                             main.Document.Save();
                                                   
    
                             //closing WordprocessingDocument automatically saves the document
                         }
                     }
                         //OpenXml kreiranje dokumenata - kraj
                        
                  
                       
                   return true;
                }
            }
            catch (SPException spEx)
            {
                // file already exists?
                if (spEx.ErrorCode == -2130575257)
                    return false;
                else
                    throw spEx;
            }

从文档库中的模板创建的文档已创建,但未使用以下值更新:Test1、Test2、Test3、Test4 和 Test5。

标签: sharepoint-2010openxml-sdk

解决方案


推荐阅读