我希望有人遇到问题,并有一个解决方案.
我能够使用DocumentFormat.OpenXml.dll库成功创建Excel文件.
问题是如果我的代码冻结了前x行,后来当我在Excel中打开文档并尝试使用“打印”对话框时,我得到其中一个“Microsoft Excel已停止工作”对话框.
如果我解冻行,保存文件并重新打开它,打印对话框工作正常.我甚至可以手动冻结行,保存文件并在Excel中打开它;打印对话框工作正常.
我甚至在OpenXML Productivity工具中打开了这两个文件,两者中的代码看起来都是一样的.
所以我正在做的事情肯定有其他问题.
如果它有帮助,这就是完成所有事情的功能.样式表和数据表填充在别处:
public void CreateSpreadsheetWorkbook ( string filepath, List<string []> dataList, List<string> excelHeader )
{
SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create ( filepath, SpreadsheetDocumentType.Workbook ); // Create a spreadsheet document by supplying the filepath.By default, AutoSave = true, Editable = true, and Type = xlsx.
WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart (); // Add a WorkbookPart to the document.
workbookpart.Workbook = new Workbook ();
WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart> (); // Add a WorksheetPart to the WorkbookPart.
//Worksheet worksheet1 = new Worksheet ();
Worksheet worksheet1 = new Worksheet () { MCAttributes = new MarkupCompatibilityAttributes () { Ignorable = "x14ac" } };
SheetData sheetData1 = new SheetData ();
Columns columns1 = new Columns ();
worksheet1.AddNamespaceDeclaration ( "r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships" );
worksheet1.AddNamespaceDeclaration ( "x", "http://schemas.openxmlformats.org/spreadsheetml/2006/main" );
worksheet1.AddNamespaceDeclaration ( "x14ac", "http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac" );
//
// Setup Freeze and SheetDimensions data elements...
//
string strFreezeRow = "";
double numRows = 0.00;
if ( xlRegular_Heading != "" )
{
numRows = 4;
strFreezeRow = "A5";
}
else
{
numRows = 1;
strFreezeRow = "A2";
}
int numSheetRows = numRecordCount + (int)numRows;
SheetViews sheetViews1 = new SheetViews ();
SheetDimension sheetDimension1 = new SheetDimension () { Reference = "A1:" + strHighestColumn + numSheetRows.ToString() };
SheetFormatProperties sheetFormatProperties1 = new SheetFormatProperties () { DefaultRowHeight = 11.25D, DyDescent = 0.2D };
sheetFormatProperties1.AddNamespaceDeclaration ( "x14ac", "http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac" );
if ( oPrinterSetup.psFreezeRows )
{
SheetView sheetView1 = new SheetView () { TabSelected = true, WorkbookViewId = (UInt32Value) 0U };
Pane pane1 = new Pane () { VerticalSplit = numRows, TopLeftCell = strFreezeRow, ActivePane = PaneValues.BottomLeft, State = PaneStateValues.Frozen };
Selection selection1 = new Selection () { Pane = PaneValues.BottomLeft, ActiveCell = strFreezeRow, SequenceOfReferences = new ListValue<StringValue> () { InnerText = strFreezeRow } };
sheetView1.Append ( pane1 );
sheetView1.Append ( selection1 );
sheetViews1.Append ( sheetView1 );
}
uint x = 0;
for ( x = 0; x < layoutList.Count; x++ )
{
uint numIndex = x + 1;
Column column1 = new Column () { Min = numIndex, Max = numIndex, Width = layoutList [ (int) x ].numColumnWIDTH, Style = 2, CustomWidth = true };
columns1.Append ( column1 );
}
worksheet1.Append ( sheetDimension1 );
if ( oPrinterSetup.psFreezeRows )
worksheet1.Append ( sheetViews1 );
worksheet1.Append ( sheetFormatProperties1 );
worksheet1.Append ( columns1 );
worksheet1.Append ( sheetData1 );
if ( xlRegular_Heading != "" )
{
MergeCells mergeCells1 = new MergeCells () { Count = (UInt32Value) 2U };
MergeCell mergeCell1 = new MergeCell () { Reference = "A1:B1" };
MergeCell mergeCell2 = new MergeCell () { Reference = "A2:" + strHighestColumn + "2" };
mergeCells1.Append ( mergeCell1 );
mergeCells1.Append ( mergeCell2 );
worksheet1.Append ( mergeCells1 );
}
worksheet1.Append ( pageMargins );
worksheet1.Append ( pageSetup );
if ( oPrinterSetup.psLeftHeader != "" && oPrinterSetup.psCenterHeader != "" && oPrinterSetup.psRightHeader != "")
worksheet1.Append ( AddHeader() );
worksheetPart.Worksheet = worksheet1;
Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild<Sheets> ( new Sheets () ); // Add Sheets to the Workbook.
Sheet sheet = new Sheet () { Id = spreadsheetDocument.WorkbookPart.GetIdOfPart ( worksheetPart ), SheetId = 1, Name = "MySheet" }; // Append a new worksheet and associate it with the workbook.
sheet.AddNamespaceDeclaration ( "r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships" );
DefinedNames definedNames1 = new DefinedNames ();
DefinedName definedName1 = new DefinedName () { Name = "_xlnm.Print_Titles", LocalSheetId = (UInt32Value) 0U };
sheets.Append ( sheet );
SheetData sData = worksheetPart.Worksheet.GetFirstChild<SheetData> (); // data for the sheet
excelHeaderMethod ( spreadsheetDocument, sData, _headerColumns, excelHeader ); // Export header
ForeachToExcel ( spreadsheetDocument, sData, _headerColumns, dataList, excelHeader ); // Export data content
definedName1.Text = "MySheet!" + oPrinterSetup.psPrintTitleRows;
definedNames1.Append ( definedName1 );
workbookpart.Workbook.Append ( definedNames1 );
workbookpart.Workbook.Save ();
spreadsheetDocument.Close (); // Close the document.
}
最佳答案 我通过向Workbook对象添加一个空的WorkbookView来修复此问题:
workbookpart.Workbook.BookViews = new BookViews(new WorkbookView());
Excel必须需要WorkbookView才能生成打印预览.