Wed Sep 02, 2015 9:30 am
public void addDataGrids(Workbook woorkbook)
{
var ws = woorkbook.Worksheets.Add(this.sheetName);
int lastRow = 0;
int firstDataRow = 0;
if (sheetHeader != null)
lastRow = sheetHeader.createSheetHeader(ws);
int correctX = 1;
int correctY = 1;
int headerX = correctX;
int headerY = correctY;
lastRow += 2;
firstDataRow = lastRow;
int gridNo = 0;
foreach (var grid in dataGrids)
{
#region setting correct x and y values for datagrid
if (grid.cellX == null)
correctX = 1;
else
correctX = grid.cellX.Value;
if (grid.cellY == null)
correctY = lastRow - 1;
else
correctY = grid.cellY.Value;
headerX = correctX;
headerY = correctY;
#endregion
#region initialize DataTable
DataTable localTable = null;
if (grid.iQueryable != null)
{
localTable = grid.iQueryable.CopyToDataTable2();
}
else
if (grid.dataTable != null)
{
localTable = grid.dataTable;
}
#endregion
#region Insert table data
if (localTable != null)
{
foreach (var prop in localTable.Columns)
{
ws.SetCellValue(correctY, correctX, TextGetter.GetText(((DataColumn)prop).ColumnName, "dbattribs"));
correctX++;
}
correctY++;
ws.InsertDataTable(localTable, false, correctY, headerX);
correctY += localTable.Rows.Count - 1;
if (grid.showAsTable)
{
ws.ListObjects.Create("Table", ws.Range[headerY, headerX, correctY, correctX - 1]);
ws.Range[headerY, headerX, headerY, correctX - 1].Style.Color = System.Drawing.Color.FromArgb(0x002550);
ws.Range[headerY, headerX, headerY, correctX - 1].Style.Font.Color = Color.White;
ws.Range[headerY, headerX, headerY, correctX - 1].Style.Font.IsBold = true;
ws.ListObjects[gridNo].BuiltInTableStyle = TableBuiltInStyles.TableStyleLight9;
if (grid.showTotalsRow)
ws.ListObjects[gridNo].DisplayTotalRow = grid.showTotalsRow;
}
else
{
if (grid.includeHeaderFormatting)
{
// ws.AutoFilters.Range = ws.Range[headerY, headerX, headerY, correctX - 2];
// ws.AutoFilters.Range = ws.Range[headerY, headerX, correctY, correctX - 1];
ws.Range[headerY, headerX, headerY, correctX - 1].Style.Color = System.Drawing.Color.FromArgb(0x002550);
//ws.Range[headerY, headerX, headerY, correctX - 1].Style.Color = System.Drawing.Color.FromArgb(0x000033);
ws.Range[headerY, headerX, headerY, correctX - 1].Style.Font.Color = Color.White;
ws.Range[headerY, headerX, headerY, correctX - 1].Style.Font.IsBold = true;
}
else
{
}
}
}
#endregion
#region show data as a table
#endregion
#region format rows
if (grid.colorRows)
{
int endFormatRow = ws.LastRow;
int startFormatRow = headerY + 1;
int color = 0;
for (int ix = startFormatRow; ix < endFormatRow + 1; ix++)
{
if (color % 2 != 0)
{
ws.Range[ix, headerX, ix, correctX - 1].Style.Color = System.Drawing.Color.FromArgb(0xdbdbdb);
}
color++;
}
}
#endregion
lastRow = ws.LastRow + 2;
gridNo++;
}
int finalRow = lastRow;
if (sheetFooter != null)
finalRow = sheetFooter.createSheetFooter(ws);
ws.AllocatedRange.AutoFitColumns();
//ws.AllocatedRange.AutoFitRows();
#region Setup pages ettings
ws.PageSetup.RightFooter = TextGetter.GetText("Antal sider: ", "Excel");
//ws.PageSetup.Footer.Right.AddText(TextGetter.GetText("Antal sider: ", "Excel"), XLHFOccurrence.AllPages);
//ws.PageSetup.Footer.Right.AddText(XLHFPredefinedText.PageNumber, XLHFOccurrence.AllPages);
//ws.PageSetup.Footer.Right.AddText("/", XLHFOccurrence.AllPages);
//ws.PageSetup.Footer.Right.AddText(XLHFPredefinedText.NumberOfPages, XLHFOccurrence.AllPages);
ws.PageSetup.Orientation = PageOrientationType.Landscape;
//ws.PageSetup.PagesWide = 1;
ws.PageSetup.PaperSize = PaperSizeType.PaperA4;
ws.PageSetup.TopMargin = 0.5;
ws.PageSetup.BottomMargin = 0.5;
ws.PageSetup.LeftMargin = 0.2;
ws.PageSetup.RightMargin = 0.2;
ws.PageSetup.FooterMarginInch = 0.15;
ws.PageSetup.HeaderMarginInch = 0.15;
#endregion
}
}