I want to set the Excel View from Normal to PageLayout. In Java (PIO) there is a getCTWorksheet method, but this method doesn't seem to exist in the C# NPOI implementation.
Please suggest a way to get the CT_WorkSheet from an ISheet object, and set the appropriate view on an Excel Worksheet.
Here is how I currently set the correct view on a CT_Worksheet object I created directly.
CT_Worksheet ct = new CT_Worksheet();
ct = (CT_Worksheet)sheet;
CT_Workbook wrkb = wbb.GetCTWorkbook();
CT_SheetView view = ct.sheetViews.GetSheetViewArray(0);
view.view = ST_SheetViewType.pageLayout;
Unfortunately, the NPOI designers made the GetCTWorksheet()
method internal, so you cannot use it from outside the library. This might have been the intention of the POI developers as well, but Java doesn't have a concept of package-private methods.
As there doesn't seem to be an official way to access the internal classes, we'll just use reflection to access them.
var workbook = new XSSFWorkbook();
var sheet = workbook.CreateSheet("Sheet 1");
var row = sheet.CreateRow(0);
row.CreateCell(0).SetCellValue("This is a test");
// Use reflection go call internal method GetCTWorksheet()
MethodInfo methodInfo = sheet.GetType().GetMethod("GetCTWorksheet", BindingFlags.NonPublic | BindingFlags.Instance);
var ct = (CT_Worksheet) methodInfo.Invoke(sheet, new object[] {});
CT_SheetView view = ct.sheetViews.GetSheetViewArray(0);
view.view = ST_SheetViewType.pageLayout;