我需要在Open XML Wordprocessing文档中添加基本的密码保护。我可以使用COM接口,当我要处理大量文档时,这很慢;或者我可以将数据直接放入docx文件中,<w:settings> <w:documentProtection>该速度非常快。但是,查看对密码进行加密的要求似乎需要花费数小时的编程时间。有人编码过该算法吗?我在用C#编码。
我有两个Word文档(WordprocessingDocument),我想将第一个元素的内容替换为第二个元素的主体中的内容.
这就是我现在正在做的事情:
var docA = WordprocessingDocument.Open(docAPath, true);
var docB = WordprocessingDocument.Open(docBPath, true);
var containerElement = docA.MainDocumentPart.Document.Body
.Descendants<SdtBlock>()
.FirstOrDefault(sdt => sdt.SdtProperties.Descendants<SdtAlias>().Any(alias => alias.Val == containerElementName))
.SdtContentBlock;
var elementsToCopy = docB.MainDocument.Part.Document.Body.ChildElements.Where(e => e.LocalName != "sectPr"));
containerElement.RemoveAllChildren();
containerElement.Append(elementsToCopy);
Run Code Online (Sandbox Code Playgroud)
基本上我从第一个文档获取容器(一个SdtBlock)使用它的别名来识别它,然后获取第二个元素的所有子元素(删除我不想复制的SectionProperties),然后尝试将它们添加到容器元素.
问题是我得到了这个例外:
Cannot insert the OpenXmlElement "newChild" because it is part of a tree.
Run Code Online (Sandbox Code Playgroud)
当我调用该代码的最后一行(Append)时.
关于如何实现我想要的任何想法?
我的预期结果是:
但当我使用以下代码时:
MainDocumentPart mainDocumentPart =
package.AddMainDocumentPart();
DocumentFormat.OpenXml.Wordprocessing.Document elementW =
new DocumentFormat.OpenXml.Wordprocessing.Document(
new Body(
new DocumentFormat.OpenXml.Wordprocessing.Paragraph(
new NumberingProperties(
new NumberingLevelReference() { Val = 0 },
new NumberingId() { Val = 1 })
),
new Run(
new RunProperties(),
new Text("Hello, ") { Space = new DocumentFormat.OpenXml.EnumValue<DocumentFormat.OpenXml.SpaceProcessingModeValues> { InnerText = "preserve" } })),
new DocumentFormat.OpenXml.Wordprocessing.Paragraph(
new ParagraphProperties(
new NumberingProperties(
new NumberingLevelReference() { Val = 0 },
new NumberingId() { Val = 1 })),
new Run(
new RunProperties(),
new Text("world!")
{
Space …Run Code Online (Sandbox Code Playgroud) 我见过的所有示例和实现都使用了某些类型的代码,例如:
//filePath is some path to a docx file
using (WordprocessingDocument wpd = WordprocessingDocument.Open(filePath, true))
{
//Do stuff here
}
Run Code Online (Sandbox Code Playgroud)
这需要关闭您的文件.我希望能够在已经打开的文档上使用Open XML SDK操作,因为我想在用户主动查看文档时做一些事情,我不一定要保存它.
这可能吗?我意识到如果文档打开,Word可能会锁定文档,因此您无法打开文件(即使是只读文件).它有什么办法吗?
如果我能以某种方式在已打开的文档上使用Open XML SDK,那将是非常好的.我有一个想法是暂时保存已打开的文件,并在临时文件上运行OpenXML内容,并以某种方式使用Office API将其与现有文档进行协调.没有想过这种方法,但这不是我想要的理想方式.
我也知道Word API上的一个属性,它通过执行返回XML字符串Word.Range.XML.但是,我不确定如何将此字符串值加载到SDK中,以便我可以利用其方法来帮助我.
我使用MathML创建一些数据块,我需要将它通过OpenXML SDK插入到docx文件中.我听说有可能,但我没有管理它.有人可以帮我解决这个问题吗?
我在PPTx中有一个简单的形状
我看到并在Openxml中获得了它的位置
<a:xfrm>
<a:off x="1447800" y="1066800" />
<a:ext cx="3886200" cy="990600" />
</a:xfrm>
Run Code Online (Sandbox Code Playgroud)
我拿出了这个价值观
但是我不知道如何在Open xml中获得形状,高度和坐标(左,上)以在wpf中显示相同的高度和位置.
我正在通过Open XML SDK 2.0在Word文档中生成新的段落.A具有属性的第一段,我想附加到所有新生成的段落.
像这样的东西:
var _texts = new List<string>() { "Text 1", "Text 2", "Text 1", "Text 4"};
var sdtBlock = wordDoc.MainDocumentPart.RootElement.Descendants<Paragraph>().First();
foreach (string _t in _texts)
{
Paragraph p = new Paragraph();
p.Append(sdtBlock.ParagraphProperties);
p.Append(new Run(new Text(_t)));
sdtBlock.InsertAfterSelf<Paragraph>(p);
}
Run Code Online (Sandbox Code Playgroud)
执行此代码会抛出异常:"无法插入OpenXmlElement"newChild"因为它是树的一部分." 有任何想法吗?
我有一个使用OpenXML SDK读取和写入Excel(xlsx)单元格的类.该课程基于此处最多投票的答案:Open XML SDK 2.0 - 如何更新电子表格中的单元格?
我需要更新一个单元格,然后获取另一个单元格的值,其中包含一个计算公式.更新工作正常,但是当我在更新后读取公式单元格时,我得到旧值,在编辑之前存在于doc中.但是,当我在运行程序后手动打开我的xlsx时,我可以看到正确的值.
所以看起来像单元格的旧值被缓存在某个地方......这很奇怪,因为我每次在读/写单元格之前打开/关闭我的文档.
编辑:文森特的回答让我更新了我的示例代码.我添加了一个Refresh方法,可以在后台运行的Excel应用程序中打开,保存和关闭文档.这会重新计算我的公式.有关更多详细信息和C#代码示例,请参阅:http://fczaja.blogspot.com/2013/05/how-to-read-and-write-excel-cells-with.html
我必须要消耗一些xlsx文件.我读过使用open xml sdk和http://www.dotnetperls.com/fromoadate 从xlsx读取日期.我的大多数列都是文本(共享字符串),但有一些数字(整数),我还有一些日期和日期时间.我正在使用OpenXML SDK 2.5.
我的问题是我不知道如何区分实际数字和日期.他们都具有DataType的null,和文本数表示是在Text小区的物业.
一些代码:
using (var xlsxStream = assembly.GetManifestResourceStream("Checklist.xlsx"))
using (var spreadsheetDocument = SpreadsheetDocument.Open(xlsxStream, false))
{
var workbookPart = spreadsheetDocument.WorkbookPart;
var sharedStringTable = workbookPart.SharedStringTablePart.SharedStringTable;
var worksheetPart = workbookPart.WorksheetParts.First();
var sheetData = worksheetPart.Worksheet.Elements<SheetData>().First();
string text;
foreach (Row r in sheetData.Elements<Row>())
{
foreach (Cell c in r.Elements<Cell>())
{
if (c.CellValue != null)
{
text = c.CellValue.Text;
if (c.DataType != null)
{
if (c.DataType.Value == CellValues.SharedString)
{ …Run Code Online (Sandbox Code Playgroud) 我正在将HTML文件导出到Open XML wordfile.如果在HTML <h1>中使用,我想在该部分添加Heading1样式.但不知何故,当我在Microsoft Word 2010中打开文档时,未应用样式.
如果我在Libre Office中打开创建的文档,则会应用一些样式.
我自己也定义了一些样式,如果我使用其中一种样式,Word和Libre Office中的一切都很顺利.
我为Microsoft Office打开了Open XML SDK 2.5 Productivity Tool,当我查看它提供的示例代码时,它建议:
ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId(){ Val = "Kop1" };
Kop1(而不是Heading1)是因为我的Word是荷兰语所以我的模板是荷兰语,但这并没有解决问题.
在此代码示例中,我创建了段落并向其添加样式和文本:
using (wordDocument = WordprocessingDocument.Open(documentStream, true))
{
MainDocumentPart mainPart = wordDocument.MainDocumentPart;
WP.Body body = wordDocument.MainDocumentPart.Document.Body;
WP.Paragraph para = body.AppendChild(new WP.Paragraph());
StyleDefinitionsPart part = wordDocument.MainDocumentPart.StyleDefinitionsPart;
if (part != null)
{
WP.ParagraphProperties pPr = new WP.ParagraphProperties();
WP.ParagraphStyleId paragraphStyleId1 = new WP.ParagraphStyleId() { Val = "Heading1" };
pPr.Append(paragraphStyleId1);
para.Append(pPr);
}
WP.Run …Run Code Online (Sandbox Code Playgroud)