3

我试图让 xlsx 文件的前 4 行在打印时在每页的顶部重复。我正在使用 Open XML SDK 来完成此操作。

我的文件由 SSRS 执行 Web 服务作为文件流生成。然后,在将文件交付给用户之前,我需要修改几个布局和页面设置(边距、页面大小、缩放等),因为我的用户对 Excel 的敏锐度非常低。我需要调整的所有其他设置都可以正常工作,因此我已经从这个示例中删除了所有这些代码。

当尝试使用下面的代码设置重复的页眉时,它执行没有错误。但是,当我在执行代码后尝试打开文件时,我收到一条损坏的文件消息,并且无法打开。

知道我在这里做错了什么吗?!这让我疯狂!

using (SpreadsheetDocument xl = SpreadsheetDocument.Open("C:\\" + filename, true))
{
WorkbookPart wbp = xl.WorkbookPart;

//-----------------------------------------
// repeat rows at top when printing
//-----------------------------------------
DefinedNames dn = new DefinedNames();
wbp.Workbook.Append(dn);
wbp.Workbook.Save();

DefinedNames definedNames = wbp.Workbook.Descendants<DefinedNames>().FirstOrDefault();
DefinedName n1 = new DefinedName() { Name = "_xlnm.Print_Titles", LocalSheetId = (UInt32Value)0U };
n1.Text = "alpha_sort_nc!$1:$4";
definedNames.Append(n1);
wbp.Workbook.Save();

xl.Close();
}
4

3 回答 3

4

更新: @Duncan - 在您发表评论后,我试图找出文件损坏的根本原因。原来SpreadsheetPinterSettings是文档中的二进制数据。甚至将 DefinedName 添加_xlnm.Print_Titles到电子表格;它在Print/Print Preview期间没有任何效果。

处理电子表格打印机设置的 Open XML 方法似乎没有帮助。

反映来自 Open XML SDK 生产力工具的代码:在 MS Excel 中手动创建打印标题

// Adds child parts and generates content of the specified part.
public void CreateSpreadsheetPrinterSettingsPart(SpreadsheetPrinterSettingsPart part)
{
    GeneratePartContent(part);

}

// Generates content of part.
private void GeneratePartContent(SpreadsheetPrinterSettingsPart part)
{
    System.IO.Stream data = GetBinaryDataStream(partData);
    part.FeedData(data);
    data.Close();
}

#region Binary Data
private string partData = "MQAyADcALgAwAC4AMAAuADEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";

private System.IO.Stream GetBinaryDataStream(string base64String)
{
    return new System.IO.MemoryStream(System.Convert.FromBase64String(base64String));
}

#endregion

替代方法[我不确定,您是否感兴趣:)]:由于二进制数据部分,使用互操作的相同代码片段。完美运行!

Excel.Application xlApp = new Excel.Application();
Excel._Workbook xlWorkbook = xlApp.Workbooks.Open(@"C:/Users/jeeva/sample-excel-files/SampleExcel.xlsx");
Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];

xlWorksheet.PageSetup.PrintTitleRows = "$1:$1";

xlWorkbook.Save();
xlWorkbook.Close();

前一个:

这三行不是必要的。

DefinedNames dn = new DefinedNames();
wbp.Workbook.Append(dn);
wbp.Workbook.Save();

要设置打印标题,DefinedName Text 的格式应为"Worksheet_name!Row_Range or Column_Range"
例如:行范围:Worksheet1!$1:$1和列范围:Worksheet1!$A:$A

using (SpreadsheetDocument xl = SpreadsheetDocument.Open(@"C:/Users/jeeva/sample-excel-files/SampleExcel.xlsx", true))
{             
    WorkbookPart wbp = xl.WorkbookPart;

    // Input for this program
    string name = "_xlnm.Print_Titles";
    string comment = "this is Print Titles for Sheet1";
    string text = "Sheet1!$1:$1";

    if (wbp.Workbook.DefinedNames == null) 
    {
        wbp.Workbook.DefinedNames = new DefinedNames();
    }                

    bool bFound = false;
    foreach (DefinedName d in wbp.Workbook.DefinedNames)
    {
        // if found overwrite existing defined name
        if (d.Name.Value.Equals(name, StringComparison.InvariantCultureIgnoreCase)) 
        {
            bFound = true;
            d.Text = text;
            d.Comment = comment;
            break;
        }
    }

    if (!bFound) // if not found, add one
    {
        wbp.Workbook.DefinedNames.Append(getDefinedName(name, text, comment));
    }

    wbp.Workbook.Save();
    xl.Close();
}

private static DefinedName getDefinedName(string Name, string Text, string Comment)
{
    DefinedName dn = new DefinedName();
    dn.Text = Text;
    dn.Name = Name;
    dn.Comment = Comment;
    dn.LocalSheetId = (UInt32Value)1U;
    dn.Xlm = true; // since its a special schema name
}
于 2012-11-28T11:27:22.537 回答
0

下面的代码块解决了我的问题:

string fileName = AppDomain.CurrentDomain.BaseDirectory + "/Content/Master-License-Export.xlsx";
                    var spreadsheetDocument = SpreadsheetDocument.Open(fileName, true);           
                    WorkbookPart wbp = spreadsheetDocument.WorkbookPart;

                    UInt32 localSheetId = 0;    //LocalSheetIds are 0-indexed.

                    var definedName = new DefinedName
                    {
                        Name = "_xlnm.Print_Titles",
                        LocalSheetId = localSheetId,
                        Text = String.Format("\'{0}\'!${1}:${2}", "Master-License-Export", 1, 4)
                    };

                    if (wbp.Workbook.DefinedNames == null)
                    {
                        var definedNamesCol = new DefinedNames();
                        wbp.Workbook.Append(definedNamesCol);
                    }

                    wbp.Workbook.DefinedNames.Append(definedName);

                    wbp.Workbook.Save();
                    spreadsheetDocument.Close(); 
于 2018-01-22T09:55:58.610 回答
-1

这可以通过ClosedXML

Worksheet.PageSetup.SetRowsToRepeatAtTop(1, 5);
于 2016-03-16T15:29:49.713 回答