如何使用电子表格灯配置工作表的打印区域和其他打印属性?

时间:2016-04-11 22:01:55

标签: c# excel excel-interop spreadsheetlight

使用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会根据非空单元格自动确定要打印的范围吗?

1 个答案:

答案 0 :(得分:1)

我可以帮助其中一些。首先是打印区域。

作为Vincent says:规则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); 

缩放是一种视图(非打印)事物AFAIK,可以在10到400之间进行更改以设置缩放百分比 - 相当于在查看电子表格时按住Ctrl键滚动。

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

要打印标题,请在SLPageSettings中使用Set< position> HeaderText:

settings.SetCenterHeaderText("My Workbook Title");

类似于SetLeftHeaderText和SetRightHeaderText

我不知道如何设置'RowsToRepeatAtTop'和'ColumnsToRepeatAtLeft',它们会匹配Interop的PrintTitleRows。

ProTip:SpreadsheetLight附带的chm帮助文件非常有用。