我想访问一个打开的xml word doc文件的一些内置属性(如作者,上次修改日期等).我想为此目的使用open xml sdk2.0.所以我想知道是否有任何类或任何方式我可以编程访问这些内置属性.
我在C#中使用服务器端的xslx Excel文件.在电子表格中,总共有15列(单元格).在单元格行中,缺少某些值.所以第一行是我的标题将正确拥有15个单元格.但是我的数据行,一些单元格可能有空值,因此Open XML有一组"锯齿状"的单元格值.第1行将包含完整的15个单元格,第2行可能包含13个单元格,因为其中两个值为空.什么!如何正确映射此数据?它基本上将一切都移到左边,我的单元格值是错误的.我错过了什么?看起来他们正在Open XML术语中"崩溃".
WorkbookPart workbookPart = spreadSheetDocument.WorkbookPart;
IEnumerable<Sheet> sheets = spreadSheetDocument.WorkbookPart.Workbook.GetFirstChild<Sheets>().Elements<Sheet>();
string relationshipId = sheets.First().Id.Value;
WorksheetPart worksheetPart = (WorksheetPart)spreadSheetDocument.WorkbookPart.GetPartById(relationshipId);
Worksheet workSheet = worksheetPart.Worksheet;
SheetData sheetData = workSheet.GetFirstChild<SheetData>();
IEnumerable<Row> rows = sheetData.Descendants<Row>();
Run Code Online (Sandbox Code Playgroud)
澄清:这是提出这个问题的另一种方式.如果我想将内容放在Excel文件中并将其放入DataTable,该怎么办?我希望所有数据列都正确排列.我怎么能做到这一点?
这个问题比我在这里要好: 阅读Excel Open XML忽略了空白单元格
我遇到一个问题,一旦我引用幻灯片,就会在powerpoint文档中删除空格.以下代码示例说明了我的意思 -
//Open the document.
using(PresentationDocument presentationDocument = PresentationDocument.Open(pptxFileName, true))
{
//Just making this reference modifies the whitespace in the slide.
Slide slide = presentationDocument.PresentationPart.SlideParts.First().Slide;
}
Run Code Online (Sandbox Code Playgroud)
要重现此问题,请使用单个幻灯片创建演示文稿,其中包含单个文本框,其中包含文本"[]"(无引号).现在,将方括号之间的空格字体设置为与文本其余部分不同的颜色.这将导致Run仅包含空格字符.一旦上面的代码针对此演示文稿运行,引用幻灯片的行将导致运行中的空白消失,最终使我们的视觉更改演示文稿比我们最初开始时,即使我们从未明确更改任何内容 - 在powerpoint应用程序中打开时,文本现在将为"[]".
在Word中,可以将xml:space属性设置为"保留"文本元素以保留空格,但似乎没有Powerpoint的等效项.
在将空白用作幻灯片设计的关键组件的情况下,这是一个关键问题.有没有人找到解决这个问题的方法?
我正在使用asp.net开发一个Web应用程序,我有一个名为Template.docx的文件,它像模板一样生成其他报告.在这个Template.docx中,我有一些MergeFields(Title,CustomerName,Content,Footer等)来代替C#中的一些动态内容.
我想知道,如何在docx的mergefield中放置内容?
我不知道MergeFields是否是正确的方法,或者是否有另一种方法.如果你能推荐我,我很感激!
PS:我的web应用程序中引用了openxml.
编辑:
private MemoryStream LoadFileIntoStream(string fileName)
{
MemoryStream memoryStream = new MemoryStream();
using (FileStream fileStream = File.OpenRead(fileName))
{
memoryStream.SetLength(fileStream.Length);
fileStream.Read(memoryStream.GetBuffer(), 0, (int) fileStream.Length);
memoryStream.Flush();
fileStream.Close();
}
return memoryStream;
}
public MemoryStream GenerateWord()
{
string templateDoc = "C:\\temp\\template.docx";
string reportFileName = "C:\\temp\\result.docx";
var reportStream = LoadFileIntoStream(templateDoc);
// Copy a new file name from template file
//File.Copy(templateDoc, reportFileName, true);
// Open the new Package
Package pkg = Package.Open(reportStream, FileMode.Open, FileAccess.ReadWrite);
// Specify the URI of the part to be …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
using (var doc = WordprocessingDocument.Open(filename, true))
{
....
}
Run Code Online (Sandbox Code Playgroud)
其中string filename是docx文件的有效路径.
但是调用Open会导致以下InvalidOperationException: Specified part does not exist in the package.
编辑:我认为问题是由于我试图打开的docx文件无效,所以我将发布xml:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 wp14">
<w:body xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:p w:rsidRPr="005501CD" w:rsidR="00217D87" w:rsidP="005501CD" w:rsidRDefault="005501CD">
<w:r>
<w:t xml:space="preserve">Hello </w:t>
</w:r>
<w:r>
<w:rPr>
<w:noProof />
</w:rPr>
<w:t>John</w:t>
</w:r>
<w:r>
<w:t xml:space="preserve"> </w:t>
</w:r>
<w:r>
<w:rPr>
<w:noProof />
</w:rPr>
<w:t>Smith</w:t>
</w:r>
<w:r>
<w:t>,</w:t>
</w:r>
<w:bookmarkStart …Run Code Online (Sandbox Code Playgroud) 我无法弄清楚如何使用OpenXML SDK 2.0在特定点开始页面编号.这是我在使用OpenXML Productivity Tool反映文档中的标题时看到的内容:
<w:hdr xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 wp14">
<w:p w:rsidR="00FC0BC9" w:rsidP="00FC0BC9" w:rsidRDefault="005F46AD">
<w:pPr>
<w:pStyle w:val="Header" />
<w:ind w:right="360" />
<w:jc w:val="right" />
</w:pPr>
<w:r>
<w:t xml:space="preserve">I-1 Page </w:t>
</w:r>
<w:r>
<w:fldChar w:fldCharType="begin" />
</w:r>
<w:r>
<w:instrText xml:space="preserve"> PAGE \* Arabic \* MERGEFORMAT </w:instrText>
</w:r>
<w:r>
<w:fldChar w:fldCharType="separate" />
</w:r>
<w:r w:rsidR="00C62387">
<w:rPr>
<w:noProof />
</w:rPr>
<w:t>1</w:t>
</w:r>
<w:r>
<w:fldChar w:fldCharType="end" />
</w:r>
</w:p>
Run Code Online (Sandbox Code Playgroud)
它看起来像是使用一组运行构建字段,然后通过包含一个Text值为"1"的Run来启动此标题的页面编号为1 - …
我需要从打开的xml文档中获取特定表的xml innerText == "something" & "somethingelse"
示例:
using (WordprocessingDocument doc = WordprocessingDocument.Open(path, false))
{
MainDocumentPart mainPart = doc.MainDocumentPart;
string xml = mainPart.Document.Descendants<Table>().Select // where innerText == "this" || innerText == "that"
Console.WriteLine(xml);
MainDocumentPart documentPrincipal = document.MainDocumentPart;
documentPrincipal.Document.InnerXml = documentPrincipal.Document.InnerXml.Replace(replacethisby, that);
documentPrincipal.Document.Save();
document.Dispose();
}
Run Code Online (Sandbox Code Playgroud)
我该如何实现这一目标?非常感谢.
有几个相关的问题,但我找不到反映我的情况的问题.
我正在使用SXSSFWorkbook和SXSSFSheet对象使用Apache POI写出Excel"xlsx"文件.该文件创建没有问题,并在LibreOffice中打开正常,但是,Excel在打开文件时会抱怨.
Excel在'test-file.xlsx'中找到了不可读的内容.是否要恢复工作簿的内容?如果您信任此工作簿的来源,请单击"是".
选择"是"时...
Excel能够通过修复或删除不可读的内容来打开该文件.
已移除的功能:来自/xl/styles.xml部分的格式(样式)
修复记录:来自/xl/worksheets/sheet1.xml部分的单元信息
创建这个工作簿的代码非常无聊,我没有设置任何样式或任何有趣的东西.我创建工作簿和一个工作表,然后将数据写入其中.
private Workbook createWorkbook(final String sheetName, final String[] headers) {
// create a new workbook and sheet
final SXSSFWorkbook workbook = new SXSSFWorkbook(500);
final SXSSFSheet sheet = (SXSSFSheet) workbook.createSheet(sheetName);
// create and fill our header row
final Row row = sheet.createRow(0);
for (int index = 0; index < headers.length; index++) {
row.createCell(index).setCellValue(headers[index]);
}
return workbook;
}
Run Code Online (Sandbox Code Playgroud)
编写数据同样无趣.
private void exportPersonWorkbook(final Workbook workbook, final String sheetName, final PersonExport personExport) {
// list of …Run Code Online (Sandbox Code Playgroud) 我知道有很多问题有相同的标题,但我目前有一些问题,他们我没有得到正确的方法去.
我使用Open xml sdk 2.5和Power工具将.docx文件转换为.html使用HtmlConverter类进行转换的文件.
我成功地将docx文件转换为Html文件,但问题是,html文件不保留文档文件的原始格式.例如.字体大小,颜色,下划线,粗体等不会反映到html文件中.
这是我现有的代码:
public void ConvertDocxToHtml(string fileName)
{
byte[] byteArray = File.ReadAllBytes(fileName);
using (MemoryStream memoryStream = new MemoryStream())
{
memoryStream.Write(byteArray, 0, byteArray.Length);
using (WordprocessingDocument doc = WordprocessingDocument.Open(memoryStream, true))
{
HtmlConverterSettings settings = new HtmlConverterSettings()
{
PageTitle = "My Page Title"
};
XElement html = HtmlConverter.ConvertToHtml(doc, settings);
File.WriteAllText(@"E:\Test.html", html.ToStringNewLineOnAttributes());
}
}
}
Run Code Online (Sandbox Code Playgroud)
所以我只想知道是否有任何方法可以保留转换后的HTML文件中的格式.
我知道一些第三方API做同样的事情.但我更喜欢使用open xml或任何其他开源来做这件事.
我有一个XML文件,其中包含我需要在Word文档上填充的数据.
我需要找到一种方法,定义一个模板,该模板可以用作从XML文件填充数据并创建输出文档的基线.
我相信有两种方法可以做到这一点.
我只是不知道如何实现这两种方式的细节.或者不确定是否有另一种更简单的方法来完成此任务.
有人可以展示如何实现这一点的例子.只是一个简单的例子就足够了.
对于任何编码,我更喜欢C#.我正在使用Word 2016,但希望它从Word 2007兼容到Word 2016以及介于两者之间的所有内容,因为用户将使用这些版本.谢谢!
openxml ×10
c# ×8
openxml-sdk ×5
.net ×2
asp.net ×2
excel ×2
apache-poi ×1
docx ×1
drawingml ×1
html ×1
ms-word ×1
powerpoint ×1
xml ×1