215 lines
10 KiB
C#
215 lines
10 KiB
C#
using CrystalDecisions.Shared;
|
|
using Newtonsoft.Json.Linq;
|
|
using NPOI.SS.Formula.Functions;
|
|
using NPOI.SS.UserModel;
|
|
using NPOI.XSSF.UserModel;
|
|
using System;
|
|
using WebLib;
|
|
|
|
namespace Education
|
|
{
|
|
public class ReportBase
|
|
{
|
|
protected XSSFWorkbook workbook;
|
|
protected ISheet sheet;
|
|
protected WebLib.Excel excel;
|
|
protected IFont headerFont;
|
|
protected IFont footerFont;
|
|
protected IFont titleFont;
|
|
protected IFont titleFontBoldUnderLine;
|
|
protected IFont bodyFont;
|
|
protected ICellStyle headerStyle;
|
|
protected ICellStyle footerStyle;
|
|
protected ICellStyle titleStyleLeft;
|
|
protected ICellStyle titleStyleRight;
|
|
protected ICellStyle titleStyleCenter;
|
|
protected ICellStyle titleStyleCenterBottom;
|
|
protected ICellStyle titleStyleLeftNoBorder;
|
|
protected ICellStyle titleStyleRightNoBorder;
|
|
protected ICellStyle titleStyleCenterNoBorder;
|
|
protected ICellStyle titleStyleCenterBottomNoBorder;
|
|
protected ICellStyle titleStyleCenterUDBorder;
|
|
protected ICellStyle bodyStyleLeft;
|
|
protected ICellStyle bodyStyleRight;
|
|
protected ICellStyle bodyStyleCenter;
|
|
protected ICellStyle bodyNumberStyle;
|
|
protected ICellStyle bodyNumberStyleNoBorder;
|
|
protected ICellStyle bodyNumberStyleUDBorder;
|
|
protected ICellStyle bodyNumberStyle2;
|
|
protected ICellStyle bodyPercentStyle;
|
|
protected int HeaderRowHeight = 24;
|
|
protected int BodyRowHeight = 18;
|
|
|
|
public ReportBase()
|
|
{
|
|
Init();
|
|
}
|
|
|
|
protected virtual void Init()
|
|
{
|
|
workbook = new XSSFWorkbook();
|
|
sheet = workbook.CreateSheet("報表");
|
|
sheet.ForceFormulaRecalculation = true;
|
|
excel = new WebLib.Excel();
|
|
excel.WorkBook = workbook;
|
|
excel.Sheet = sheet;
|
|
|
|
headerFont = excel.NewFont("標楷體", 16, true);
|
|
footerFont = excel.NewFont("標楷體", 16);
|
|
titleFont = excel.NewFont("標楷體", 16, true);
|
|
titleFontBoldUnderLine = excel.NewFont("標楷體", 12, true);
|
|
titleFontBoldUnderLine.Underline = FontUnderlineType.Single;
|
|
bodyFont = excel.NewFont("標楷體", 12);
|
|
|
|
headerStyle = excel.NewCellStyle(headerFont, HorizontalAlignment.Center, VerticalAlignment.Center, true);
|
|
footerStyle = excel.NewCellStyle(footerFont, HorizontalAlignment.Left, VerticalAlignment.Center, true);
|
|
|
|
titleStyleLeft = excel.NewCellStyle(titleFont, HorizontalAlignment.Left, VerticalAlignment.Center, true);
|
|
/*
|
|
titleStyleLeft.BorderTop = BorderStyle.Thin;
|
|
titleStyleLeft.BorderBottom = BorderStyle.Thin;
|
|
titleStyleLeft.BorderLeft = BorderStyle.Thin;
|
|
titleStyleLeft.BorderRight = BorderStyle.Thin;
|
|
*/
|
|
|
|
titleStyleRight = excel.NewCellStyle(titleFont, HorizontalAlignment.Right, VerticalAlignment.Center, true);
|
|
/*
|
|
titleStyleRight.BorderTop = BorderStyle.Thin;
|
|
titleStyleRight.BorderBottom = BorderStyle.Thin;
|
|
titleStyleRight.BorderLeft = BorderStyle.Thin;
|
|
titleStyleRight.BorderRight = BorderStyle.Thin;
|
|
*/
|
|
|
|
titleStyleCenter = excel.NewCellStyle(titleFont, HorizontalAlignment.Center, VerticalAlignment.Center, true);
|
|
/*
|
|
titleStyleCenter.BorderTop = BorderStyle.Thin;
|
|
titleStyleCenter.BorderBottom = BorderStyle.Thin;
|
|
titleStyleCenter.BorderLeft = BorderStyle.Thin;
|
|
titleStyleCenter.BorderRight = BorderStyle.Thin;
|
|
*/
|
|
|
|
titleStyleCenterBottom = excel.NewCellStyle(titleFont, HorizontalAlignment.Center, VerticalAlignment.Bottom, true);
|
|
/*
|
|
titleStyleCenterBottom.BorderTop = BorderStyle.Thin;
|
|
titleStyleCenterBottom.BorderBottom = BorderStyle.Thin;
|
|
titleStyleCenterBottom.BorderLeft = BorderStyle.Thin;
|
|
titleStyleCenterBottom.BorderRight = BorderStyle.Thin;
|
|
*/
|
|
|
|
titleStyleLeftNoBorder = excel.NewCellStyle(titleFont, HorizontalAlignment.Left, VerticalAlignment.Center, true);
|
|
titleStyleRightNoBorder = excel.NewCellStyle(titleFont, HorizontalAlignment.Right, VerticalAlignment.Center, true);
|
|
titleStyleCenterNoBorder = excel.NewCellStyle(titleFont, HorizontalAlignment.Center, VerticalAlignment.Center, true);
|
|
titleStyleCenterBottomNoBorder = excel.NewCellStyle(titleFont, HorizontalAlignment.Center, VerticalAlignment.Bottom, true);
|
|
|
|
titleStyleCenterUDBorder = excel.NewCellStyle(titleFont, HorizontalAlignment.Center, VerticalAlignment.Center, true);
|
|
titleStyleCenterUDBorder.BorderTop = BorderStyle.Thin;
|
|
titleStyleCenterUDBorder.BorderBottom = BorderStyle.Thin;
|
|
|
|
bodyStyleLeft = excel.NewCellStyle(bodyFont, HorizontalAlignment.Left, VerticalAlignment.Center, true);
|
|
bodyStyleLeft.BorderTop = BorderStyle.Thin;
|
|
bodyStyleLeft.BorderBottom = BorderStyle.Thin;
|
|
bodyStyleLeft.BorderLeft = BorderStyle.Thin;
|
|
bodyStyleLeft.BorderRight = BorderStyle.Thin;
|
|
|
|
bodyStyleRight = excel.NewCellStyle(bodyFont, HorizontalAlignment.Right, VerticalAlignment.Center, true);
|
|
bodyStyleRight.BorderTop = BorderStyle.Thin;
|
|
bodyStyleRight.BorderBottom = BorderStyle.Thin;
|
|
bodyStyleRight.BorderLeft = BorderStyle.Thin;
|
|
bodyStyleRight.BorderRight = BorderStyle.Thin;
|
|
|
|
bodyStyleCenter = excel.NewCellStyle(bodyFont, HorizontalAlignment.Center, VerticalAlignment.Center, true);
|
|
bodyStyleCenter.BorderTop = BorderStyle.Thin;
|
|
bodyStyleCenter.BorderBottom = BorderStyle.Thin;
|
|
bodyStyleCenter.BorderLeft = BorderStyle.Thin;
|
|
bodyStyleCenter.BorderRight = BorderStyle.Thin;
|
|
|
|
IDataFormat numberFormat = workbook.CreateDataFormat();
|
|
bodyNumberStyle = excel.NewCellStyle(bodyFont, HorizontalAlignment.Right, VerticalAlignment.Center, true);
|
|
bodyNumberStyle.DataFormat = numberFormat.GetFormat("###,###,##0");
|
|
bodyNumberStyle.BorderTop = BorderStyle.Thin;
|
|
bodyNumberStyle.BorderBottom = BorderStyle.Thin;
|
|
bodyNumberStyle.BorderLeft = BorderStyle.Thin;
|
|
bodyNumberStyle.BorderRight = BorderStyle.Thin;
|
|
|
|
bodyNumberStyle2 = excel.NewCellStyle(bodyFont, HorizontalAlignment.Right, VerticalAlignment.Center, true);
|
|
bodyNumberStyle2.DataFormat = numberFormat.GetFormat("###,###,##0.00");
|
|
bodyNumberStyle2.BorderTop = BorderStyle.Thin;
|
|
bodyNumberStyle2.BorderBottom = BorderStyle.Thin;
|
|
bodyNumberStyle2.BorderLeft = BorderStyle.Thin;
|
|
bodyNumberStyle2.BorderRight = BorderStyle.Thin;
|
|
|
|
bodyPercentStyle = excel.NewCellStyle(bodyFont, HorizontalAlignment.Right, VerticalAlignment.Center, true);
|
|
bodyPercentStyle.DataFormat = numberFormat.GetFormat("##0.00");
|
|
bodyPercentStyle.BorderTop = BorderStyle.Thin;
|
|
bodyPercentStyle.BorderBottom = BorderStyle.Thin;
|
|
bodyPercentStyle.BorderLeft = BorderStyle.Thin;
|
|
bodyPercentStyle.BorderRight = BorderStyle.Thin;
|
|
|
|
bodyNumberStyleNoBorder = excel.NewCellStyle(bodyFont, HorizontalAlignment.Right, VerticalAlignment.Center, true);
|
|
bodyNumberStyleNoBorder.DataFormat = numberFormat.GetFormat("###,###,##0");
|
|
|
|
bodyNumberStyleUDBorder = excel.NewCellStyle(bodyFont, HorizontalAlignment.Right, VerticalAlignment.Center, true);
|
|
bodyNumberStyleUDBorder.DataFormat = numberFormat.GetFormat("###,###,##0");
|
|
bodyNumberStyleUDBorder.BorderTop = BorderStyle.Thin;
|
|
bodyNumberStyleUDBorder.BorderBottom = BorderStyle.Thin;
|
|
}
|
|
|
|
protected void SetPaperAndMargin(ISheet Asheet)
|
|
{
|
|
// 設定紙張
|
|
Asheet.PrintSetup.PaperSize = 9; // A4
|
|
Asheet.PrintSetup.Landscape = false; // 直向
|
|
|
|
// 列印格式
|
|
Asheet.PrintSetup.UsePage = true;
|
|
Asheet.PrintSetup.FitWidth = 1; // 將所有欄擠進 1 頁寬
|
|
Asheet.PrintSetup.FitHeight = 0; // 0 表示不指定幾頁
|
|
Asheet.HorizontallyCenter = true; // 水平置中
|
|
|
|
// 頁首頁尾
|
|
Asheet.Footer.Left = "列印日期:&D";
|
|
Asheet.Footer.Right = "頁數:&P";
|
|
|
|
// 紙張邊界
|
|
Asheet.PrintSetup.HeaderMargin = 0d;
|
|
Asheet.PrintSetup.FooterMargin = 0.3d;
|
|
Asheet.SetMargin(MarginType.TopMargin, 0.5d);
|
|
Asheet.SetMargin(MarginType.BottomMargin, 0.5d);
|
|
Asheet.SetMargin(MarginType.LeftMargin, 0.3d);
|
|
Asheet.SetMargin(MarginType.RightMargin, 0.3d);
|
|
}
|
|
|
|
protected void SetDataValue(int row, int col, ExportColumn column, object value, ICellStyle cellStyle)
|
|
{
|
|
if (column.MoneyFormat)
|
|
{
|
|
if (column.MondyDigits == 0)
|
|
excel.SetCellValue(row, col, DAC.GetDouble(value), bodyNumberStyle);
|
|
else
|
|
excel.SetCellValue(row, col, DAC.GetDouble(value), bodyNumberStyle2);
|
|
}
|
|
else if (!string.IsNullOrEmpty(column.Format))
|
|
{
|
|
excel.SetCellValue(row, col, String.Format(column.Format, value), cellStyle);
|
|
}
|
|
else
|
|
{
|
|
excel.SetCellValue(row, col, DAC.GetString(value), cellStyle);
|
|
}
|
|
}
|
|
}
|
|
|
|
public class ExportColumn
|
|
{
|
|
public string Key { get; set; }
|
|
public string Title { get; set; }
|
|
public int Width { get; set; }
|
|
public string FieldName { get; set; }
|
|
public string Format { get; set; } = string.Empty;
|
|
public bool MoneyFormat { get; set; } = false;
|
|
public int MondyDigits { get; set; } = 0;
|
|
public HorizontalAlignment Align { get; set; } = HorizontalAlignment.Left;
|
|
public string Comment { get; set; }
|
|
}
|
|
}
|