简体   繁体   中英

How can I configure the print area and other printing properties of a sheet using Spreadsheet Light?

Using Excel Interop, I can configure a sheet for printing with code like this:

_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);

I think I can pretty much emulate that with Spreadsheet Light with this code:

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?

I'm not sure about many of these, though, especially the replacement code for "PrintTitleRows" ("PrintHeadings" and "SetCenterHeaderText"), but one thing seems to be totally missing from Spreadsheet Light, namely "PrintArea".

Also, what should the "Zoom" value be? And what corresponds to "FitToPagesWide" and "FitToPagesTall"?

What is the analagous way to accomplish the same thing with Spreadsheet Light? Or does Spreadsheet Light just automatically determine the range to print based on non-empty cells?

I can help with some of these. First up, Print Area.

As Vincent says : Rule 1: Everything begins and ends with SLDocument

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

Next: Fit to Pages:

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); 

Zoom is a view (not print) thing AFAIK and can be changed between 10 and 400 to set the Zoom percentage - equivalent to Ctrl-scrolling when viewing a spreadsheet.

PrintHeadings displays the row (1, 2, 3, ...) and column headings (A, B, C, ...) when you print, so you can see the value in say, D25 easier.

For printing titles, you use the Set<position>HeaderText in SLPageSettings:

settings.SetCenterHeaderText("My Workbook Title");

Similarly SetLeftHeaderText and SetRightHeaderText

I don't know how to set 'RowsToRepeatAtTop' and 'ColumnsToRepeatAtLeft' which would match Interop's PrintTitleRows.

ProTip: The chm help file that comes with SpreadsheetLight is very useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM