我有一个c#程序,可以生成格式化的Excel报告。由于Office的升级,我将程序转换为使用Microsoft Excel 16对象库。除了pagesetup类之外,其他一切都很好。新的DLL中不再支持Pagesetup类。由于该报告的代码很广泛,所以我决定最后使用OpenXML修改Excel文件。
我通过将文件的后缀更改为zip来研究原始的OpenXML。下面是与pagesetup类相对应的原始OpenXML。
<printOptions horizontalCentered="1"/>
<pageMargins left="0.5" right="0.5" top="0.75" bottom="0.75" header="0.3" footer="0.3"/>
<pageSetup orientation="landscape" r:id="rId1"/>
<headerFooter><oddHeader>&L&"Arial Narrow,Bold"&8 9825 Fairmount Drive SE
&R&"Arial Narrow,Bold"&8Ref. No.: 10-219</oddHeader>
<oddFooter>&C&"Arial Narrow,Regular"&8Page 1 of 2</oddFooter></headerFooter>
<headerFooter>
<oddHeader>&R&8&B&"Arial Narrow"Ref. No.: 10-219
&L&8&B&"Arial Narrow"9825 Fairmount Drive SE</oddHeader>
<oddFooter>&C&8&"Arial Narrow"Page 2 of 2</oddFooter></headerFooter>
但是,在用OpenXML函数格式化Excel文件之后,Excel告诉我文件中存在OpenXML代码错误。下面是从修订后的文件中提取的内容:
<x:pageMargins left="0.5" right="0.5" top="0.75" bottom="0.75" header="0.3" footer="0.3" />
<x:printOptions horizontalCentered="1" />
<x:pageSetup orientation="landscape" />
<x:headerFooter differentOddEven="0">
<x:oddHeader>&L&B&"Arial Narrow"&9825 Fairmount Drive SE&R&B&"Arial Narrow"&8Ref. No.: 10-219</x:oddHeader>
<x:oddFooter>&C&"Arial Narrow"&8Page &P of &N</x:oddFooter></x:headerFooter>
似乎在每个标签的开头都放置了x:和/ x:。下面是我的OpenXML函数。
public void formatLandscapeReport(String fileName, String jobNumber, String Address) {
using (SpreadsheetDocument document = SpreadsheetDocument.Open(fileName, true))
{
WorkbookPart workbookpart = document.WorkbookPart;
IEnumerable<String> worksheetIds = workbookpart.Workbook.Descendants<Sheet>().Select(w => w.Id.Value);
WorksheetPart worksheetpart = null;
foreach (String worksheetId in worksheetIds)
{
worksheetpart = ((WorksheetPart)workbookpart.GetPartById(worksheetId));
PrintOptions po = new PrintOptions();
po.HorizontalCentered = true;
worksheetpart.Worksheet.AppendChild(po);
PageMargins pm = worksheetpart.Worksheet.Descendants<PageMargins>().FirstOrDefault();
if (pm == null)
{
pm = new PageMargins();
worksheetpart.Worksheet.AppendChild(pm);
}
pm.Left = .5D;
pm.Right = .5D;
pm.Top = .75D;
pm.Bottom = .75D;
PageSetup pagesetup = worksheetpart.Worksheet.Descendants<PageSetup>().FirstOrDefault();
if (pagesetup == null)
{
pagesetup = new PageSetup();
worksheetpart.Worksheet.AppendChild(pagesetup);
}
pagesetup.Orientation = OrientationValues.Landscape;
// Header and Footer
HeaderFooter hf = new HeaderFooter();
hf.DifferentOddEven = false;
OddHeader ohdr = new OddHeader();
OddFooter oftr = new OddFooter();
ohdr.Text= "&L&B&\"Arial Narrow\"&" + Address +"&R&B&\"Arial Narrow\"&8Ref. No.: " + jobNumber;
oftr.Text = "&C&\"Arial Narrow\"&8Page &P of &N";
hf.Append(ohdr);
hf.Append(oftr);
worksheetpart.Worksheet.Append(hf);
worksheetpart.Worksheet.Save();
} // foreach
workbookpart.Workbook.Save();
} // using
} //formatLandscapeReport
如果有人可以让我了解我做错了什么,将不胜感激。
谢谢
卡门
答案 0 :(得分:0)
ECMA模式的相关部分是这样的:
<xsd:sequence>
...
<xsd:element name="printOptions" type="CT_PrintOptions" minOccurs="0" maxOccurs="1"/>
<xsd:element name="pageMargins" type="CT_PageMargins" minOccurs="0" maxOccurs="1"/>
<xsd:element name="pageSetup" type="CT_PageSetup" minOccurs="0" maxOccurs="1"/>
<xsd:element name="headerFooter" type="CT_HeaderFooter" minOccurs="0" maxOccurs="1"/>
....
<xsd:sequence>
请注意,此构造定义为sequence
,因此顺序很重要。
在您的原始文件中,顺序是正确的,printOptions
出现在pageMargins
之前是,但是在错误的文件中,顺序是错误的。
交换订单应该可以解决您的问题,因此问题变为“为什么订单错了?”。
这里的问题很可能是您的Worksheet
已经包含一个PageMargins
项目,因此在它们之后添加了PrintOptions
。
要解决此问题,您始终可以先执行PageMargins
代码,然后使用InsertBefore
方法在PrintOptions
之前插入PageMargins
:
PageMargins pm = worksheetpart.Worksheet.Descendants<PageMargins>().FirstOrDefault();
if (pm == null)
{
pm = new PageMargins();
worksheetpart.Worksheet.AppendChild(pm);
}
pm.Left = .5D;
pm.Right = .5D;
pm.Top = .75D;
pm.Bottom = .75D;
worksheetpart = ((WorksheetPart)workbookpart.GetPartById(worksheetId));
PrintOptions po = new PrintOptions();
po.HorizontalCentered = true;
//the PrintOptions must be before the PageMargins
worksheetpart.Worksheet.InsertAfter(po, pm);
x:
部分只是一个名称空间。我认为这不是造成您问题的原因。