OpenXML自定义列宽不起作用

时间:2016-10-17 15:01:10

标签: c# openxml

我试图更改某些Excel列的宽度。我已经尝试了所有内容,但我一直在获取空的excel文档,我的excel文件在没有添加的情况下正常工作:

Columns columns = new Columns();

columns.Append(new Column() { Min = 1, Max = 3, Width = 20, CustomWidth = true });
columns.Append(new Column() { Min = 4, Max = 4, Width = 30, CustomWidth = true });

wsPart.Worksheet.Append(columns);

但是一旦我添加了这个like this user suggested,我就必须修复我的excel文件,它将是空的。我收到此错误:"替换部件:/xl/worksheets/sheet.xml部分出现XML错误。加载错误。第1行,第0列。 &#34 ;.并且修复了'文件是空的。

我的代码与MSDN example

的代码基本相同

我尝试了很多不同的方法来改变列宽,所以我认为问题出在其他地方。我似乎无法弄清楚错误是什么。

编辑:完整代码

_document = SpreadsheetDocument.Create(newFileName, SpreadsheetDocumentType.Workbook);

_workbookPart = _document.AddWorkbookPart();
_workbookPart.Workbook = new Workbook();

WorksheetPart worksheetPart = _workbookPart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = new Worksheet(new SheetViews(new SheetView { WorkbookViewId = 0, ShowGridLines = new BooleanValue(false) }), new SheetData());

_document.WorkbookPart.Workbook.AppendChild(new Sheets());

Sheet sheet = new Sheet { Id = _document.WorkbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "Sheet1" };
_workbookPart.Workbook.Sheets.Append(sheet);


WorkbookStylesPart stylesPart = _workbookPart.AddNewPart<WorkbookStylesPart>();
stylesPart.Stylesheet = CreateStylesheet();
stylesPart.Stylesheet.Save();

Columns columns = new Columns();

columns.Append(new Column() { Min = 1, Max = 3, Width = 20, CustomWidth = true });
columns.Append(new Column() { Min = 4, Max = 4, Width = 30, CustomWidth = true });

worksheetPart.Worksheet.Append(columns);

_sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>();

1 个答案:

答案 0 :(得分:5)

您的元素的顺序略有不同。 Columns应放在之前 SheetData而不是之后。工作表的XML模式的相关部分是:

<xsd:complexType name="CT_Worksheet">
    <xsd:sequence>
    <xsd:element name="sheetPr" type="CT_SheetPr" minOccurs="0" maxOccurs="1"/>
    <xsd:element name="dimension" type="CT_SheetDimension" minOccurs="0" maxOccurs="1"/>
    <xsd:element name="sheetViews" type="CT_SheetViews" minOccurs="0" maxOccurs="1"/>
    <xsd:element name="sheetFormatPr" type="CT_SheetFormatPr" minOccurs="0" maxOccurs="1"/>
    <xsd:element name="cols" type="CT_Cols" minOccurs="0" maxOccurs="unbounded"/>
    <xsd:element name="sheetData" type="CT_SheetData" minOccurs="1" maxOccurs="1"/>

要修复您的代码,您可以从SheetData行中删除new Worksheet,然后在SheetData之后添加Columns

worksheetPart.Worksheet = new Worksheet(new SheetViews(new SheetView { WorkbookViewId = 0, ShowGridLines = new BooleanValue(false) }));

//....code omitted for brevity

Columns columns = new Columns();

columns.Append(new Column() { Min = 1, Max = 3, Width = 20, CustomWidth = true });
columns.Append(new Column() { Min = 4, Max = 4, Width = 30, CustomWidth = true });

worksheetPart.Worksheet.Append(columns);
worksheetPart.Worksheet.Append(new SheetData());

或者您可以保留new Worksheet代码,并在添加Columns时使用InsertBefore方法在SheetData之前插入它们:

Columns columns = new Columns();

columns.Append(new Column() { Min = 1, Max = 3, Width = 20, CustomWidth = true });
columns.Append(new Column() { Min = 4, Max = 4, Width = 30, CustomWidth = true });

var sheetdata = worksheetPart.Worksheet.GetFirstChild<SheetData>();
worksheetPart.Worksheet.InsertBefore(columns, sheetdata);

还有一点需要注意 - 你应该将SpreadsheetDocument.Create包装在using语句中。这将清理所有资源并在更改结束时为您保存文件,例如:

using (var _document = SpreadsheetDocument.Create(filename, SpreadsheetDocumentType.Workbook))
{
    //all your OpenXml code here...
}