using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace FineUIPro.Web
{
    public class CommonPrint
    {
        /// <summary>
        /// 数据分页
        /// </summary>
        /// <returns></returns>
        public static DataTable GetPageToDataTable(DataTable dt, int PageIndex, int PageSize)
        {
            if (PageIndex == 0)
                return dt;//0页代表每页数据,直接返回

            if (dt == null)
            {
                DataTable table = new DataTable();
                return table;
            }

            DataTable newdt = dt.Copy();
            newdt.Clear();//copy dt的框架

            int rowbegin = (PageIndex - 1) * PageSize;
            int rowend = PageIndex * PageSize;//要展示的数据条数

            if (rowbegin >= dt.Rows.Count)
                return newdt;//源数据记录数小于等于要显示的记录,直接返回dt

            if (rowend > dt.Rows.Count)
                rowend = dt.Rows.Count;
            for (int i = rowbegin; i <= rowend - 1; i++)
            {
                DataRow newdr = newdt.NewRow();
                DataRow dr = dt.Rows[i];
                foreach (DataColumn column in dt.Columns)
                {
                    newdr[column.ColumnName] = dr[column.ColumnName];
                }
                newdt.Rows.Add(newdr);
            }
            return newdt;
        }

        /// <summary>
        /// 创建样式
        /// </summary>
        /// <returns></returns>
        public static ICellStyle SetExcelStyle(XSSFWorkbook wb, BorderStyle Bottom, BorderStyle Left, BorderStyle Right, BorderStyle Top, VerticalAlignment VerAig, HorizontalAlignment HorAig, double FontSize, bool WrapText = true, bool Bold = false, string FontName = "宋体")
        {
            ICellStyle style = wb.CreateCellStyle();
            style.BorderBottom = Bottom;
            style.BorderLeft = Left;
            style.BorderRight = Right;
            style.BorderTop = Top;
            style.VerticalAlignment = VerAig;
            style.Alignment = HorAig;
            IFont font = wb.CreateFont();
            font.FontHeightInPoints = FontSize;
            font.IsBold = Bold;
            font.FontName = FontName;
            style.SetFont(font);
            style.WrapText = WrapText;
            return style;
        }

    }
}