如何使用 Spreadsheet Light 配置工作表的打印区域和其他打印属性?

问题描述 投票:0回答:2

使用 Excel Interop,我可以使用如下代码配置用于打印的工作表:

_xlSheetPlatypus.PageSetup.PrintArea = "A1:" + 
    GetExcelTextColumnName(
        _xlSheetPlatypus.UsedRange.Columns.Count) + 
        _xlSheetPlatypus.UsedRange.Rows.Count;
_xlSheetPlatypus.PageSetup.Orientation = Excel.XlPageOrientation.xlLandscape;
_xlSheetPlatypus.PageSetup.Zoom = false;
_xlSheetPlatypus.PageSetup.FitToPagesWide = 1;
_xlSheetPlatypus.PageSetup.FitToPagesTall = 100;

_xlSheetPlatypus.PageSetup.LeftMargin = _xlApp.Application.InchesToPoints(0.5);
_xlSheetPlatypus.PageSetup.RightMargin = _xlApp.Application.InchesToPoints(0.5);
_xlSheetPlatypus.PageSetup.TopMargin = _xlApp.Application.InchesToPoints(0.5);
_xlSheetPlatypus.PageSetup.BottomMargin = _xlApp.Application.InchesToPoints(0.5);
_xlSheetPlatypus.PageSetup.HeaderMargin = _xlApp.Application.InchesToPoints(0.5);
_xlSheetPlatypus.PageSetup.FooterMargin = _xlApp.Application.InchesToPoints(0.5);

_xlSheetPlatypus.PageSetup.PrintTitleRows = String.Format("${0}:${0}", CUSTOMER_HEADING_ROW);

我想我几乎可以使用 Spreadsheet Light 使用以下代码来模拟这一点:

SLPageSettings ps = new SLPageSettings();
// PrintArea
// ???

// PrintTitleRows
ps.PrintHeadings = true;
ps.SetCenterHeaderText(String.Format("${0}:${0}", CUSTOMER_HEADING_ROW); 

// Margins
ps.SetNarrowMargins();
ps.TopMargin = 0.5;
ps.BottomMargin = 0.5;
ps.LeftMargin = 0.5;
ps.RightMargin = 0.5;
ps.HeaderMargin = 0.5;
ps.FooterMargin = 0.5;

// Orientation
ps.Orientation = OrientationValues.Landscape;

// Zoom
//psByCust.ZoomScale = what should this be? Is not a boolean...

// FitToPagesWide
//psByCust.FitToWidth = ; "cannot be assigned to" so how can I set this?

// FitToPagesTall
//psByCust.FitToHeight = 100; "cannot be assigned to" so how can I set this?

不过,我不确定其中的许多内容,尤其是“PrintTitleRows”(“PrintHeadings”和“SetCenterHeaderText”)的替换代码,但 Spreadsheet Light 似乎完全缺少一件事,即“PrintArea”。

另外,“缩放”值应该是多少? “FitToPagesWide”和“FitToPagesTall”对应什么?

使用 Spreadsheet Light 完成同样的事情的类似方法是什么?或者 Spreadsheet Light 只是根据非空单元格自动确定打印范围?

c# excel excel-interop spreadsheetlight
2个回答
3
投票

我可以帮助解决其中一些问题。首先,打印区域。

正如 Vincent 所说:规则 1:一切都以 SLDocument 开始和结束

SLDocument myWorkbook = new SLDocument();
myWorkbook.SetPrintArea("A1", "E10");
// or
myWorkbook.SetPrintArea(1, 1, 10, 5);

下一步:适合页面:

SLPageSettings settings = new SLPageSettings();
settings.ScalePage(2, 3)  // print 2 pages wide and 3 long
// There is no info on how to just scale in one dimension, I would use 
// a definitely too big™ number in the field you don't care about
// Eg for fit all columns on one page:
settings.ScalePage(1, 99999); 

缩放是一种视图(不是打印)功能,据我所知,可以在 10 到 400 之间更改以设置缩放百分比 - 相当于查看电子表格时按 Ctrl 键滚动。

PrintHeadings 在打印时显示行(1、2、3、...)和列标题(A、B、C、...),因此您可以更轻松地看到 D25 中的值。

要打印标题,请使用 SLPageSettings 中的 SetHeaderText:

settings.SetCenterHeaderText("My Workbook Title");

类似地 SetLeftHeaderText 和 SetRightHeaderText

我不知道如何设置 'RowsToRepeatAtTop' 和 'ColumnsToRepeatAtLeft' 来匹配 Interop 的 PrintTitleRows。

专业提示:SpreadsheetLight 附带的 chm 帮助文件非常有用。


0
投票

我们可以使用 SpreadSheetLight 设置 RowsToRepeatAtTop 和 ColumnsToRepeatAtLeft,如下所示:

sl.SetDefinedName("Print_Titles", "Sheet1!$A:$C,Sheet1!$1:$3", "", "Sheet1");

其中 Sheet1 是您棚屋的名称,而 $A:$C 将使其重复 A、B、C 列,而 $1:$3 将重复前三行。

当我们打开Excel时,它会显示为: Page Layout -> 工作表 -> 打印标题 希望这能回答这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.