我已经使用 OpenXML 在 C# 中生成了 Word/docx 文档。使用 altChunk 添加/合并了另一份 docx 文档。当您打开该文档时,它看起来很棒。但进一步我需要使用 C# 代码处理该文档。我看到仍然存在 altChunk,它实际上具有指向合并文档的二进制序列化内容的指针。如果您在 Word - SaveAs 中执行此操作,并将文档保存到新文件并反映该文档(OpenXML 工具或 VS 扩展),那么您会发现 Word 引擎已将 altChunk 转换为段落。那太棒了。但我无法通过 C# 代码实现这一点。
如果我复制到新文件并保存,则什么也不会发生:
public static void SaveAgain(string masterDocumentPath)
{
string newDocumentPath = @"F:\test.docx";
using (Stream original = new FileStream(masterDocumentPath, FileMode.Open))
{
using (FileStream fs = File.Create(newDocumentPath))
{
original.CopyTo(fs);
}
}
using (WordprocessingDocument myDocNew =
WordprocessingDocument.Open(newDocumentPath, true))
{
myDocNew.MainDocumentPart.Document.Save();
}
}
Run Code Online (Sandbox Code Playgroud)
我在构建代码时使用了这篇文章(如何使用 altChunk 进行文档组装)。我缺少什么?
更新
我已经通过 Interop 成功地做到了这一点。这是片段:
public static string SaveAsWithInterop(string masterDocumentPath)
{ …Run Code Online (Sandbox Code Playgroud) 我的 Excel 文件中的数值用“.”存储。小数分隔符。我想知道,excel总是使用“.” 作为小数点分隔符?如果没有,那么如何检索 Excel 文件的文化信息以正确格式化十进制值?
谢谢。
我正在将 docx 转换为 html 格式(使用 apache poi)并将其作为电子邮件发送。
生成的 html 片段看起来像这样
<html>
<head>
....
<style>
span.Normal{
font-family: 'Arial';font-size: 9.0pt;
}
span.Title{
font-family: 'Cambria';font-size: 28.0pt;color: #000000;
}
span.MySubtitle{
font-family: 'Arial';font-size: 18.0pt;color: #000000;
}
span.MyTitle{
font-family: 'Arial';font-size: 22.0pt;font-weight: bold;color: #000000;
}
...
</style>
</head>
<body>
....
<p class="Normal Title MyTitle">
<span id="_GoBack">
<span class="Normal Title MyTitle">Welcome Message</span>
<span class="Normal Title MyTitle"> </span>
<span class="Normal Title MyTitle">Username</span>
</p>
<p class="Normal Title MySubtitle">
<span class="Normal Title MySubtitle">Issues and Solutions</span>
</p>
...
</body>
</html>Run Code Online (Sandbox Code Playgroud)
Outlook 客户端无法识别多个 …
我的 C# 应用程序使用 OpenXML 创建一个 MSWord 文档,其中已包含多个表。最后一部分是添加条形图。我找不到这个案例的好例子。
感谢您的帮助!
我正在从头开始创建该文档。从...开始:
using (WordprocessingDocument myDoc = WordprocessingDocument.Create(documentStream, WordprocessingDocumentType.Document, autoSave: true))
Run Code Online (Sandbox Code Playgroud)
然后我在 C# 代码中添加新的表格和段落。在我到达条形图之前,所有这些都在起作用。我找到了一个示例项目,它将饼图插入到Word文档中,但我不明白转换它的图表类型之间的差异。这是我找到的饼图示例项目:
https://code.msdn.microsoft.com/office/How-to-create-Chart-into-a7d424f6
感谢您的帮助!
我开发了一个应用程序,该应用程序应该将图像添加到Word文档中。它从 Word 文档中获取副本并将图片添加到副本中。我尝试添加文本,效果很好,但是通过添加图像,word 文档想要打开一个文件,并给出错误(无法打开文件“名称”,因为内容存在问题。)
我的代码如下所示:
File.Copy(file, newFile, true);
WordprocessingDocument wordFile = WordprocessingDocument.Open(newFile, true);
Body body = wordFile.MainDocumentPart.Document.Body;
var picture = new Picture();
var shape = new Shape() { Style = "width: 20px; height: 20px" };
var imageData = new ImageData() { RelationshipId = "img" };
shape.Append(imageData);
picture.Append(shape);
wordFile.MainDocumentPart.AddExternalRelationship(
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
new Uri(link_of_image, UriKind.Absolute),"img");
Paragraph para = body.AppendChild(new Paragraph());
Run run = para.AppendChild(new Run());
run.AppendChild(new Picture(picture));
wordFile.Close();
Run Code Online (Sandbox Code Playgroud)
可能出什么问题了?
我有一个 C# 网络应用程序。我正在使用 xlsx 模板来创建文档。xlsx 的格式相当复杂,因此我选择了模板路线,而不是从头开始创建。在 x2 单元格中,大多数数据以百分比形式出现,但在某些情况下,它会是一个金额。但当该单元格的数量为百分比时,我似乎无法更改该单元格的格式。模板以百分比格式保存在该单元格中。设置单元格值的代码如下,适用于以货币格式保存的其他单元格。我尝试根据另一个具有相同格式的单元格中的代码将单元格样式索引设置为 103U。
如何更改单元格 x2 的格式?任何帮助将不胜感激。预先感谢。`
public void setCellValueNum(WorksheetPart ws, int row, int col, Double newVal)
{
Cell cl = getCell(ws.Worksheet, getColLetter(col), row);
cl.CellValue = new CellValue(newVal.ToString());
cl.CellReference = getColLetter(col) + row.ToString();
cl.DataType =
new EnumValue<CellValues>(CellValues.Number);
//cl.StyleIndex = (UInt32Value)103U;
}
Run Code Online (Sandbox Code Playgroud) 我在更新由 Linux 上的python-docx生成的 docx 文件中的目录时遇到问题。一般来说,创建TOC并不难(感谢这个答案/sf/answers/3403559211/和这个线程https://github.com/python-openxml/python-docx/issues/36)
from docx.oxml.ns import qn
from docx.oxml import OxmlElement
paragraph = self.document.add_paragraph()
run = paragraph.add_run()
fldChar = OxmlElement('w:fldChar') # creates a new element
fldChar.set(qn('w:fldCharType'), 'begin') # sets attribute on element
instrText = OxmlElement('w:instrText')
instrText.set(qn('xml:space'), 'preserve') # sets attribute on element
instrText.text = 'TOC \o "1-3" \h \z \u' # change 1-3 depending on heading levels you need
fldChar2 = OxmlElement('w:fldChar')
fldChar2.set(qn('w:fldCharType'), 'separate')
fldChar3 = OxmlElement('w:t')
fldChar3.text = "Right-click …Run Code Online (Sandbox Code Playgroud) 如何更改Excel中CellValue的文本颜色?我可以更改单元格的前景色,但它会更改单元格内所有文本的颜色,这是我不想要的。我想仅突出显示单元格内的特定文本,即 CellValue 文本。
我使用下面的代码突出显示单元格文本,如何为 CellValue 完成此操作?
foreach (DocumentFormat.OpenXml.Spreadsheet.Cell currentCell in allCells)
{
Fill fill = new Fill()
{
PatternFill = new PatternFill
{
PatternType = PatternValues.Solid,
ForegroundColor = new ForegroundColor() { Rgb = "FFFF00" }
}
};
styleSheet.Fills.AppendChild(fill);
//Adding the CellFormat which uses the Fill element
CellFormats cellFormats = styleSheet.CellFormats;
CellFormat cf = new CellFormat();
cf.FillId = styleSheet.Fills.Count;
cellFormats.AppendChild(cf);
currentCell.StyleIndex = styleSheet.CellFormats.Count;
}
Run Code Online (Sandbox Code Playgroud)
我在 CellValue 中没有看到 Style 的任何属性
CellValue currentCellValue = currentCell.GetFirstChild<CellValue>();
if (currentCell.DataType == CellValues.SharedString) // cell has a …Run Code Online (Sandbox Code Playgroud) 我使用下面的代码来获取页数,但它没有给出实际页数(PFA)。获取总页数的更好方法是什么?
var pageCount = doc.ExtendedFilePropertiesPart.Properties.Pages.Text.Trim();
Run Code Online (Sandbox Code Playgroud)
注意:我们无法在我的 Azure Web 应用服务中使用 Office 主互操作程序集
提前致谢。
我对 python-docx 比较陌生。我试图更改现有文档中表格的行距,但它更改了文档中所有表格的行距。
这是一个最小的、可重现的示例,从头开始创建一个包含三个表的文档:
from docx import Document
from docx.shared import Inches
from docx.shared import Pt
from docx.enum.text import WD_LINE_SPACING
document = Document()
# Some sample text to add to tables
records = (
(3, '101', 'Spam'),
(7, '422', 'Eggs'),
(4, '631', 'Spam, spam, eggs, and spam')
)
# Create table 0
table = document.add_table(rows=1, cols=3)
hdr_cells = table.rows[0].cells
hdr_cells[0].text = 'Qty'
hdr_cells[1].text = 'Id'
hdr_cells[2].text = 'Desc'
for qty, id, desc in records:
row_cells = table.add_row().cells
row_cells[0].text = str(qty) …Run Code Online (Sandbox Code Playgroud) openxml ×10
c# ×4
excel ×2
ms-word ×2
openxml-sdk ×2
python ×2
python-docx ×2
.net ×1
apache-poi ×1
css ×1
culture ×1
cultureinfo ×1
docx ×1
outlook ×1
outlook-2010 ×1