sharepoint-2010 - 使用 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。
解决方案
推荐阅读
- gnuplot - 在 v5.2 中使用非线性 x 轴损坏的 gnuplot
- xcode - Xcode 状态栏中的橙色锤子代表什么?
- r - 在 R 中反向显示标签的堆叠条形图
- javascript - 如何在本机反应中创建跨平台图标?
- javascript - 解析 JSON 以提取键和值
- angular - Angular5 中的错误/异常处理 .. ResponseEntity 在异常时返回 null
- php - CakePhp 上的 LC_TIME 翻译问题
- android - Android:VectorDrawable 用于通知小图标 pre-lollipop
- sql - 查找不适合多个定义范围的所有值
- git - 尽管设置了 ssh,为什么在推送时提示输入用户名?