如何设置Excel的"打印标题"与OpenXML的 [英] How to set Excel "Print Titles" with OpenXML

查看:322
本文介绍了如何设置Excel的"打印标题"与OpenXML的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何设置打印标题与OpenXML的电子表格的财产,有一排展示在每个打印页的顶部?






< DIV CLASS =h2_lin>解决方案

属性直接保存在 SpreadsheetPrintingParts 对象,不过,这似乎并没有被完全整合成的OpenXML作为然而,并要求给变量传递一个base64字符串。 (见这里)此字符串的内容似乎绑在机器上的文件被打开时,里面也没有工作对我实施 - 我是不是能够创建通过无损坏的文件SpreadsheetPrintingParts.FeedData()



<。 p>相反,我发现这个帖子其中规定给予定义的名称Print_Titles具有相同的效果的行。当时我能够通过OpenXML可以创建一个定义的名称下面的代码:

 公共无效SetPrintTitleRows(INT startRowIndex,诠释endRowIndex = NULL)
{
VAR localSheetId = _localsheetId ++; // LocalSheetIds是0索引。

变种definedName =新DefinedName
{
NAME =_xlnm.Print_Titles,
LocalSheetId = localSheetId,
文本=的String.Format(\\ !\\{0} \$ {1} $ {2},_sheetName,startRowIndex,endRowIndex ?? startRowIndex)
};

如果(_workbookPart.Workbook.DefinedNames == NULL)
{
变种definedNamesCol =新DefinedNames();
_workbookPart.Workbook.Append(definedNamesCol);
}

_workbookPart.Workbook.DefinedNames.Append(definedName);
}



注意要点:




  1. DefinedName.LocalSheetId 的索引从零开始,而不是 Sheet.Id 这是1索引

  2. DefinedNames 是每个工作簿一次,但可以包含多个 DefinedName 对象为不同的表。


How can I set the "Print Titles" property of a spreadsheet with OpenXML, to have a row show at the top of every printed page?

解决方案

The property is directly saved in the SpreadsheetPrintingParts object, however, this doesn't appear to be fully integrated into OpenXML as of yet, and requires passing a base64 string in to the variable. (see here) The content of this string appears to be tied to the machine the file is opened on, which didn't work for my implementation - I wasn't able to create a non-corrupt file through SpreadsheetPrintingParts.FeedData().

Instead, I found this post which stated giving the row the defined name "Print_Titles" has the same effect. I was then able to create a defined name through OpenXML with the following code:

public void SetPrintTitleRows(int startRowIndex, int? endRowIndex = null)
        {
            var localSheetId = _localsheetId++;    //LocalSheetIds are 0-indexed.

            var definedName = new DefinedName
            {
                Name = "_xlnm.Print_Titles",
                LocalSheetId = localSheetId,
                Text = String.Format("\'{0}\'!${1}:${2}", _sheetName, startRowIndex, endRowIndex ?? startRowIndex)
            };

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

            _workbookPart.Workbook.DefinedNames.Append(definedName);
        }

Points to note:

  1. DefinedName.LocalSheetId is zero-indexed, as opposed to Sheet.Id which is 1-indexed
  2. DefinedNames is once per workbook, but can contain multiple DefinedName objects for different sheets.

这篇关于如何设置Excel的&QUOT;打印标题&QUOT;与OpenXML的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆