示例#1
0
            /// <summary>
            /// Init
            /// </summary>
            /// <param name="Response"></param>
            public void Init(HttpResponseBase Response)
            {
                this.Response = Response;
                if (Book == null)
                {
                    Book = new NPOI.HSSF.UserModel.HSSFWorkbook();

                    this.DataFormat                   = Book.CreateDataFormat();
                    this.DateCellStyle                = Book.CreateCellStyle();
                    this.DateTimeCellStyle            = Book.CreateCellStyle();
                    this.TimeCellStyle                = Book.CreateCellStyle();
                    this.DateCellStyle.DataFormat     = DataFormat.GetFormat("yyyy/MM/dd");
                    this.DateTimeCellStyle.DataFormat = DataFormat.GetFormat("yyyy/MM/dd HH:mm:ss");
                    this.TimeCellStyle.DataFormat     = DataFormat.GetFormat("HH:mm:ss");

                    DefaultSheet = Book.CreateSheet("Sheet1");
                }
            }
示例#2
0
        ///// <summary>
        ///// 樣式的值是否改變
        ///// </summary>
        //internal bool StyleValueIsChange
        //{
        //    get { return _styleValueIsChange; }
        //    set{_styleValueIsChange =value;}
        //}

        #endregion

        #region 方法

        internal ICellStyle GetCellStyle(NPOI.HSSF.UserModel.HSSFWorkbook workBook)
        {
            if (workBook == null)
            {
                throw new ArgumentNullException("workBook參數不能為NULL。");
            }

            if (_cellStyle == null || _styleValueIsChange)
            {
                _cellStyle = workBook.CreateCellStyle();
                IFont font = workBook.FindFont((short)_fontBold, _fontColor, (short)(_fontSize * 20), _fontName, _isItalic, _isStrikeout, _fontTypeOffset, (byte)_underline);
                if (font == null)
                {
                    font                    = workBook.CreateFont();
                    font.FontName           = this._fontName;
                    font.Boldweight         = (short)this._fontBold;
                    font.FontHeightInPoints = (short)this._fontSize;
                    font.Underline          = (byte)this.UnderLine;
                    font.IsItalic           = _isItalic;
                    font.Color              = _fontColor;
                    font.IsStrikeout        = _isStrikeout;
                    font.TypeOffset         = _fontTypeOffset;
                }
                _cellStyle.SetFont(font);
                IDataFormat format = workBook.CreateDataFormat();
                _cellStyle.BorderBottom      = this._borderBottom;
                _cellStyle.BorderLeft        = this._borderLeft;
                _cellStyle.BorderRight       = this._borderRight;
                _cellStyle.BorderTop         = this._borderTop;
                _cellStyle.Alignment         = this._alignment;
                _cellStyle.VerticalAlignment = this.VerticalAlignment;
                if (!string.IsNullOrEmpty(_dataFormart))
                {
                    _cellStyle.DataFormat = format.GetFormat(this._dataFormart);
                }
                _cellStyle.WrapText = this._warpText;
                _styleValueIsChange = false;
            }
            return(_cellStyle);
        }
示例#3
0
    private void GenerateExcelReport(DataTable tmpDataTable, string exportFileName)
    {
        int columnCount  = 0;
        int lastRowIndex = 0;

        // Set column style
        NPOI.HSSF.UserModel.HSSFWorkbook   workbook      = new NPOI.HSSF.UserModel.HSSFWorkbook();
        NPOI.HSSF.UserModel.HSSFDataFormat format        = (NPOI.HSSF.UserModel.HSSFDataFormat)workbook.CreateDataFormat();
        NPOI.HSSF.UserModel.HSSFCellStyle  dateCellStyle = (NPOI.HSSF.UserModel.HSSFCellStyle)workbook.CreateCellStyle();
        dateCellStyle.DataFormat = format.GetFormat("yyyy-MM-dd");
        dateCellStyle.Alignment  = NPOI.SS.UserModel.HorizontalAlignment.LEFT;
        NPOI.HSSF.UserModel.HSSFSheet     worksheet    = (NPOI.HSSF.UserModel.HSSFSheet)workbook.CreateSheet("AverageCostCentreExport");
        NPOI.HSSF.UserModel.HSSFCellStyle numericStyle = (NPOI.HSSF.UserModel.HSSFCellStyle)workbook.CreateCellStyle();
        numericStyle.DataFormat = NPOI.HSSF.UserModel.HSSFDataFormat.GetBuiltinFormat("#,##0.00");
        numericStyle.Alignment  = NPOI.SS.UserModel.HorizontalAlignment.RIGHT;
        NPOI.HSSF.UserModel.HSSFCellStyle style = (NPOI.HSSF.UserModel.HSSFCellStyle)workbook.CreateCellStyle();
        style.Alignment = NPOI.SS.UserModel.HorizontalAlignment.RIGHT;

        // Set column width
        worksheet.SetColumnWidth(0, 40 * 256);
        worksheet.SetColumnWidth(2, 20 * 256);
        worksheet.SetColumnWidth(5, 15 * 256);
        worksheet.SetColumnWidth(7, 15 * 256);
        worksheet.SetColumnWidth(8, 15 * 256);
        worksheet.SetColumnWidth(9, 15 * 256);
        worksheet.SetColumnWidth(10, 12 * 256);

        // Set column title
        NPOI.HSSF.UserModel.HSSFRow headerRow = (NPOI.HSSF.UserModel.HSSFRow)worksheet.CreateRow(lastRowIndex);
        headerRow.CreateCell(0).SetCellValue("Average Cost Centre Export");
        headerRow = (NPOI.HSSF.UserModel.HSSFRow)worksheet.CreateRow(lastRowIndex + 1);
        headerRow.CreateCell(0).SetCellValue(peroid);
        headerRow = (NPOI.HSSF.UserModel.HSSFRow)worksheet.CreateRow(lastRowIndex + 3);

        foreach (DataColumn headercolumn in tmpDataTable.Columns)
        {
            NPOI.HSSF.UserModel.HSSFCell cell = (NPOI.HSSF.UserModel.HSSFCell)headerRow.CreateCell(columnCount);
            cell.SetCellValue(headercolumn.ColumnName);
            if (columnCount == 11)
            {
                cell.CellStyle = style;
            }
            columnCount++;
        }

        // Set value for every row
        foreach (DataRow row in tmpDataTable.Rows)
        {
            NPOI.HSSF.UserModel.HSSFRow detailRow = (NPOI.HSSF.UserModel.HSSFRow)worksheet.CreateRow(lastRowIndex + 4);

            detailRow.CreateCell(0).SetCellValue(row[FIELD_COMPANY].ToString());
            detailRow.CreateCell(1).SetCellValue(row[FIELD_DIVISION].ToString());
            detailRow.CreateCell(2).SetCellValue(row[FIELD_DEPARTMENT].ToString());
            detailRow.CreateCell(3).SetCellValue(row[FIELD_SECTION].ToString());
            detailRow.CreateCell(4).SetCellValue(row[FIELD_EMP_NO].ToString());
            detailRow.CreateCell(5).SetCellValue(row[FIELD_EMP_NAME].ToString());
            detailRow.CreateCell(6).SetCellValue(row[FIELD_ALIAS].ToString());
            detailRow.CreateCell(7).SetCellValue(row[FIELD_POSITION].ToString());

            NPOI.HSSF.UserModel.HSSFCell cell = (NPOI.HSSF.UserModel.HSSFCell)detailRow.CreateCell(8);
            cell.SetCellValue((DateTime)row[FIELD_FROM]);
            cell.CellStyle = dateCellStyle;

            cell = (NPOI.HSSF.UserModel.HSSFCell)detailRow.CreateCell(9);
            cell.SetCellValue((DateTime)row[FIELD_TO]);
            cell.CellStyle = dateCellStyle;

            detailRow.CreateCell(10).SetCellValue(row[FIELD_COST_CENTER].ToString());
            cell = (NPOI.HSSF.UserModel.HSSFCell)detailRow.CreateCell(11);
            cell.SetCellValue((double)row[FIELD_PERCENTAGE]);
            cell.CellStyle = numericStyle;

            lastRowIndex++;
        }

        System.IO.FileStream file = new System.IO.FileStream(exportFileName, System.IO.FileMode.Create);
        workbook.Write(file);
        file.Close();
    }
示例#4
0
        public static DataSet parse(string ExcelFilePath, string ZipPassword, string FirstColumnName)
        {
            //OleDbConnection cnnxls = null;
            DataSet ds = new DataSet();

            if (System.IO.Path.GetExtension(ExcelFilePath).Equals(".zip", StringComparison.CurrentCultureIgnoreCase))
            {
                string strTmpFolder = ExcelFilePath + ".dir";

                try
                {
                    zip.ExtractAll(ExcelFilePath, strTmpFolder, ZipPassword);
                    System.IO.DirectoryInfo rootDir = new System.IO.DirectoryInfo(strTmpFolder);
                    foreach (System.IO.FileInfo fileInfo in rootDir.GetFiles("*", System.IO.SearchOption.AllDirectories))
                    {
                        ds.Merge(parse(fileInfo.FullName, ZipPassword, FirstColumnName));
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    System.IO.Directory.Delete(strTmpFolder, true);
                }
                //string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strTmpFolder + ";Extended Properties=\"text;HDR=YES;IMEX=1;FMT=Delimited;\"";
                //cnnxls = new OleDbConnection(strConn);
                //cnnxls.Open();
            }
            else if (System.IO.Path.GetExtension(ExcelFilePath).Equals(".csv", StringComparison.CurrentCultureIgnoreCase))
            {
                System.IO.FileInfo fileInfo = new System.IO.FileInfo(ExcelFilePath);
                DataTable          table    = CSVReader.parse(fileInfo.OpenRead(), true, ",", "\"");
                table.TableName = System.IO.Path.GetFileNameWithoutExtension(fileInfo.FullName);
                ds.Tables.Add(table);
            }
            else
            {
                NPOI.HSSF.UserModel.HSSFWorkbook workBook = new NPOI.HSSF.UserModel.HSSFWorkbook(new System.IO.FileStream(ExcelFilePath, System.IO.FileMode.Open)); // ExcelLibrary.SpreadSheet.Workbook.Load(Filename);

                for (int sheetIndex = 0; sheetIndex < workBook.NumberOfSheets; sheetIndex++)
                {
                    if (!workBook.IsSheetHidden(sheetIndex))
                    {
                        int intHeaderRow = 0;
                        NPOI.HSSF.UserModel.HSSFSheet workSheet = (NPOI.HSSF.UserModel.HSSFSheet)workBook.GetSheetAt(sheetIndex);
                        NPOI.HSSF.UserModel.HSSFRow   headerRow = null; //= (NPOI.HSSF.UserModel.HSSFRow)workSheet.GetRow(intHeaderRow);

                        if (!string.IsNullOrEmpty(FirstColumnName))
                        {
                            for (int tmpRowIdx = intHeaderRow; tmpRowIdx <= workSheet.LastRowNum; tmpRowIdx++)
                            {
                                headerRow = (NPOI.HSSF.UserModel.HSSFRow)workSheet.GetRow(tmpRowIdx);
                                if (headerRow == null)
                                {
                                    continue;
                                }
                                bool columnNameMatch = false;
                                for (int tmpColumnIndex = 0; tmpColumnIndex <= headerRow.LastCellNum; tmpColumnIndex++)
                                {
                                    if (headerRow.GetCell(tmpColumnIndex) != null)
                                    {
                                        string columnName = headerRow.GetCell(tmpColumnIndex).ToString().Trim();
                                        if (FirstColumnName.Equals(columnName))
                                        {
                                            intHeaderRow    = tmpRowIdx;
                                            columnNameMatch = true;
                                            break;
                                        }
                                    }
                                }
                                if (columnNameMatch)
                                {
                                    break;
                                }
                            }
                        }
                        else
                        {
                            headerRow = (NPOI.HSSF.UserModel.HSSFRow)workSheet.GetRow(intHeaderRow);
                        }

                        if (headerRow == null)
                        {
                            continue;
                        }
                        string    tableName      = workSheet.SheetName.Trim();
                        DataTable table          = new DataTable(tableName);
                        int       intColumnIndex = 0;
                        while (intColumnIndex <= headerRow.LastCellNum)
                        {
                            if (headerRow.GetCell(intColumnIndex) != null)
                            {
                                string columnName = headerRow.GetCell(intColumnIndex).ToString().Trim();
                                if (string.IsNullOrEmpty(columnName))
                                {
                                    columnName = "Column_" + intColumnIndex;
                                }
                                if (table.Columns.Contains(columnName))
                                {
                                    columnName = "Column_" + intColumnIndex;
                                }
                                table.Columns.Add(columnName, typeof(string));

                                //  resign new value of column name to Excel for below part of import
                                headerRow.GetCell(intColumnIndex).SetCellValue(columnName);
                            }
                            intColumnIndex++;
                        }
                        int rowCount = 1;

                        while (intHeaderRow + rowCount <= workSheet.LastRowNum)
                        {
                            int colCount = 0;

                            NPOI.HSSF.UserModel.HSSFRow row = (NPOI.HSSF.UserModel.HSSFRow)workSheet.GetRow(intHeaderRow + rowCount);
                            if (row == null)
                            {
                                rowCount++;
                                continue;
                            }

                            DataRow dataRow = table.NewRow();

                            while (colCount <= headerRow.LastCellNum)
                            {
                                if (headerRow.GetCell(colCount) != null)
                                {
                                    string columnName = headerRow.GetCell(colCount).ToString();
                                    if (table.Columns.Contains(columnName))
                                    {
                                        NPOI.HSSF.UserModel.HSSFCell cell = (NPOI.HSSF.UserModel.HSSFCell)row.GetCell(colCount);
                                        if (cell != null)
                                        {
                                            if (cell.CellType.Equals(NPOI.SS.UserModel.CellType.FORMULA))
                                            {
                                                NPOI.HSSF.UserModel.HSSFFormulaEvaluator e = new NPOI.HSSF.UserModel.HSSFFormulaEvaluator(workBook);
                                                cell = (NPOI.HSSF.UserModel.HSSFCell)e.EvaluateInCell(cell);
                                            }
                                            string fieldValue = cell.ToString();
                                            if (cell.CellType.Equals(NPOI.SS.UserModel.CellType.NUMERIC))
                                            {
                                                string format = string.Empty;
                                                //bool IsBuildinformat = false;
                                                //  Not sure whether workBook.CreateDataFormat().GetFormat(index) can obtain all the build-in format
                                                try
                                                {
                                                    format = NPOI.HSSF.UserModel.HSSFDataFormat.GetBuiltinFormat(cell.CellStyle.DataFormat);
                                                    //IsBuildinformat = true;
                                                }
                                                catch
                                                {
                                                    format = workBook.CreateDataFormat().GetFormat(cell.CellStyle.DataFormat);
                                                }

                                                //  [h]:mm:ss handle NOT support
                                                int midBlanketStartPos = format.IndexOf('[');
                                                while (midBlanketStartPos >= 0)
                                                {
                                                    int midBlanketEndPos = format.IndexOf(']', midBlanketStartPos);
                                                    format             = format.Substring(0, midBlanketStartPos) + format.Substring(midBlanketStartPos + 1, midBlanketEndPos - midBlanketStartPos - 1) + format.Substring(midBlanketEndPos + 1);
                                                    midBlanketStartPos = format.IndexOf('[');
                                                }

                                                if (format.IndexOf("y", StringComparison.CurrentCultureIgnoreCase) >= 0 || format.IndexOf("d", StringComparison.CurrentCultureIgnoreCase) >= 0)
                                                {
                                                    if (format.IndexOf("h", StringComparison.CurrentCultureIgnoreCase) >= 0)
                                                    {
                                                        fieldValue = cell.DateCellValue.ToString("yyyy-MM-dd HH:mm:ss");
                                                    }
                                                    else
                                                    {
                                                        DateTime date = cell.DateCellValue;
                                                        if (date.TimeOfDay.TotalSeconds > 0)
                                                        {
                                                            fieldValue = date.ToString("yyyy-MM-dd HH:mm:ss");
                                                        }
                                                        else
                                                        {
                                                            fieldValue = date.ToString("yyyy-MM-dd");
                                                        }
                                                    }
                                                }
                                                else if (format.IndexOf("h", StringComparison.CurrentCultureIgnoreCase) >= 0)
                                                {
                                                    DateTime date = cell.DateCellValue;

                                                    //  default date of "Time Only" field is 1899-12-31
                                                    if (!date.Date.Ticks.Equals(new DateTime(1899, 12, 31).Ticks))
                                                    {
                                                        fieldValue = cell.DateCellValue.ToString("yyyy-MM-dd HH:mm:ss");
                                                    }
                                                    else
                                                    {
                                                        fieldValue = cell.DateCellValue.ToString("HH:mm:ss");
                                                    }
                                                }
                                                else
                                                {
                                                    fieldValue = cell.NumericCellValue.ToString();
                                                }
                                            }
                                            dataRow[columnName] = fieldValue;
                                        }
                                    }
                                }


                                colCount++;
                            }
                            table.Rows.Add(dataRow);
                            rowCount++;
                        }
                        ds.Tables.Add(table);
                    }
                }

                //string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + ExcelFilePath + ";Extended Properties=\"Excel 12.0 Xml;IMEX=1;HDR=YES;\"";
                //cnnxls = new OleDbConnection(strConn);
                //try
                //{
                //    cnnxls.Open();
                //}
                //catch
                //{
                //    cnnxls.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + ExcelFilePath + ";Extended Properties=\"Excel 8.0;IMEX=1;HDR=YES;\"";
                //    cnnxls.Open();
                //}


                //DataTable schemaTable = cnnxls.GetSchema("Tables");


                //foreach (DataRow schemaRow in schemaTable.Rows)
                //{
                //    string tableName = schemaRow["Table_Name"].ToString().Trim();
                //    if (tableName.EndsWith("$"))
                //    {
                //        OleDbDataAdapter oda = new OleDbDataAdapter("select * from [" + tableName + "]", cnnxls);
                //        try
                //        {


                //            //DataTable[] tables = oda.FillSchema(ds, SchemaType.Mapped);//
                //            //tables[0].TableName = schemaRow["Table_Name"].ToString().Replace("$", "").Replace("#csv", "");
                //            //if (tables[0].Columns.Contains("Emp No*"))
                //            //    tables[0].Columns["Emp No*"].DataType = typeof(string);
                //            //OleDbDataReader dr = oda.SelectCommand.ExecuteReader();

                //            //while (dr.Read())
                //            //{
                //            //    DataRow row = tables[0].NewRow();
                //            //    for (int i = 0; i < tables[0].Columns.Count; i++)
                //            //        row[i] = dr[i];
                //            //    tables[0].Rows.Add(row);
                //            //}
                //            ////                    oda.Fill(tables[0]);
                //            //if (ds.Tables.Contains(tableName) && tableName.ToString().EndsWith("$"))
                //            //    ds.Tables.Remove(tableName);
                //            string actualTableName = tableName.Substring(0, tableName.Length - 1);
                //            if (!ds.Tables.Contains(actualTableName))
                //                oda.Fill(ds, actualTableName);
                //        }
                //        catch
                //        {
                //            //  unknown error caused by hidden sheet
                //        }
                //        //                oda.Fill(ds);
                //    }
                //}

                //cnnxls.Close();
            }
            foreach (DataTable tempTable in ds.Tables)
            {
                for (int rowIdx = tempTable.Rows.Count - 1; rowIdx >= 0; rowIdx--)
                {
                    DataRow row        = tempTable.Rows[rowIdx];
                    bool    isEmptyRow = true;
                    foreach (DataColumn tempColumn in tempTable.Columns)
                    {
                        if (!row.IsNull(tempColumn))
                        {
                            if (!string.IsNullOrEmpty(row[tempColumn].ToString().Trim()))
                            {
                                isEmptyRow = false;
                                break;
                            }
                        }
                    }
                    if (isEmptyRow)
                    {
                        tempTable.Rows.Remove(row);
                    }
                    else
                    {
                        break;
                    }
                }
            }
            foreach (DataTable tempTable in ds.Tables)
            {
                foreach (DataColumn tempColumn in tempTable.Columns)
                {
                    string tempColumnName = tempColumn.ColumnName;
                    tempColumnName        = tempColumnName.Trim().Replace("*", "");
                    tempColumnName        = tempColumnName.Trim().Replace("#", "");
                    tempColumn.ColumnName = tempColumnName;
                }
            }
            return(ds);
        }
示例#5
0
        //private ExcelLibrary.SpreadSheet.Worksheet CreateWorkSheet(DataTable dataTable)
        private NPOI.HSSF.UserModel.HSSFSheet CreateWorkSheet(DataTable dataTable)
        {
            if (workbook != null)
            {
                NPOI.HSSF.UserModel.HSSFDataFormat format = (NPOI.HSSF.UserModel.HSSFDataFormat)workbook.CreateDataFormat();

                NPOI.HSSF.UserModel.HSSFCellStyle dateCellStyle = (NPOI.HSSF.UserModel.HSSFCellStyle)workbook.CreateCellStyle();
                dateCellStyle.DataFormat = format.GetFormat("yyyy-MM-dd");

                NPOI.HSSF.UserModel.HSSFCellStyle numericCellStyle = (NPOI.HSSF.UserModel.HSSFCellStyle)workbook.CreateCellStyle();
                numericCellStyle.DataFormat = format.GetFormat("0.00##");

                NPOI.HSSF.UserModel.HSSFSheet worksheet = (NPOI.HSSF.UserModel.HSSFSheet)workbook.CreateSheet(dataTable.TableName.Replace("$", ""));
                //ExcelLibrary.SpreadSheet.Row headerRow = new ExcelLibrary.SpreadSheet.Row();
                NPOI.HSSF.UserModel.HSSFRow headerRow = (NPOI.HSSF.UserModel.HSSFRow)worksheet.CreateRow(0);
                int columnCount = 0;
                foreach (DataColumn headercolumn in dataTable.Columns)
                {
                    headercolumn.ColumnName = headercolumn.ColumnName.Trim();

                    NPOI.HSSF.UserModel.HSSFCell cell = (NPOI.HSSF.UserModel.HSSFCell)headerRow.CreateCell(columnCount); //new ExcelLibrary.SpreadSheet.Cell(headercolumn.ColumnName, new ExcelLibrary.SpreadSheet.CellFormat(ExcelLibrary.SpreadSheet.CellFormatType.Text, string.Empty));
                    cell.SetCellValue(headercolumn.ColumnName);

                    //headerRow.SetCell(columnCount,cell);
                    //worksheet.Cells[0, columnCount] = cell;//new ExcelLibrary.SpreadSheet.Cell(column.ColumnName, new ExcelLibrary.SpreadSheet.CellFormat(ExcelLibrary.SpreadSheet.CellFormatType.Text, string.Empty));
                    columnCount++;
                }
                //worksheet.Cells.Rows.Add(0, headerRow);

                int rowCount = 0;



                //NPOI.HSSF.UserModel.HSSFCellStyle numericCellStyle = (NPOI.HSSF.UserModel.HSSFCellStyle)workbook.CreateCellStyle();
                //numericCellStyle.DataFormat = NPOI.HSSF.UserModel.HSSFDataFormat.GetBuiltinFormat("0.00"); ;

                //NPOI.HSSF.UserModel.HSSFCellStyle integerCellStyle = (NPOI.HSSF.UserModel.HSSFCellStyle)workbook.CreateCellStyle();
                //integerCellStyle.DataFormat = NPOI.HSSF.UserModel.HSSFDataFormat.GetBuiltinFormat("0"); ;

                foreach (DataRow row in dataTable.Rows)
                {
                    rowCount++;
                    columnCount = 0;

                    //                    ExcelLibrary.SpreadSheet.Row detailRow = new ExcelLibrary.SpreadSheet.Row();

                    NPOI.HSSF.UserModel.HSSFRow detailRow = (NPOI.HSSF.UserModel.HSSFRow)worksheet.CreateRow(rowCount);

                    foreach (DataColumn column in dataTable.Columns)
                    {
                        //ExcelLibrary.SpreadSheet.Cell cell =new ExcelLibrary.SpreadSheet.Cell(string.Empty, new ExcelLibrary.SpreadSheet.CellFormat(ExcelLibrary.SpreadSheet.CellFormatType.Text, string.Empty));
                        NPOI.HSSF.UserModel.HSSFCell cell = (NPOI.HSSF.UserModel.HSSFCell)detailRow.CreateCell(columnCount);

                        if (column.DataType.Equals(typeof(string)))
                        {
                            //cell.Format = new ExcelLibrary.SpreadSheet.CellFormat(ExcelLibrary.SpreadSheet.CellFormatType.Text, string.Empty);
                            //cell.Value = (row[column] == System.DBNull.Value ? string.Empty : row[column].ToString());
                            cell.SetCellValue(row[column] == System.DBNull.Value ? string.Empty : row[column].ToString());
                        }
                        else if (column.DataType.Equals(typeof(double)) || column.DataType.Equals(typeof(float)))
                        {
                            //cell.Format = new ExcelLibrary.SpreadSheet.CellFormat(ExcelLibrary.SpreadSheet.CellFormatType.Number, "0.00");
                            //cell.Value = (row[column] == System.DBNull.Value ? string.Empty : row[column]);
                            if (row[column] != System.DBNull.Value)
                            {
                                double value = Convert.ToDouble(row[column].ToString());
                                if (value.Equals(double.NaN))
                                {
                                    cell.SetCellValue(string.Empty);
                                }
                                else
                                {
                                    cell.SetCellValue(value);
                                }
                            }
                            cell.CellStyle = numericCellStyle;
                        }
                        else if (column.DataType.Equals(typeof(int)))
                        {
                            //cell.Format = new ExcelLibrary.SpreadSheet.CellFormat(ExcelLibrary.SpreadSheet.CellFormatType.Number, "0.00");
                            //cell.Value = (row[column] == System.DBNull.Value ? string.Empty : row[column]);
                            if (row[column] != System.DBNull.Value)
                            {
                                cell.SetCellValue(Convert.ToDouble(row[column].ToString()));
                            }
                            //cell.CellStyle = integerCellStyle;
                        }
                        else if (column.DataType.Equals(typeof(DateTime)))
                        {
                            //cell.Format = new ExcelLibrary.SpreadSheet.CellFormat(ExcelLibrary.SpreadSheet.CellFormatType.DateTime, "yyyy-MM-dd");
                            //if (row[column] == System.DBNull.Value)
                            //    cell.Value = string.Empty;
                            //else
                            //    cell.Value = (DateTime)row[column];
                            if (row[column] != System.DBNull.Value)
                            {
                                cell.SetCellValue((DateTime)row[column]);
                            }

                            cell.CellStyle = dateCellStyle;
                        }
                        else
                        {
                            //cell.Format = new ExcelLibrary.SpreadSheet.CellFormat(ExcelLibrary.SpreadSheet.CellFormatType.Text, string.Empty);
                            //cell.Value = (row[column] == System.DBNull.Value ? string.Empty : row[column].ToString());
                            if (row[column] != System.DBNull.Value)
                            {
                                cell.SetCellValue(row[column].ToString());
                            }
                        }
                        //worksheet.Cells[rowCount, columnCount] = cell;
                        columnCount++;
                    }
                    //                    worksheet.Cells.Rows.Add(rowCount, detailRow);
                }
                //workbook.Worksheets.Add(worksheet);

                return(worksheet);
            }
            else
            {
                return(null);
            }
        }
        public ExcelCellStyle(NPOI.HSSF.UserModel.HSSFWorkbook book)
        {
            /*新建名为style的(普通)CellStyle,水平垂直居中*/
            style                   = book.CreateCellStyle();
            style.Alignment         = NPOI.SS.UserModel.HorizontalAlignment.Center;
            style.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
            style.BorderBottom      = BorderStyle.Thin;
            style.BorderTop         = BorderStyle.Thin;
            style.BorderLeft        = BorderStyle.Thin;
            style.BorderRight       = BorderStyle.Thin;

            /*新建名为styleLeft的(普通)CellStyle,水平垂直居左*/
            styleLeft                   = book.CreateCellStyle();
            styleLeft.Alignment         = NPOI.SS.UserModel.HorizontalAlignment.Left;
            styleLeft.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
            styleLeft.BorderBottom      = BorderStyle.Thin;
            styleLeft.BorderTop         = BorderStyle.Thin;
            styleLeft.BorderLeft        = BorderStyle.Thin;
            styleLeft.BorderRight       = BorderStyle.Thin;

            /*新建名为style4LemonChiffon的(普通)CellStyle,水平垂直居中*/
            style4LemonChiffon                     = book.CreateCellStyle();
            style4LemonChiffon.Alignment           = NPOI.SS.UserModel.HorizontalAlignment.Center;
            style4LemonChiffon.VerticalAlignment   = NPOI.SS.UserModel.VerticalAlignment.Center;
            style4LemonChiffon.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LemonChiffon.Index;
            style4LemonChiffon.FillPattern         = FillPattern.SolidForeground;
            style4LemonChiffon.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LemonChiffon.Index;
            style4LemonChiffon.BorderBottom        = BorderStyle.Thin;
            style4LemonChiffon.BorderTop           = BorderStyle.Thin;
            style4LemonChiffon.BorderLeft          = BorderStyle.Thin;
            style4LemonChiffon.BorderRight         = BorderStyle.Thin;

            /*新建名为style4CornflowerBlue的(普通)CellStyle,水平垂直居中*/
            style4CornflowerBlue                     = book.CreateCellStyle();
            style4CornflowerBlue.Alignment           = NPOI.SS.UserModel.HorizontalAlignment.Center;
            style4CornflowerBlue.VerticalAlignment   = NPOI.SS.UserModel.VerticalAlignment.Center;
            style4CornflowerBlue.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.CornflowerBlue.Index;
            style4CornflowerBlue.FillPattern         = FillPattern.SolidForeground;
            style4CornflowerBlue.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.CornflowerBlue.Index;
            style4CornflowerBlue.BorderBottom        = BorderStyle.Thin;
            style4CornflowerBlue.BorderTop           = BorderStyle.Thin;
            style4CornflowerBlue.BorderLeft          = BorderStyle.Thin;
            style4CornflowerBlue.BorderRight         = BorderStyle.Thin;

            /*新建名为style4LightGreen的(普通)CellStyle,水平垂直居中*/
            style4LightGreen                     = book.CreateCellStyle();
            style4LightGreen.Alignment           = NPOI.SS.UserModel.HorizontalAlignment.Center;
            style4LightGreen.VerticalAlignment   = NPOI.SS.UserModel.VerticalAlignment.Center;
            style4LightGreen.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LightGreen.Index;
            style4LightGreen.FillPattern         = FillPattern.SolidForeground;
            style4LightGreen.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LightGreen.Index;
            style4LightGreen.BorderBottom        = BorderStyle.Thin;
            style4LightGreen.BorderTop           = BorderStyle.Thin;
            style4LightGreen.BorderLeft          = BorderStyle.Thin;
            style4LightGreen.BorderRight         = BorderStyle.Thin;

            /*新建名为style4Lime的(普通)CellStyle,水平垂直居中*/
            style4Lime                     = book.CreateCellStyle();
            style4Lime.Alignment           = NPOI.SS.UserModel.HorizontalAlignment.Center;
            style4Lime.VerticalAlignment   = NPOI.SS.UserModel.VerticalAlignment.Center;
            style4Lime.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Lime.Index;
            style4Lime.FillPattern         = FillPattern.SolidForeground;
            style4Lime.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Lime.Index;
            style4Lime.BorderBottom        = BorderStyle.Thin;
            style4Lime.BorderTop           = BorderStyle.Thin;
            style4Lime.BorderLeft          = BorderStyle.Thin;
            style4Lime.BorderRight         = BorderStyle.Thin;

            /*新建名为cellStyleDate的(日期用)CellStyle,水平居中,日期格式化为yyyy-MM-dd HH:mm:ss*/
            cellStyleDate = book.CreateCellStyle();
            IDataFormat formatDate = book.CreateDataFormat();

            cellStyleDate.DataFormat   = formatDate.GetFormat("yyyy-MM-dd HH:mm:ss");
            cellStyleDate.Alignment    = NPOI.SS.UserModel.HorizontalAlignment.Center;
            cellStyleDate.BorderBottom = BorderStyle.Thin;
            cellStyleDate.BorderTop    = BorderStyle.Thin;
            cellStyleDate.BorderLeft   = BorderStyle.Thin;
            cellStyleDate.BorderRight  = BorderStyle.Thin;

            /*新建名为cellStyleSmallDate的(日期用)CellStyle,水平居中,日期格式化为yyyy-MM-dd*/
            cellStyleSmallDate = book.CreateCellStyle();
            IDataFormat formatSmallDate = book.CreateDataFormat();

            cellStyleSmallDate.DataFormat   = formatSmallDate.GetFormat("yyyy-MM-dd");
            cellStyleSmallDate.Alignment    = NPOI.SS.UserModel.HorizontalAlignment.Center;
            cellStyleSmallDate.BorderBottom = BorderStyle.Thin;
            cellStyleSmallDate.BorderTop    = BorderStyle.Thin;
            cellStyleSmallDate.BorderLeft   = BorderStyle.Thin;
            cellStyleSmallDate.BorderRight  = BorderStyle.Thin;

            /*新建名为cellStyleNumber的(千位分割数字)CellStyle,水平居中,整数化*/
            cellStyleNumber = book.CreateCellStyle();
            IDataFormat formatNumber = book.CreateDataFormat();

            cellStyleNumber.DataFormat   = formatNumber.GetFormat("#,##0");
            cellStyleNumber.Alignment    = NPOI.SS.UserModel.HorizontalAlignment.Right;
            cellStyleNumber.BorderBottom = BorderStyle.Thin;
            cellStyleNumber.BorderTop    = BorderStyle.Thin;
            cellStyleNumber.BorderLeft   = BorderStyle.Thin;
            cellStyleNumber.BorderRight  = BorderStyle.Thin;

            /*cellStyleCurrency(千位分割货币)CellStyle,水平居中,整数化*/
            cellStyleCurrency = book.CreateCellStyle();
            IDataFormat formatCurrency = book.CreateDataFormat();

            cellStyleCurrency.DataFormat   = formatCurrency.GetFormat("#,##0.00");
            cellStyleCurrency.Alignment    = NPOI.SS.UserModel.HorizontalAlignment.Right;
            cellStyleCurrency.BorderBottom = BorderStyle.Thin;
            cellStyleCurrency.BorderTop    = BorderStyle.Thin;
            cellStyleCurrency.BorderLeft   = BorderStyle.Thin;
            cellStyleCurrency.BorderRight  = BorderStyle.Thin;

            /*新建名为cellStyleDouble的(折扣用)CellStyle,水平居中,整数化*/
            cellStyleDouble = book.CreateCellStyle();
            IDataFormat formatDoubel = book.CreateDataFormat();

            cellStyleDouble.DataFormat   = formatDoubel.GetFormat("0.00");
            cellStyleDouble.Alignment    = NPOI.SS.UserModel.HorizontalAlignment.Right;
            cellStyleDouble.BorderBottom = BorderStyle.Thin;
            cellStyleDouble.BorderTop    = BorderStyle.Thin;
            cellStyleDouble.BorderLeft   = BorderStyle.Thin;
            cellStyleDouble.BorderRight  = BorderStyle.Thin;

            /*新建名为cellStyleNumber4LemonChiffon的(千位分割数字用)CellStyle,颜色LemonChiffon,水平剧中*/
            cellStyleNumber4LemonChiffon = book.CreateCellStyle();
            IDataFormat formatNumber4LemonChiffon = book.CreateDataFormat();

            cellStyleNumber4LemonChiffon.DataFormat          = formatNumber4LemonChiffon.GetFormat("#,##0");
            cellStyleNumber4LemonChiffon.Alignment           = NPOI.SS.UserModel.HorizontalAlignment.Right;
            cellStyleNumber4LemonChiffon.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LemonChiffon.Index;
            cellStyleNumber4LemonChiffon.FillPattern         = FillPattern.SolidForeground;
            cellStyleNumber4LemonChiffon.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LemonChiffon.Index;
            cellStyleNumber4LemonChiffon.BorderBottom        = BorderStyle.Thin;
            cellStyleNumber4LemonChiffon.BorderTop           = BorderStyle.Thin;
            cellStyleNumber4LemonChiffon.BorderLeft          = BorderStyle.Thin;
            cellStyleNumber4LemonChiffon.BorderRight         = BorderStyle.Thin;

            /*新建名为cellStyleNumber4CornflowerBlue的(千位分割数字用)CellStyle,颜色CornflowerBlue,水平剧中*/
            cellStyleNumber4CornflowerBlue = book.CreateCellStyle();
            IDataFormat formatNumber4CornflowerBlue = book.CreateDataFormat();

            cellStyleNumber4CornflowerBlue.DataFormat          = formatNumber4CornflowerBlue.GetFormat("#,##0");
            cellStyleNumber4CornflowerBlue.Alignment           = NPOI.SS.UserModel.HorizontalAlignment.Right;
            cellStyleNumber4CornflowerBlue.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.CornflowerBlue.Index;
            cellStyleNumber4CornflowerBlue.FillPattern         = FillPattern.SolidForeground;
            cellStyleNumber4CornflowerBlue.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.CornflowerBlue.Index;
            cellStyleNumber4CornflowerBlue.BorderBottom        = BorderStyle.Thin;
            cellStyleNumber4CornflowerBlue.BorderTop           = BorderStyle.Thin;
            cellStyleNumber4CornflowerBlue.BorderLeft          = BorderStyle.Thin;
            cellStyleNumber4CornflowerBlue.BorderRight         = BorderStyle.Thin;

            /*新建名为cellStyleNumber4LightGreen的(千位分割数字用)CellStyle,颜色LightGreen,水平剧中*/
            cellStyleNumber4LightGreen = book.CreateCellStyle();
            IDataFormat formatNumber4LightGreen = book.CreateDataFormat();

            cellStyleNumber4LightGreen.DataFormat          = formatNumber4LightGreen.GetFormat("#,##0");
            cellStyleNumber4LightGreen.Alignment           = NPOI.SS.UserModel.HorizontalAlignment.Right;
            cellStyleNumber4LightGreen.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LightGreen.Index;
            cellStyleNumber4LightGreen.FillPattern         = FillPattern.SolidForeground;
            cellStyleNumber4LightGreen.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LightGreen.Index;
            cellStyleNumber4LightGreen.BorderBottom        = BorderStyle.Thin;
            cellStyleNumber4LightGreen.BorderTop           = BorderStyle.Thin;
            cellStyleNumber4LightGreen.BorderLeft          = BorderStyle.Thin;
            cellStyleNumber4LightGreen.BorderRight         = BorderStyle.Thin;

            /*新建名为cellStyleNumber4Lime的(千位分割数字用)CellStyle,颜色Lime,水平剧中*/
            cellStyleNumber4Lime = book.CreateCellStyle();
            IDataFormat formatNumber4Lime = book.CreateDataFormat();

            cellStyleNumber4Lime.DataFormat          = formatNumber4Lime.GetFormat("#,##0");
            cellStyleNumber4Lime.Alignment           = NPOI.SS.UserModel.HorizontalAlignment.Right;
            cellStyleNumber4Lime.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Lime.Index;
            cellStyleNumber4Lime.FillPattern         = FillPattern.SolidForeground;
            cellStyleNumber4Lime.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Lime.Index;
            cellStyleNumber4Lime.BorderBottom        = BorderStyle.Thin;
            cellStyleNumber4Lime.BorderTop           = BorderStyle.Thin;
            cellStyleNumber4Lime.BorderLeft          = BorderStyle.Thin;
            cellStyleNumber4Lime.BorderRight         = BorderStyle.Thin;

            /*新建名为cellStyleCurrency4LemonChiffon的(千位分割数字用)CellStyle,颜色LemonChiffon,水平剧中*/
            cellStyleCurrency4LemonChiffon = book.CreateCellStyle();
            IDataFormat formatCurrency4LemonChiffon = book.CreateDataFormat();

            cellStyleCurrency4LemonChiffon.DataFormat          = formatCurrency4LemonChiffon.GetFormat("#,##0.00");
            cellStyleCurrency4LemonChiffon.Alignment           = NPOI.SS.UserModel.HorizontalAlignment.Right;
            cellStyleCurrency4LemonChiffon.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LemonChiffon.Index;
            cellStyleCurrency4LemonChiffon.FillPattern         = FillPattern.SolidForeground;
            cellStyleCurrency4LemonChiffon.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LemonChiffon.Index;
            cellStyleCurrency4LemonChiffon.BorderBottom        = BorderStyle.Thin;
            cellStyleCurrency4LemonChiffon.BorderTop           = BorderStyle.Thin;
            cellStyleCurrency4LemonChiffon.BorderLeft          = BorderStyle.Thin;
            cellStyleCurrency4LemonChiffon.BorderRight         = BorderStyle.Thin;

            /*新建名为cellStyleCurrency4CornflowerBlue的(千位分割数字用)CellStyle,颜色CornflowerBlue,水平剧中*/
            cellStyleCurrency4CornflowerBlue = book.CreateCellStyle();
            IDataFormat formatCurrency4CornflowerBlue = book.CreateDataFormat();

            cellStyleCurrency4CornflowerBlue.DataFormat          = formatCurrency4CornflowerBlue.GetFormat("#,##0.00");
            cellStyleCurrency4CornflowerBlue.Alignment           = NPOI.SS.UserModel.HorizontalAlignment.Right;
            cellStyleCurrency4CornflowerBlue.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.CornflowerBlue.Index;
            cellStyleCurrency4CornflowerBlue.FillPattern         = FillPattern.SolidForeground;
            cellStyleCurrency4CornflowerBlue.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.CornflowerBlue.Index;
            cellStyleCurrency4CornflowerBlue.BorderBottom        = BorderStyle.Thin;
            cellStyleCurrency4CornflowerBlue.BorderTop           = BorderStyle.Thin;
            cellStyleCurrency4CornflowerBlue.BorderLeft          = BorderStyle.Thin;
            cellStyleCurrency4CornflowerBlue.BorderRight         = BorderStyle.Thin;

            /*新建名为cellStyleCurrency4LightGreen的(千位分割数字用)CellStyle,颜色LightGreen,水平剧中*/
            cellStyleCurrency4LightGreen = book.CreateCellStyle();
            IDataFormat formatCurrency4LightGreen = book.CreateDataFormat();

            cellStyleCurrency4LightGreen.DataFormat          = formatCurrency4LightGreen.GetFormat("#,##0.00");
            cellStyleCurrency4LightGreen.Alignment           = NPOI.SS.UserModel.HorizontalAlignment.Right;
            cellStyleCurrency4LightGreen.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LightGreen.Index;
            cellStyleCurrency4LightGreen.FillPattern         = FillPattern.SolidForeground;
            cellStyleCurrency4LightGreen.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LightGreen.Index;
            cellStyleCurrency4LightGreen.BorderBottom        = BorderStyle.Thin;
            cellStyleCurrency4LightGreen.BorderTop           = BorderStyle.Thin;
            cellStyleCurrency4LightGreen.BorderLeft          = BorderStyle.Thin;
            cellStyleCurrency4LightGreen.BorderRight         = BorderStyle.Thin;

            /*新建名为cellStyleCurrency4Lime的(千位分割数字用)CellStyle,颜色Lime,水平剧中*/
            cellStyleCurrency4Lime = book.CreateCellStyle();
            IDataFormat formatCurrency4Lime = book.CreateDataFormat();

            cellStyleCurrency4Lime.DataFormat          = formatCurrency4Lime.GetFormat("#,##0.00");
            cellStyleCurrency4Lime.Alignment           = NPOI.SS.UserModel.HorizontalAlignment.Right;
            cellStyleCurrency4Lime.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Lime.Index;
            cellStyleCurrency4Lime.FillPattern         = FillPattern.SolidForeground;
            cellStyleCurrency4Lime.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Lime.Index;
            cellStyleCurrency4Lime.BorderBottom        = BorderStyle.Thin;
            cellStyleCurrency4Lime.BorderTop           = BorderStyle.Thin;
            cellStyleCurrency4Lime.BorderLeft          = BorderStyle.Thin;
            cellStyleCurrency4Lime.BorderRight         = BorderStyle.Thin;
        }
示例#7
0
        protected override void GenerateWorkbookDetail(NPOI.HSSF.UserModel.HSSFWorkbook workBook, System.Data.DataSet dataSet)
        {
            NPOI.HSSF.UserModel.HSSFSheet workSheet = (NPOI.HSSF.UserModel.HSSFSheet)workBook.CreateSheet("Payroll Allocation Report - Detail");

            ushort rowPos = 0;

            DataTable empInfoTable          = dataSet.Tables["EmpInfo"];
            DataTable paymentTable          = dataSet.Tables["payment"];
            DataTable costCenterDetailTable = dataSet.Tables["CostCenterDetail"];
            DataTable hierarchyTable        = dataSet.Tables["hierarchy"];
            DataTable payPeriodTable        = dataSet.Tables["payPeriod"];

            DataTable hierarchyTotalTable = new DataTable("HierarchyTotal");    // use for calculate local hierarchy total

            hierarchyTotalTable.Columns.Add("company", typeof(string));
            hierarchyTotalTable.Columns.Add("LevelDesc", typeof(string));
            hierarchyTotalTable.Columns.Add("payPeriodFr", typeof(DateTime));
            hierarchyTotalTable.Columns.Add("ee", typeof(double));
            hierarchyTotalTable.Columns.Add("er", typeof(double));
            hierarchyTotalTable.Columns.Add("netAmount", typeof(double));

            NPOI.HSSF.UserModel.HSSFFont boldFont = (NPOI.HSSF.UserModel.HSSFFont)workBook.CreateFont();
            boldFont.Boldweight = 700;

            NPOI.HSSF.UserModel.HSSFCellStyle reportHeaderStyle = (NPOI.HSSF.UserModel.HSSFCellStyle)workBook.CreateCellStyle();
            reportHeaderStyle.SetFont(boldFont);
            reportHeaderStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.LEFT;

            NPOI.HSSF.UserModel.HSSFCellStyle groupHeaderStyle = (NPOI.HSSF.UserModel.HSSFCellStyle)workBook.CreateCellStyle();
            groupHeaderStyle.SetFont(boldFont);

            NPOI.HSSF.UserModel.HSSFCellStyle monthHeaderStyle = (NPOI.HSSF.UserModel.HSSFCellStyle)workBook.CreateCellStyle();
            monthHeaderStyle.SetFont(boldFont);
            monthHeaderStyle.DataFormat = workBook.CreateDataFormat().GetFormat("MMM-yyyy");
            monthHeaderStyle.Alignment  = NPOI.SS.UserModel.HorizontalAlignment.CENTER;

            NPOI.HSSF.UserModel.HSSFCellStyle columnHeaderStyle = (NPOI.HSSF.UserModel.HSSFCellStyle)workBook.CreateCellStyle();
            columnHeaderStyle.SetFont(boldFont);
            columnHeaderStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.THIN;

            NPOI.HSSF.UserModel.HSSFCellStyle detailNumberStyle = (NPOI.HSSF.UserModel.HSSFCellStyle)workBook.CreateCellStyle();
            detailNumberStyle.DataFormat = workBook.CreateDataFormat().GetFormat("#,##0.00;(#,##0.00)");
            detailNumberStyle.Alignment  = NPOI.SS.UserModel.HorizontalAlignment.RIGHT;

            NPOI.HSSF.UserModel.HSSFCellStyle detailTextStyle = (NPOI.HSSF.UserModel.HSSFCellStyle)workBook.CreateCellStyle();
            detailTextStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.LEFT;

            NPOI.HSSF.UserModel.HSSFCellStyle subTotalStyle = (NPOI.HSSF.UserModel.HSSFCellStyle)workBook.CreateCellStyle();
            subTotalStyle.DataFormat = workBook.CreateDataFormat().GetFormat("#,##0.00;(#,##0.00)");
            subTotalStyle.BorderTop  = NPOI.SS.UserModel.BorderStyle.THIN;

            DataRow[] EmpInfoRows = dataSet.Tables["EmpInfo"].Select("", "Company, " + empInfoTable.Columns[hierarchyLevelGroupingFieldName].ColumnName + ", Employee No.");

            string currentCompany        = "#$%@$@#$"; //string.Empty;
            string currentHierarchyGroup = string.Empty;
            string currentEmployeeNo     = string.Empty;
            string tmpCompany            = "";
            string tmpHierarchy          = "";
            string tmpEmployeeNo         = "";
            ushort groupRowCount         = 0;
            bool   sectionEnded          = false;


            if (bolShowIndividual == true)
            {
                summaryStart = Convert.ToUInt16(4 + hierarchyTable.Rows.Count);
            }
            else
            {
                summaryStart = 1;
            }
            reportEndCol = Convert.ToUInt16(summaryStart + (payPeriodTable.Rows.Count * 4) - 1);

            foreach (DataRow EmpInfoRow in EmpInfoRows)
            {
                tmpCompany    = EmpInfoRow["Company"].ToString();
                tmpEmployeeNo = EmpInfoRow["Employee No."].ToString();
                tmpHierarchy  = EmpInfoRow[hierarchyLevelGroupingFieldName].ToString();

                if (EmpInfoRows[0] == EmpInfoRow)
                {
                    currentHierarchyGroup = tmpHierarchy;
                    currentCompany        = tmpCompany;

                    rowPos = GenerateHeader(workSheet, PeriodFrom, PeriodTo, rowPos, reportHeaderStyle, groupHeaderStyle);
                    rowPos = GenerateCompanyHeader(workSheet, dataSet, tmpCompany, rowPos, groupHeaderStyle);
                    rowPos++;
                    rowPos = GenerateColumnHeader(workSheet, dataSet, hierarchyLevelGroupingFieldName, rowPos, groupHeaderStyle, monthHeaderStyle, columnHeaderStyle);
                }

                sectionEnded = (!currentHierarchyGroup.Equals(tmpHierarchy, StringComparison.CurrentCultureIgnoreCase) ||
                                !currentCompany.Equals(tmpCompany, StringComparison.CurrentCultureIgnoreCase));

                if (sectionEnded)
                {
                    if (bolShowIndividual == true)
                    {
                        // print hirarchy total if show employee detail
                        rowPos  = GenerateHierarchyTotal(workSheet, rowPos, subTotalStyle, groupRowCount);
                        rowPos += 2;
                    }
                    else
                    {
                        // just print hierarchy line (i.e. not subtotal) if employee detail not shown
                        ushort colPos = 0;
                        NPOI.HSSF.UserModel.HSSFRow  sheetRow = (NPOI.HSSF.UserModel.HSSFRow)workSheet.CreateRow((int)rowPos);
                        NPOI.HSSF.UserModel.HSSFCell cell     = (NPOI.HSSF.UserModel.HSSFCell)sheetRow.CreateCell((int)colPos);

                        cell = (NPOI.HSSF.UserModel.HSSFCell)sheetRow.CreateCell(colPos);
                        cell.SetCellValue(currentHierarchyGroup);
                        cell.CellStyle = detailTextStyle;
                        colPos++;

                        foreach (DataRow m_hiearchyTotalRow in hierarchyTotalTable.Select("LevelDesc = '" + currentHierarchyGroup + "' ", "payPeriodFr"))
                        {
                            cell = (NPOI.HSSF.UserModel.HSSFCell)sheetRow.CreateCell(colPos);
                            cell.SetCellValue((double)m_hiearchyTotalRow["EE"]);
                            cell.CellStyle = detailNumberStyle;
                            colPos++;

                            cell = (NPOI.HSSF.UserModel.HSSFCell)sheetRow.CreateCell(colPos);
                            cell.SetCellValue((double)m_hiearchyTotalRow["netAmount"]);
                            cell.CellStyle = detailNumberStyle;
                            colPos++;

                            cell = (NPOI.HSSF.UserModel.HSSFCell)sheetRow.CreateCell(colPos);
                            cell.SetCellValue((double)m_hiearchyTotalRow["ER"]);
                            cell.CellStyle = detailNumberStyle;
                            colPos++;

                            cell             = (NPOI.HSSF.UserModel.HSSFCell)sheetRow.CreateCell(colPos);
                            cell.CellFormula = "SUM(" + ToCellString(rowPos, colPos - 3) + ":" + ToCellString(rowPos, colPos - 1) + ")";
                            cell.CellStyle   = detailNumberStyle;
                            colPos++;
                        }
                        rowPos++;
                        groupRowCount++;
                    }

                    if (!currentCompany.Equals(tmpCompany, StringComparison.CurrentCultureIgnoreCase))
                    {
                        if (bolShowIndividual == true)
                        {
                            // print company header and column header for new company (in show-employee mode)
                            rowPos = GenerateCompanyHeader(workSheet, dataSet, tmpCompany, rowPos, groupHeaderStyle);
                            rowPos++;
                            rowPos = GenerateColumnHeader(workSheet, dataSet, hierarchyLevelGroupingFieldName, rowPos, groupHeaderStyle, monthHeaderStyle, columnHeaderStyle);
                        }
                        else
                        {
                            // print a summary for previous company (in hide employee info mode)
                            rowPos  = GenerateHierarchyTotal(workSheet, rowPos, subTotalStyle, groupRowCount);
                            rowPos += 2;
                            rowPos  = GenerateCompanyHeader(workSheet, dataSet, tmpCompany, rowPos, groupHeaderStyle);
                            rowPos++;
                            rowPos = GenerateColumnHeader(workSheet, dataSet, hierarchyLevelGroupingFieldName, rowPos, groupHeaderStyle, monthHeaderStyle, columnHeaderStyle);
                        }
                        currentCompany        = tmpCompany;
                        currentHierarchyGroup = tmpHierarchy;
                        groupRowCount         = 0;

                        // print company information for new company
                    }
                    else if (!currentHierarchyGroup.Equals(tmpHierarchy, StringComparison.CurrentCultureIgnoreCase))
                    {
                        if (bolShowIndividual == true)
                        {
                            rowPos        = GenerateColumnHeader(workSheet, dataSet, hierarchyLevelGroupingFieldName, rowPos, groupHeaderStyle, monthHeaderStyle, columnHeaderStyle);
                            groupRowCount = 0;
                        }
                        else
                        {
                        }
                        currentHierarchyGroup = tmpHierarchy;
                    }

                    hierarchyTotalTable.Clear();
                }

                if (bolShowIndividual == true)
                {
                    groupRowCount++;

                    NPOI.HSSF.UserModel.HSSFRow  m_sheetRow = (NPOI.HSSF.UserModel.HSSFRow)workSheet.CreateRow(rowPos);
                    NPOI.HSSF.UserModel.HSSFCell cell;
                    ushort colPos = 0;

                    // Employee Number
                    cell = (NPOI.HSSF.UserModel.HSSFCell)m_sheetRow.CreateCell(colPos);
                    cell.SetCellValue(EmpInfoRow["Employee No."].ToString());
                    cell.CellStyle = detailTextStyle;
                    colPos++;

                    // Employee Name
                    cell = (NPOI.HSSF.UserModel.HSSFCell)m_sheetRow.CreateCell(colPos);
                    cell.SetCellValue(EmpInfoRow["Employee Name"].ToString());
                    cell.CellStyle = detailTextStyle;
                    colPos++;

                    // Alias
                    cell = (NPOI.HSSF.UserModel.HSSFCell)m_sheetRow.CreateCell(colPos);
                    cell.SetCellValue(EmpInfoRow["Alias"].ToString());
                    cell.CellStyle = detailTextStyle;
                    colPos++;

                    // Chinese Name
                    cell = (NPOI.HSSF.UserModel.HSSFCell)m_sheetRow.CreateCell(colPos);
                    cell.SetCellValue(EmpInfoRow["Chinese Name"].ToString());
                    cell.CellStyle = detailTextStyle;
                    colPos++;

                    // hierarchy
                    foreach (DataRow m_hierarchyRow in hierarchyTable.Rows)
                    {
                        cell = (NPOI.HSSF.UserModel.HSSFCell)m_sheetRow.CreateCell(colPos);
                        cell.SetCellValue(EmpInfoRow[m_hierarchyRow["LevelDesc"].ToString()].ToString());
                        cell.CellStyle = detailTextStyle;
                        colPos++;
                    }

                    foreach (DataRow m_payPeriodRow in payPeriodTable.Rows)
                    {
                        //DBFilter m_paymentFilter = new DBFilter();
                        //m_paymentFilter.add(new Match("payPeriodFr", m_payPeriodRow["payPeriodFr"]));
                        //m_paymentFilter.add(new Match("EmpID", row["EmpID"]));

                        double m_er        = 0;
                        double m_ee        = 0;
                        double m_netAmount = 0;
                        foreach (DataRow m_paymentRow in paymentTable.Select("payPeriodFr='" + ((DateTime)m_payPeriodRow["payPeriodFr"]).ToString("yyyy-MM-dd") + "' AND " +
                                                                             "EmpID=" + EmpInfoRow["EmpID"].ToString()))
                        {
                            m_ee        += (double)m_paymentRow["EE"];
                            m_er        += (double)m_paymentRow["ER"];
                            m_netAmount += (double)m_paymentRow["netAmount"];
                        }

                        cell = (NPOI.HSSF.UserModel.HSSFCell)m_sheetRow.CreateCell(colPos);
                        cell.SetCellValue(m_ee);
                        cell.CellStyle = detailNumberStyle;
                        colPos++;

                        cell = (NPOI.HSSF.UserModel.HSSFCell)m_sheetRow.CreateCell(colPos);
                        cell.SetCellValue(m_netAmount);
                        cell.CellStyle = detailNumberStyle;
                        colPos++;

                        cell = (NPOI.HSSF.UserModel.HSSFCell)m_sheetRow.CreateCell(colPos);
                        cell.SetCellValue(m_er);
                        cell.CellStyle = detailNumberStyle;
                        colPos++;

                        cell             = (NPOI.HSSF.UserModel.HSSFCell)m_sheetRow.CreateCell(colPos);
                        cell.CellFormula = "SUM(" + ToCellString(rowPos, colPos - 3) + ":" + ToCellString(rowPos, colPos - 1) + ")";
                        cell.CellStyle   = detailNumberStyle;
                        colPos++;
                    }
                    rowPos++;
                }
                else
                {
                    foreach (DataRow m_payPeriodRow in payPeriodTable.Rows)
                    {
                        if (tmpCompany == "Magazines International (Asia) Limited - SALES" && tmpHierarchy == "Sales Division")
                        {
                            double m_trash = 0;
                        }

                        double m_er        = 0;
                        double m_ee        = 0;
                        double m_netAmount = 0;
                        foreach (DataRow m_paymentRow in paymentTable.Select("payPeriodFr='" + ((DateTime)m_payPeriodRow["payPeriodFr"]).ToString("yyyy-MM-dd") + "' AND " +
                                                                             "EmpID=" + EmpInfoRow["EmpID"].ToString()))
                        {
                            m_ee        += (double)m_paymentRow["EE"];
                            m_er        += (double)m_paymentRow["ER"];
                            m_netAmount += (double)m_paymentRow["netAmount"];
                        }

                        // find from local HierarchyTotal table and add the new employee amounts
                        DataRow[] m_hierarchyTotalRows = hierarchyTotalTable.Select("payPeriodFr='" + ((DateTime)m_payPeriodRow["payPeriodFr"]).ToString("yyyy-MM-dd") + "' AND " +
//                                                                                    "Company='" + tmpCompany + "' AND " +
                                                                                    "LevelDesc='" + tmpHierarchy + "' ");
                        if (m_hierarchyTotalRows.Length > 0)
                        {
                            m_hierarchyTotalRows[0]["EE"]        = (double)m_hierarchyTotalRows[0]["EE"] + m_ee;
                            m_hierarchyTotalRows[0]["ER"]        = (double)m_hierarchyTotalRows[0]["ER"] + m_er;
                            m_hierarchyTotalRows[0]["netAmount"] = (double)m_hierarchyTotalRows[0]["netAmount"] + m_netAmount;
                        }
                        else
                        {
                            DataRow m_newHierarchyTotal = hierarchyTotalTable.Rows.Add();
                            m_newHierarchyTotal["company"]     = tmpCompany;
                            m_newHierarchyTotal["LevelDesc"]   = tmpHierarchy;
                            m_newHierarchyTotal["payPeriodFr"] = (DateTime)m_payPeriodRow["payPeriodFr"];
                            m_newHierarchyTotal["EE"]          = m_ee;
                            m_newHierarchyTotal["ER"]          = m_er;
                            m_newHierarchyTotal["netAmount"]   = m_netAmount;
                        }
                    }
                }
            }

            // insert last hierarchy total
            if (bolShowIndividual == true)
            {
                rowPos = GenerateHierarchyTotal(workSheet, rowPos, subTotalStyle, groupRowCount);
            }
            else
            {
                NPOI.HSSF.UserModel.HSSFRow  m_sheetRow = (NPOI.HSSF.UserModel.HSSFRow)workSheet.CreateRow((int)rowPos);
                NPOI.HSSF.UserModel.HSSFCell cell       = (NPOI.HSSF.UserModel.HSSFCell)m_sheetRow.CreateCell(0);
                ushort colPos = 0;

                cell = (NPOI.HSSF.UserModel.HSSFCell)m_sheetRow.CreateCell(colPos);
                cell.SetCellValue(tmpHierarchy);
                cell.CellStyle = detailTextStyle;
                colPos++;

                foreach (DataRow m_hiearchyTotalRow in hierarchyTotalTable.Select("LevelDesc = '" + tmpHierarchy + "' ", "payPeriodFr"))
                {
                    cell = (NPOI.HSSF.UserModel.HSSFCell)m_sheetRow.CreateCell(colPos);
                    cell.SetCellValue((double)m_hiearchyTotalRow["EE"]);
                    cell.CellStyle = detailNumberStyle;
                    colPos++;

                    cell = (NPOI.HSSF.UserModel.HSSFCell)m_sheetRow.CreateCell(colPos);
                    cell.SetCellValue((double)m_hiearchyTotalRow["netAmount"]);
                    cell.CellStyle = detailNumberStyle;
                    colPos++;

                    cell = (NPOI.HSSF.UserModel.HSSFCell)m_sheetRow.CreateCell(colPos);
                    cell.SetCellValue((double)m_hiearchyTotalRow["ER"]);
                    cell.CellStyle = detailNumberStyle;
                    colPos++;

                    cell             = (NPOI.HSSF.UserModel.HSSFCell)m_sheetRow.CreateCell(colPos);
                    cell.CellFormula = "SUM(" + ToCellString(rowPos, colPos - 3) + ":" + ToCellString(rowPos, colPos - 1) + ")";
                    cell.CellStyle   = detailNumberStyle;
                    colPos++;
                }
                groupRowCount++;
                rowPos++;
                rowPos = GenerateHierarchyTotal(workSheet, rowPos, subTotalStyle, groupRowCount);
            }

            for (int i = 0; i <= reportEndCol; i++)
            {
                if (i < summaryStart)
                {
                    workSheet.SetColumnWidth(i, 15 * 254);
                }
                else
                {
                    workSheet.SetColumnWidth(i, 14 * 254);
                }
            }
        }
        public void GenerateExcelReport(string exportFileName)
        {
            DataSet.Payroll_KTPFundStatement dataSet = CreateDataSource();

            int lastRowIndex = 0;

            // Set column style
            NPOI.HSSF.UserModel.HSSFWorkbook workbook  = new NPOI.HSSF.UserModel.HSSFWorkbook();
            NPOI.HSSF.UserModel.HSSFSheet    worksheet = (NPOI.HSSF.UserModel.HSSFSheet)workbook.CreateSheet("KTPF Contribution Report");

            // Date format
            NPOI.HSSF.UserModel.HSSFDataFormat format        = (NPOI.HSSF.UserModel.HSSFDataFormat)workbook.CreateDataFormat();
            NPOI.HSSF.UserModel.HSSFCellStyle  dateCellStyle = (NPOI.HSSF.UserModel.HSSFCellStyle)workbook.CreateCellStyle();
            dateCellStyle.DataFormat   = format.GetFormat("yyyy-MM-dd");
            dateCellStyle.Alignment    = NPOI.SS.UserModel.HorizontalAlignment.RIGHT;
            dateCellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.DOTTED;
            dateCellStyle.BorderLeft   = NPOI.SS.UserModel.BorderStyle.DOTTED;
            dateCellStyle.BorderRight  = NPOI.SS.UserModel.BorderStyle.DOTTED;

            // Numeric format
            NPOI.HSSF.UserModel.HSSFCellStyle numericStyle = (NPOI.HSSF.UserModel.HSSFCellStyle)workbook.CreateCellStyle();
            numericStyle.DataFormat   = workbook.CreateDataFormat().GetFormat("#,##0.00;(#,##0.00);-");
            numericStyle.Alignment    = NPOI.SS.UserModel.HorizontalAlignment.RIGHT;
            numericStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.DOTTED;
            numericStyle.BorderLeft   = NPOI.SS.UserModel.BorderStyle.DOTTED;
            numericStyle.BorderRight  = NPOI.SS.UserModel.BorderStyle.DOTTED;

            // String left format
            NPOI.HSSF.UserModel.HSSFCellStyle stringLeftStyle = (NPOI.HSSF.UserModel.HSSFCellStyle)workbook.CreateCellStyle();
            stringLeftStyle.Alignment    = NPOI.SS.UserModel.HorizontalAlignment.LEFT;
            stringLeftStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.DOTTED;
            stringLeftStyle.BorderLeft   = NPOI.SS.UserModel.BorderStyle.DOTTED;
            stringLeftStyle.BorderRight  = NPOI.SS.UserModel.BorderStyle.DOTTED;

            // String center format
            NPOI.HSSF.UserModel.HSSFCellStyle stringCenterStyle = (NPOI.HSSF.UserModel.HSSFCellStyle)workbook.CreateCellStyle();
            stringCenterStyle.Alignment    = NPOI.SS.UserModel.HorizontalAlignment.CENTER;
            stringCenterStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.DOTTED;
            stringCenterStyle.BorderLeft   = NPOI.SS.UserModel.BorderStyle.DOTTED;
            stringCenterStyle.BorderRight  = NPOI.SS.UserModel.BorderStyle.DOTTED;

            // Column 0 style
            NPOI.HSSF.UserModel.HSSFCellStyle column0Style = (NPOI.HSSF.UserModel.HSSFCellStyle)workbook.CreateCellStyle();
            column0Style.BorderLeft   = NPOI.SS.UserModel.BorderStyle.THIN;
            column0Style.Alignment    = NPOI.SS.UserModel.HorizontalAlignment.LEFT;
            column0Style.BorderBottom = NPOI.SS.UserModel.BorderStyle.DOTTED;
            column0Style.BorderRight  = NPOI.SS.UserModel.BorderStyle.DOTTED;

            // Column 10, 11 style
            NPOI.HSSF.UserModel.HSSFCellStyle column10To11Style = (NPOI.HSSF.UserModel.HSSFCellStyle)workbook.CreateCellStyle();
            column10To11Style.BorderRight  = NPOI.SS.UserModel.BorderStyle.THIN;
            column10To11Style.BorderBottom = NPOI.SS.UserModel.BorderStyle.DOTTED;
            column10To11Style.BorderLeft   = NPOI.SS.UserModel.BorderStyle.DOTTED;

            // Column 4 style
            NPOI.HSSF.UserModel.HSSFCellStyle column4Style = (NPOI.HSSF.UserModel.HSSFCellStyle)workbook.CreateCellStyle();
            column4Style.DataFormat   = workbook.CreateDataFormat().GetFormat("#,##0.00;(#,##0.00);-");
            column4Style.BorderRight  = NPOI.SS.UserModel.BorderStyle.THIN;
            column4Style.BorderBottom = NPOI.SS.UserModel.BorderStyle.DOTTED;
            column4Style.BorderLeft   = NPOI.SS.UserModel.BorderStyle.DOTTED;

            // Column 8 style
            NPOI.HSSF.UserModel.HSSFCellStyle column8Style = (NPOI.HSSF.UserModel.HSSFCellStyle)workbook.CreateCellStyle();
            column8Style.DataFormat   = format.GetFormat("yyyy-MM-dd");
            column8Style.Alignment    = NPOI.SS.UserModel.HorizontalAlignment.RIGHT;
            column8Style.BorderRight  = NPOI.SS.UserModel.BorderStyle.THIN;
            column8Style.BorderBottom = NPOI.SS.UserModel.BorderStyle.DOTTED;
            column8Style.BorderLeft   = NPOI.SS.UserModel.BorderStyle.DOTTED;

            // Align right
            NPOI.HSSF.UserModel.HSSFCellStyle style = (NPOI.HSSF.UserModel.HSSFCellStyle)workbook.CreateCellStyle();
            style.Alignment = NPOI.SS.UserModel.HorizontalAlignment.RIGHT;

            // Bottom border
            NPOI.HSSF.UserModel.HSSFCellStyle bottomBorderStyle = (NPOI.HSSF.UserModel.HSSFCellStyle)workbook.CreateCellStyle();
            bottomBorderStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.THIN;

            // Bold style
            NPOI.HSSF.UserModel.HSSFCellStyle boldStyle = (NPOI.HSSF.UserModel.HSSFCellStyle)workbook.CreateCellStyle();
            NPOI.SS.UserModel.IFont           boldFont  = workbook.CreateFont();
            boldFont.Boldweight = 700;
            boldStyle.SetFont(boldFont);

            // Header Border
            NPOI.HSSF.UserModel.HSSFCellStyle headerBorderStyle = (NPOI.HSSF.UserModel.HSSFCellStyle)workbook.CreateCellStyle();
            headerBorderStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.THIN;
            headerBorderStyle.BorderTop    = NPOI.SS.UserModel.BorderStyle.THIN;
            headerBorderStyle.BorderRight  = NPOI.SS.UserModel.BorderStyle.DOTTED;
            headerBorderStyle.SetFont(boldFont);
            headerBorderStyle.WrapText  = true;
            headerBorderStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CENTER;

            // Header Left Border
            NPOI.HSSF.UserModel.HSSFCellStyle headerLeftBorderStyle = (NPOI.HSSF.UserModel.HSSFCellStyle)workbook.CreateCellStyle();
            headerLeftBorderStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.THIN;
            headerLeftBorderStyle.BorderTop    = NPOI.SS.UserModel.BorderStyle.THIN;
            headerLeftBorderStyle.BorderLeft   = NPOI.SS.UserModel.BorderStyle.THIN;
            headerLeftBorderStyle.BorderRight  = NPOI.SS.UserModel.BorderStyle.DOTTED;
            headerLeftBorderStyle.SetFont(boldFont);
            headerLeftBorderStyle.WrapText  = true;
            headerLeftBorderStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CENTER;

            // Header Right Border
            NPOI.HSSF.UserModel.HSSFCellStyle headerRightBorderStyle = (NPOI.HSSF.UserModel.HSSFCellStyle)workbook.CreateCellStyle();
            headerRightBorderStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.THIN;
            headerRightBorderStyle.BorderTop    = NPOI.SS.UserModel.BorderStyle.THIN;
            headerRightBorderStyle.BorderRight  = NPOI.SS.UserModel.BorderStyle.THIN;
            headerRightBorderStyle.SetFont(boldFont);
            headerRightBorderStyle.WrapText  = true;
            headerRightBorderStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CENTER;

            // Total numeric format
            NPOI.HSSF.UserModel.HSSFCellStyle totalNumericStyle = (NPOI.HSSF.UserModel.HSSFCellStyle)workbook.CreateCellStyle();
            totalNumericStyle.DataFormat = workbook.CreateDataFormat().GetFormat("#,##0.00;(#,##0.00);-");
            totalNumericStyle.Alignment  = NPOI.SS.UserModel.HorizontalAlignment.RIGHT;
            totalNumericStyle.BorderTop  = NPOI.SS.UserModel.BorderStyle.THIN;

            // Grey color
            NPOI.HSSF.UserModel.HSSFCellStyle grey25Style = (NPOI.HSSF.UserModel.HSSFCellStyle)workbook.CreateCellStyle();
            grey25Style.Alignment           = NPOI.SS.UserModel.HorizontalAlignment.CENTER;
            grey25Style.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.GREY_25_PERCENT.index;
            grey25Style.FillPattern         = NPOI.SS.UserModel.FillPatternType.SOLID_FOREGROUND;
            grey25Style.SetFont(boldFont);

            // Yellow color
            NPOI.HSSF.UserModel.HSSFCellStyle yellowStyle = (NPOI.HSSF.UserModel.HSSFCellStyle)workbook.CreateCellStyle();
            yellowStyle.Alignment           = NPOI.SS.UserModel.HorizontalAlignment.CENTER;
            yellowStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LIGHT_YELLOW.index;
            yellowStyle.FillPattern         = NPOI.SS.UserModel.FillPatternType.SOLID_FOREGROUND;
            yellowStyle.SetFont(boldFont);

            // Set column width
            worksheet.SetColumnWidth(0, 12 * 256);
            worksheet.SetColumnWidth(1, 20 * 256);
            worksheet.SetColumnWidth(2, 15 * 256);
            worksheet.SetColumnWidth(3, 15 * 256);
            worksheet.SetColumnWidth(4, 15 * 256);
            worksheet.SetColumnWidth(5, 7 * 256);
            worksheet.SetColumnWidth(6, 15 * 256);
            worksheet.SetColumnWidth(7, 15 * 256);
            worksheet.SetColumnWidth(8, 15 * 256);
            worksheet.SetColumnWidth(9, 15 * 256);
            worksheet.SetColumnWidth(10, 15 * 256);
            worksheet.SetColumnWidth(11, 25 * 256);

            // Set column title
            NPOI.HSSF.UserModel.HSSFRow headerRow = (NPOI.HSSF.UserModel.HSSFRow)worksheet.CreateRow(0);

            NPOI.HSSF.UserModel.HSSFCell headerCell = (NPOI.HSSF.UserModel.HSSFCell)headerRow.CreateCell(0);
            headerCell.SetCellValue("KTPF Contribution Report");
            headerCell.CellStyle = boldStyle;

            headerRow  = (NPOI.HSSF.UserModel.HSSFRow)worksheet.CreateRow(1);
            headerCell = (NPOI.HSSF.UserModel.HSSFCell)headerRow.CreateCell(0);
            if (dataSet.ORSOPlan.Rows.Count > 0)
            {
                Payroll_KTPFundStatement.ORSOPlanRow m_orsoPlan = (Payroll_KTPFundStatement.ORSOPlanRow)dataSet.ORSOPlan.Rows[0];

                headerCell.SetCellValue(m_orsoPlan.ORSOPlanCompanyName);
            }
            headerCell.CellStyle = boldStyle;

            string m_reportPeriod = "";

            if (_payPeriodFr.Month != _payPeriodTo.Month)
            {
                m_reportPeriod = _payPeriodFr.ToString("dd MMMM yyyy") + " - " + _payPeriodTo.ToString("dd MMMM yyyy");
            }
            else
            {
                m_reportPeriod = _payPeriodFr.ToString("MMMM yyyy");
            }

            headerRow  = (NPOI.HSSF.UserModel.HSSFRow)worksheet.CreateRow(2);
            headerCell = (NPOI.HSSF.UserModel.HSSFCell)headerRow.CreateCell(0);
            headerCell.SetCellValue(m_reportPeriod);
            headerCell.CellStyle = boldStyle;

            // Merge header
            headerRow = (NPOI.HSSF.UserModel.HSSFRow)worksheet.CreateRow(3);

            // Merge cell from 5-8
            NPOI.SS.Util.CellRangeAddress cellRangeAddress = new NPOI.SS.Util.CellRangeAddress(3, (short)3, 5, (short)8);
            worksheet.AddMergedRegion(cellRangeAddress);
            headerCell = (NPOI.HSSF.UserModel.HSSFCell)headerRow.CreateCell(5);
            headerCell.SetCellValue("For New Joiner");
            headerCell.CellStyle = yellowStyle;

            ((NPOI.HSSF.UserModel.HSSFSheet)worksheet).SetEnclosedBorderOfRegion(cellRangeAddress, NPOI.SS.UserModel.BorderStyle.THIN, NPOI.HSSF.Util.HSSFColor.BLACK.index);

            // Merge cell from 9-10
            cellRangeAddress = new NPOI.SS.Util.CellRangeAddress(3, (short)3, 9, (short)10);
            worksheet.AddMergedRegion(cellRangeAddress);
            headerCell = (NPOI.HSSF.UserModel.HSSFCell)headerRow.CreateCell(9);
            headerCell.SetCellValue("For Resigned Staff");
            headerCell.CellStyle = grey25Style;
            ((NPOI.HSSF.UserModel.HSSFSheet)worksheet).SetEnclosedBorderOfRegion(cellRangeAddress, NPOI.SS.UserModel.BorderStyle.THIN, NPOI.HSSF.Util.HSSFColor.BLACK.index);

            headerRow = (NPOI.HSSF.UserModel.HSSFRow)worksheet.CreateRow(4);
            headerRow.HeightInPoints = 40;

            // column A
            headerCell = (NPOI.HSSF.UserModel.HSSFCell)headerRow.CreateCell(0);
            headerCell.SetCellValue("Member ID");
            headerCell.CellStyle = headerLeftBorderStyle;

            // column B
            headerCell = (NPOI.HSSF.UserModel.HSSFCell)headerRow.CreateCell(1);
            headerCell.SetCellValue("Employee Name");
            headerCell.CellStyle = headerBorderStyle;

            // column C
            headerCell = (NPOI.HSSF.UserModel.HSSFCell)headerRow.CreateCell(2);
            headerCell.SetCellValue("Basic Salary");
            headerCell.CellStyle = headerBorderStyle;

            // column D
            headerCell = (NPOI.HSSF.UserModel.HSSFCell)headerRow.CreateCell(3);
            headerCell.SetCellValue("KTPF Contribution");
            headerCell.CellStyle = headerBorderStyle;

            // column E
            headerCell = (NPOI.HSSF.UserModel.HSSFCell)headerRow.CreateCell(4);
            headerCell.SetCellValue("Employer MPF Contribution");
            headerCell.CellStyle = headerRightBorderStyle;


            // column F
            headerCell = (NPOI.HSSF.UserModel.HSSFCell)headerRow.CreateCell(5);
            headerCell.SetCellValue("Sex");
            headerCell.CellStyle = headerBorderStyle;


            // column G
            headerCell = (NPOI.HSSF.UserModel.HSSFCell)headerRow.CreateCell(6);
            headerCell.SetCellValue("Date Of Birth");
            headerCell.CellStyle = headerBorderStyle;


            // column H
            headerCell = (NPOI.HSSF.UserModel.HSSFCell)headerRow.CreateCell(7);
            headerCell.SetCellValue("Date Join");
            headerCell.CellStyle = headerBorderStyle;

            // column I
            headerCell = (NPOI.HSSF.UserModel.HSSFCell)headerRow.CreateCell(8);
            headerCell.SetCellValue("Effective Date");
            headerCell.CellStyle = headerRightBorderStyle;

            // column J
            headerCell = (NPOI.HSSF.UserModel.HSSFCell)headerRow.CreateCell(9);
            headerCell.SetCellValue("Termination Date");
            headerCell.CellStyle = headerBorderStyle;

            // column K
            headerCell = (NPOI.HSSF.UserModel.HSSFCell)headerRow.CreateCell(10);
            headerCell.SetCellValue("Termination Mode");
            headerCell.CellStyle = headerRightBorderStyle;

            // column L
            headerCell = (NPOI.HSSF.UserModel.HSSFCell)headerRow.CreateCell(11);
            headerCell.SetCellValue("Remarks");
            headerCell.CellStyle = headerRightBorderStyle;

            // Create total
            int length = dataSet.ExistingMember.Rows.Count + 5;

            NPOI.HSSF.UserModel.HSSFRow totalRow = (NPOI.HSSF.UserModel.HSSFRow)worksheet.CreateRow(lastRowIndex + length);

            NPOI.HSSF.UserModel.HSSFCell totalCell = (NPOI.HSSF.UserModel.HSSFCell)totalRow.CreateCell(0);
            totalCell.CellStyle = totalNumericStyle;
            totalCell           = (NPOI.HSSF.UserModel.HSSFCell)totalRow.CreateCell(5);
            totalCell.CellStyle = totalNumericStyle;
            totalCell           = (NPOI.HSSF.UserModel.HSSFCell)totalRow.CreateCell(6);
            totalCell.CellStyle = totalNumericStyle;
            totalCell           = (NPOI.HSSF.UserModel.HSSFCell)totalRow.CreateCell(7);
            totalCell.CellStyle = totalNumericStyle;
            totalCell           = (NPOI.HSSF.UserModel.HSSFCell)totalRow.CreateCell(8);
            totalCell.CellStyle = totalNumericStyle;
            totalCell           = (NPOI.HSSF.UserModel.HSSFCell)totalRow.CreateCell(9);
            totalCell.CellStyle = totalNumericStyle;
            totalCell           = (NPOI.HSSF.UserModel.HSSFCell)totalRow.CreateCell(10);
            totalCell.CellStyle = totalNumericStyle;
            totalCell           = (NPOI.HSSF.UserModel.HSSFCell)totalRow.CreateCell(11);
            totalCell.CellStyle = totalNumericStyle;

            totalCell = (NPOI.HSSF.UserModel.HSSFCell)totalRow.CreateCell(1);
            totalCell.SetCellValue("Total");
            totalCell.CellStyle = totalNumericStyle;

            totalCell = (NPOI.HSSF.UserModel.HSSFCell)totalRow.CreateCell(2);
            totalCell.SetCellFormula("SUM(C5:C" + totalRow.RowNum.ToString("0") + ")");
            totalCell.CellStyle = totalNumericStyle;

            totalCell = (NPOI.HSSF.UserModel.HSSFCell)totalRow.CreateCell(3);
            totalCell.SetCellFormula("SUM(D5:D" + totalRow.RowNum.ToString("0") + ")");
            totalCell.CellStyle = totalNumericStyle;

            totalCell = (NPOI.HSSF.UserModel.HSSFCell)totalRow.CreateCell(4);
            totalCell.SetCellFormula("SUM(E5:E" + totalRow.RowNum.ToString("0") + ")");
            totalCell.CellStyle = totalNumericStyle;

            int rowLength = 0;

            // Set value for every row

            dataSet.ExistingMember.DefaultView.Sort = "EmpName";
            DataTable m_table = dataSet.ExistingMember.DefaultView.ToTable();


            foreach (DataRow m_row in m_table.Rows)
            //foreach (DataRow row in tmpDataTable.Rows)
            {
                NPOI.HSSF.UserModel.HSSFRow detailRow = (NPOI.HSSF.UserModel.HSSFRow)worksheet.CreateRow(lastRowIndex + 5);
                rowLength++;

                if (lastRowIndex == (m_table.Rows.Count))
                {
                    detailRow.RowStyle = bottomBorderStyle;
                }

                NPOI.HSSF.UserModel.HSSFCell cell = (NPOI.HSSF.UserModel.HSSFCell)detailRow.CreateCell(0);
                cell.SetCellValue(m_row["MemberID"].ToString());
                cell.CellStyle = column0Style;

                cell = (NPOI.HSSF.UserModel.HSSFCell)detailRow.CreateCell(1);
                cell.SetCellValue(m_row["EmpName"].ToString());
                cell.CellStyle = stringLeftStyle;

                cell = (NPOI.HSSF.UserModel.HSSFCell)detailRow.CreateCell(2);
                cell.SetCellValue((double)m_row["RelevantIncome"]);
                cell.CellStyle = numericStyle;

                cell = (NPOI.HSSF.UserModel.HSSFCell)detailRow.CreateCell(3);
                cell.SetCellValue((double)m_row["ER"]);
                cell.CellStyle = numericStyle;

                cell = (NPOI.HSSF.UserModel.HSSFCell)detailRow.CreateCell(4);
                cell.SetCellValue((double)m_row["MpfMCER"]);
                cell.CellStyle = column4Style;

                DateTime m_periodFrom = (DateTime)m_row["PeriodFrom"];
                DateTime m_effDate    = (DateTime)m_row["OrsoEffDate"];

                {
                    cell           = (NPOI.HSSF.UserModel.HSSFCell)detailRow.CreateCell(5);
                    cell.CellStyle = stringCenterStyle;
                    if (m_periodFrom.Year == m_effDate.Year && m_periodFrom.Year == m_effDate.Month)
                    {
                        cell.SetCellValue(m_row["EmpSex"].ToString());
                    }

                    cell           = (NPOI.HSSF.UserModel.HSSFCell)detailRow.CreateCell(6);
                    cell.CellStyle = dateCellStyle;

                    if (m_periodFrom.Year == m_effDate.Year && m_periodFrom.Year == m_effDate.Month)
                    {
                        try
                        {
                            cell.SetCellValue((DateTime)m_row["EmpDOB"]);
                        }
                        catch { }
                    }

                    cell           = (NPOI.HSSF.UserModel.HSSFCell)detailRow.CreateCell(7);
                    cell.CellStyle = dateCellStyle;
                    if (m_periodFrom.Year == m_effDate.Year && m_periodFrom.Year == m_effDate.Month)
                    {
                        try
                        {
                            cell.SetCellValue((DateTime)m_row["EmpDateJoin"]);
                        }
                        catch { }
                    }

                    cell           = (NPOI.HSSF.UserModel.HSSFCell)detailRow.CreateCell(8);
                    cell.CellStyle = column8Style;
                    if (m_periodFrom.Year == m_effDate.Year && m_periodFrom.Year == m_effDate.Month)
                    {
                        try
                        {
                            cell.SetCellValue((DateTime)m_row["OrsoEffDate"]);
                        }
                        catch { }
                    }
                }
                cell           = (NPOI.HSSF.UserModel.HSSFCell)detailRow.CreateCell(9);
                cell.CellStyle = dateCellStyle;
                cell.SetCellValue("");// put something into the cell and so as the border line can be shown
                if (m_periodFrom.Year == m_effDate.Year && m_periodFrom.Year == m_effDate.Month)
                {
                    try
                    {
                        cell.SetCellValue((DateTime)m_row["LastEmploymentDate"]);
                    }
                    catch { }
                }

                cell = (NPOI.HSSF.UserModel.HSSFCell)detailRow.CreateCell(10);
                cell.SetCellValue(m_row["TermCode"].ToString());
                cell.CellStyle = column10To11Style;

                cell = (NPOI.HSSF.UserModel.HSSFCell)detailRow.CreateCell(11);
                //cell.SetCellValue(row[FIELD_REMARKS].ToString());
                cell.CellStyle = column10To11Style;

                lastRowIndex++;
            }

            System.IO.FileStream file = new System.IO.FileStream(exportFileName, System.IO.FileMode.Create);
            workbook.Write(file);
            file.Close();
        }
    private NPOI.HSSF.UserModel.HSSFSheet CreateWorkSheet(NPOI.HSSF.UserModel.HSSFWorkbook workbook, DataTable dataTable)
    {
        if (workbook != null)
        {
            NPOI.HSSF.UserModel.HSSFSheet worksheet = (NPOI.HSSF.UserModel.HSSFSheet)workbook.CreateSheet(dataTable.TableName.Replace("$", ""));
            //NPOI.HSSF.UserModel.HSSFRow chineseHeaderRow = (NPOI.HSSF.UserModel.HSSFRow)worksheet.CreateRow(0);
            NPOI.HSSF.UserModel.HSSFRow headerRow = (NPOI.HSSF.UserModel.HSSFRow)worksheet.CreateRow(0);
            int columnCount = 0;

            //System.Globalization.CultureInfo chineseCI = new System.Globalization.CultureInfo("zh-cht");
            foreach (DataColumn headercolumn in dataTable.Columns)
            {
                headercolumn.ColumnName = headercolumn.ColumnName.Trim();

                if (!headercolumn.ColumnName.EndsWith("TimeCardRecordID", StringComparison.CurrentCultureIgnoreCase))
                {
                    string columnName = headercolumn.ColumnName;
                    //string chineseColumnName = HROne.Common.WebUtility.GetLocalizedString(columnName, chineseCI);
                    //if (columnName.Equals(chineseColumnName))
                    //    chineseColumnName = string.Empty;

                    NPOI.HSSF.UserModel.HSSFCell cell = (NPOI.HSSF.UserModel.HSSFCell)headerRow.CreateCell(columnCount); //new ExcelLibrary.SpreadSheet.Cell(headercolumn.ColumnName, new ExcelLibrary.SpreadSheet.CellFormat(ExcelLibrary.SpreadSheet.CellFormatType.Text, string.Empty));
                    cell.SetCellValue(columnName);

                    //cell = (NPOI.HSSF.UserModel.HSSFCell)chineseHeaderRow.CreateCell(columnCount); //new ExcelLibrary.SpreadSheet.Cell(headercolumn.ColumnName, new ExcelLibrary.SpreadSheet.CellFormat(ExcelLibrary.SpreadSheet.CellFormatType.Text, string.Empty));
                    //cell.SetCellValue(chineseColumnName);

                    //headerRow.SetCell(columnCount,cell);
                    //worksheet.Cells[0, columnCount] = cell;//new ExcelLibrary.SpreadSheet.Cell(column.ColumnName, new ExcelLibrary.SpreadSheet.CellFormat(ExcelLibrary.SpreadSheet.CellFormatType.Text, string.Empty));
                    columnCount++;
                }
            }
            //worksheet.Cells.Rows.Add(0, headerRow);

            int rowCount = 0;


            NPOI.HSSF.UserModel.HSSFDataFormat format = (NPOI.HSSF.UserModel.HSSFDataFormat)workbook.CreateDataFormat();

            NPOI.HSSF.UserModel.HSSFFont boldFont = (NPOI.HSSF.UserModel.HSSFFont)workbook.CreateFont();
            boldFont.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.BOLD; //900;

            NPOI.HSSF.UserModel.HSSFCellStyle dateCellStyle = (NPOI.HSSF.UserModel.HSSFCellStyle)workbook.CreateCellStyle();
            dateCellStyle.DataFormat = format.GetFormat("yyyy-MM-dd");

            NPOI.HSSF.UserModel.HSSFCellStyle ManualAdjustCellStyle = (NPOI.HSSF.UserModel.HSSFCellStyle)workbook.CreateCellStyle();
            //ManualInputDateCellStyle.CloneStyleFrom(dateCellStyle);
            ManualAdjustCellStyle.SetFont(boldFont);

            //NPOI.HSSF.UserModel.HSSFCellStyle numericCellStyle = (NPOI.HSSF.UserModel.HSSFCellStyle)workbook.CreateCellStyle();
            //numericCellStyle.DataFormat = NPOI.HSSF.UserModel.HSSFDataFormat.GetBuiltinFormat("0.00"); ;

            //NPOI.HSSF.UserModel.HSSFCellStyle integerCellStyle = (NPOI.HSSF.UserModel.HSSFCellStyle)workbook.CreateCellStyle();
            //integerCellStyle.DataFormat = NPOI.HSSF.UserModel.HSSFDataFormat.GetBuiltinFormat("0"); ;

            foreach (DataRow row in dataTable.Rows)
            {
                rowCount++;
                columnCount = 0;

                //                    ExcelLibrary.SpreadSheet.Row detailRow = new ExcelLibrary.SpreadSheet.Row();

                NPOI.HSSF.UserModel.HSSFRow detailRow = (NPOI.HSSF.UserModel.HSSFRow)worksheet.CreateRow(rowCount);

                foreach (DataColumn column in dataTable.Columns)
                {
                    if (!column.ColumnName.EndsWith("TimeCardRecordID", StringComparison.CurrentCultureIgnoreCase))
                    {
                        //ExcelLibrary.SpreadSheet.Cell cell =new ExcelLibrary.SpreadSheet.Cell(string.Empty, new ExcelLibrary.SpreadSheet.CellFormat(ExcelLibrary.SpreadSheet.CellFormatType.Text, string.Empty));
                        NPOI.HSSF.UserModel.HSSFCell cell = (NPOI.HSSF.UserModel.HSSFCell)detailRow.CreateCell(columnCount);

                        if (column.DataType.Equals(typeof(string)))
                        {
                            //cell.Format = new ExcelLibrary.SpreadSheet.CellFormat(ExcelLibrary.SpreadSheet.CellFormatType.Text, string.Empty);
                            //cell.Value = (row[column] == System.DBNull.Value ? string.Empty : row[column].ToString());
                            cell.SetCellValue(row[column] == System.DBNull.Value ? string.Empty : row[column].ToString());

                            //  Override style to bold if manual adjust
                            if (cell.StringCellValue != string.Empty)
                            {
                                if (column.ColumnName.Equals(HROne.Import.ImportAttendanceRecordProcess.FIELD_WORKIN))
                                {
                                    ETimeCardRecord timeCard = new ETimeCardRecord();
                                    try
                                    {
                                        timeCard.TimeCardRecordID = (int)row[HROne.Import.ImportAttendanceRecordProcess.FIELD_WORKIN_TIMECARDID];
                                    }
                                    catch { }
                                    if (ETimeCardRecord.db.select(dbConn, timeCard))
                                    {
                                        if (!timeCard.TimeCardRecordDateTime.ToString("HH:mm").Equals(cell.StringCellValue))
                                        {
                                            cell.CellStyle = ManualAdjustCellStyle;
                                        }
                                    }
                                    else
                                    {
                                        cell.CellStyle = ManualAdjustCellStyle;
                                    }
                                }
                                else if (column.ColumnName.Equals(HROne.Import.ImportAttendanceRecordProcess.FIELD_WORKOUT))
                                {
                                    ETimeCardRecord timeCard = new ETimeCardRecord();
                                    try
                                    {
                                        timeCard.TimeCardRecordID = (int)row[HROne.Import.ImportAttendanceRecordProcess.FIELD_WORKOUT_TIMECARDID];
                                    }
                                    catch { }
                                    if (ETimeCardRecord.db.select(dbConn, timeCard))
                                    {
                                        if (!timeCard.TimeCardRecordDateTime.ToString("HH:mm").Equals(cell.StringCellValue))
                                        {
                                            cell.CellStyle = ManualAdjustCellStyle;
                                        }
                                    }
                                    else
                                    {
                                        cell.CellStyle = ManualAdjustCellStyle;
                                    }
                                }
                                else if (column.ColumnName.Equals(HROne.Import.ImportAttendanceRecordProcess.FIELD_LUNCHOUT))
                                {
                                    ETimeCardRecord timeCard = new ETimeCardRecord();
                                    try
                                    {
                                        timeCard.TimeCardRecordID = (int)row[HROne.Import.ImportAttendanceRecordProcess.FIELD_LUNCHOUT_TIMECARDID];
                                    }
                                    catch { }
                                    if (ETimeCardRecord.db.select(dbConn, timeCard))
                                    {
                                        if (!timeCard.TimeCardRecordDateTime.ToString("HH:mm").Equals(cell.StringCellValue))
                                        {
                                            cell.CellStyle = ManualAdjustCellStyle;
                                        }
                                    }
                                    else
                                    {
                                        cell.CellStyle = ManualAdjustCellStyle;
                                    }
                                }
                                else if (column.ColumnName.Equals(HROne.Import.ImportAttendanceRecordProcess.FIELD_LUNCHIN))
                                {
                                    ETimeCardRecord timeCard = new ETimeCardRecord();
                                    try
                                    {
                                        timeCard.TimeCardRecordID = (int)row[HROne.Import.ImportAttendanceRecordProcess.FIELD_LUNCHIN_TIMECARDID];
                                    }
                                    catch { }
                                    if (ETimeCardRecord.db.select(dbConn, timeCard))
                                    {
                                        if (!timeCard.TimeCardRecordDateTime.ToString("HH:mm").Equals(cell.StringCellValue))
                                        {
                                            cell.CellStyle = ManualAdjustCellStyle;
                                        }
                                    }
                                    else
                                    {
                                        cell.CellStyle = ManualAdjustCellStyle;
                                    }
                                }
                                else if (column.ColumnName.Equals(HROne.Import.ImportAttendanceRecordProcess.FIELD_WORKIN_LOCATION))
                                {
                                    ETimeCardRecord timeCard = new ETimeCardRecord();
                                    try
                                    {
                                        timeCard.TimeCardRecordID = (int)row[HROne.Import.ImportAttendanceRecordProcess.FIELD_WORKIN_TIMECARDID];
                                    }
                                    catch { }
                                    if (ETimeCardRecord.db.select(dbConn, timeCard))
                                    {
                                        if (!timeCard.TimeCardRecordLocation.Equals(cell.StringCellValue))
                                        {
                                            cell.CellStyle = ManualAdjustCellStyle;
                                        }
                                    }
                                    else
                                    {
                                        cell.CellStyle = ManualAdjustCellStyle;
                                    }
                                }
                                else if (column.ColumnName.Equals(HROne.Import.ImportAttendanceRecordProcess.FIELD_WORKOUT_LOCATION))
                                {
                                    ETimeCardRecord timeCard = new ETimeCardRecord();
                                    try
                                    {
                                        timeCard.TimeCardRecordID = (int)row[HROne.Import.ImportAttendanceRecordProcess.FIELD_WORKOUT_TIMECARDID];
                                    }
                                    catch { }
                                    if (ETimeCardRecord.db.select(dbConn, timeCard))
                                    {
                                        if (!timeCard.TimeCardRecordLocation.Equals(cell.StringCellValue))
                                        {
                                            cell.CellStyle = ManualAdjustCellStyle;
                                        }
                                    }
                                    else
                                    {
                                        cell.CellStyle = ManualAdjustCellStyle;
                                    }
                                }
                                else if (column.ColumnName.Equals(HROne.Import.ImportAttendanceRecordProcess.FIELD_LUNCHOUT_LOCATION))
                                {
                                    ETimeCardRecord timeCard = new ETimeCardRecord();
                                    try
                                    {
                                        timeCard.TimeCardRecordID = (int)row[HROne.Import.ImportAttendanceRecordProcess.FIELD_LUNCHOUT_TIMECARDID];
                                    }
                                    catch { }
                                    if (ETimeCardRecord.db.select(dbConn, timeCard))
                                    {
                                        if (!timeCard.TimeCardRecordLocation.Equals(cell.StringCellValue))
                                        {
                                            cell.CellStyle = ManualAdjustCellStyle;
                                        }
                                    }
                                    else
                                    {
                                        cell.CellStyle = ManualAdjustCellStyle;
                                    }
                                }
                                else if (column.ColumnName.Equals(HROne.Import.ImportAttendanceRecordProcess.FIELD_LUNCHIN_LOCATION))
                                {
                                    ETimeCardRecord timeCard = new ETimeCardRecord();
                                    try
                                    {
                                        timeCard.TimeCardRecordID = (int)row[HROne.Import.ImportAttendanceRecordProcess.FIELD_LUNCHIN_TIMECARDID];
                                    }
                                    catch { }
                                    if (ETimeCardRecord.db.select(dbConn, timeCard))
                                    {
                                        if (!timeCard.TimeCardRecordLocation.Equals(cell.StringCellValue))
                                        {
                                            cell.CellStyle = ManualAdjustCellStyle;
                                        }
                                    }
                                    else
                                    {
                                        cell.CellStyle = ManualAdjustCellStyle;
                                    }
                                }
                            }
                        }
                        else if (column.DataType.Equals(typeof(double)) || column.DataType.Equals(typeof(float)))
                        {
                            //cell.Format = new ExcelLibrary.SpreadSheet.CellFormat(ExcelLibrary.SpreadSheet.CellFormatType.Number, "0.00");
                            //cell.Value = (row[column] == System.DBNull.Value ? string.Empty : row[column]);
                            if (row[column] != System.DBNull.Value)
                            {
                                double value = Convert.ToDouble(row[column].ToString());
                                if (value.Equals(double.NaN))
                                {
                                    cell.SetCellValue(string.Empty);
                                }
                                else
                                {
                                    cell.SetCellValue(value);
                                }
                            }
                            //cell.CellStyle = numericCellStyle;
                        }
                        else if (column.DataType.Equals(typeof(int)))
                        {
                            //cell.Format = new ExcelLibrary.SpreadSheet.CellFormat(ExcelLibrary.SpreadSheet.CellFormatType.Number, "0.00");
                            //cell.Value = (row[column] == System.DBNull.Value ? string.Empty : row[column]);
                            if (row[column] != System.DBNull.Value)
                            {
                                cell.SetCellValue(Convert.ToDouble(row[column].ToString()));
                            }
                            //cell.CellStyle = integerCellStyle;
                        }
                        else if (column.DataType.Equals(typeof(DateTime)))
                        {
                            //cell.Format = new ExcelLibrary.SpreadSheet.CellFormat(ExcelLibrary.SpreadSheet.CellFormatType.DateTime, "yyyy-MM-dd");
                            //if (row[column] == System.DBNull.Value)
                            //    cell.Value = string.Empty;
                            //else
                            //    cell.Value = (DateTime)row[column];
                            if (row[column] != System.DBNull.Value)
                            {
                                cell.SetCellValue((DateTime)row[column]);
                            }

                            cell.CellStyle = dateCellStyle;
                        }
                        else
                        {
                            //cell.Format = new ExcelLibrary.SpreadSheet.CellFormat(ExcelLibrary.SpreadSheet.CellFormatType.Text, string.Empty);
                            //cell.Value = (row[column] == System.DBNull.Value ? string.Empty : row[column].ToString());
                            if (row[column] != System.DBNull.Value)
                            {
                                cell.SetCellValue(row[column].ToString());
                            }
                        }
                        //worksheet.Cells[rowCount, columnCount] = cell;
                        columnCount++;
                    }
                }
                //                    worksheet.Cells.Rows.Add(rowCount, detailRow);
            }
            //workbook.Worksheets.Add(worksheet);

            return(worksheet);
        }
        else
        {
            return(null);
        }
    }
示例#10
0
        protected override void GenerateWorkbookDetail(NPOI.HSSF.UserModel.HSSFWorkbook workBook, System.Data.DataSet dataSet)
        {
            NPOI.HSSF.UserModel.HSSFSheet workSheet = (NPOI.HSSF.UserModel.HSSFSheet)workBook.CreateSheet("Payroll Detail");

            ushort rowPos = 0;

            DataTable empInfoTable          = dataSet.Tables["EmpInfo"];
            DataTable paymentTable          = dataSet.Tables["payment"];
            DataTable costCenterDetailTable = dataSet.Tables["CostCenterDetail"];

            NPOI.HSSF.UserModel.HSSFFont boldFont = (NPOI.HSSF.UserModel.HSSFFont)workBook.CreateFont();
            boldFont.Boldweight = 700;

            NPOI.HSSF.UserModel.HSSFCellStyle reportHeaderStyle = (NPOI.HSSF.UserModel.HSSFCellStyle)workBook.CreateCellStyle();
            reportHeaderStyle.SetFont(boldFont);
            reportHeaderStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CENTER;

            NPOI.HSSF.UserModel.HSSFCellStyle groupHeaderStyle = (NPOI.HSSF.UserModel.HSSFCellStyle)workBook.CreateCellStyle();
            groupHeaderStyle.SetFont(boldFont);

            NPOI.HSSF.UserModel.HSSFCellStyle columnHeaderStyle = (NPOI.HSSF.UserModel.HSSFCellStyle)workBook.CreateCellStyle();
            columnHeaderStyle.SetFont(boldFont);
            columnHeaderStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.THIN;

            NPOI.HSSF.UserModel.HSSFCellStyle detailStyle = (NPOI.HSSF.UserModel.HSSFCellStyle)workBook.CreateCellStyle();
            detailStyle.DataFormat = workBook.CreateDataFormat().GetFormat("#,##0.00;(#,##0.00)");

            NPOI.HSSF.UserModel.HSSFCellStyle subTotalStyle = (NPOI.HSSF.UserModel.HSSFCellStyle)workBook.CreateCellStyle();
            subTotalStyle.DataFormat = workBook.CreateDataFormat().GetFormat("#,##0.00;(#,##0.00)");
            subTotalStyle.BorderTop  = NPOI.SS.UserModel.BorderStyle.THIN;

            //rowPos = GenerateHeader(xlsDoc, workSheet, PeriodFrom, PeriodTo, rowPos);
            rowPos = GenerateHeader(workSheet, PeriodFrom, PeriodTo, rowPos, reportHeaderStyle, groupHeaderStyle);

            DataRow[] rows = dataSet.Tables["EmpInfo"].Select("", "Company, " + empInfoTable.Columns[hierarchyLevelGroupingFieldName].ColumnName + ", Employee Name, Alias");

            string    currentCompany             = string.Empty;
            string    currentHierarchyGroup      = string.Empty;
            string    currentEmployeeNo          = string.Empty;
            ushort    groupRowCount              = 0;
            Hashtable groupedTotalCostCenterHash = new Hashtable();
            ArrayList subTotalRowList            = new ArrayList();
            Hashtable paymentHashTable           = new Hashtable();
            Hashtable companyTotalHashTable      = new Hashtable();
            double    netPayment        = 0;
            double    companyNetPayment = 0;

            double employerContribution        = 0;
            double companyEmployerContribution = 0;

            ArrayList companyTotalRowNumList = new ArrayList();

            foreach (DataRow row in rows)
            {
                string tmpCompany    = row["Company"].ToString();
                string tmpHierarchy  = row[hierarchyLevelGroupingFieldName].ToString();
                string tmpEmployeeNo = row["Employee No."].ToString();


                if (!currentCompany.Equals(tmpCompany, StringComparison.CurrentCultureIgnoreCase) || rows[0] == row)
                {
                    if (rows[0] != row)
                    {
                        groupRowCount++;

                        rowPos = GenerateHierarchyTotal(workSheet, paymentTable, currentHierarchyGroup, currentEmployeeNo, paymentHashTable, netPayment, employerContribution, rowPos, detailStyle);

                        paymentHashTable = new Hashtable();


                        rowPos = GenerateCompanyTotal(workSheet, paymentTable, groupRowCount, companyEmployerContribution, rowPos, subTotalStyle);

                        companyTotalRowNumList.Add(rowPos);

                        companyTotalHashTable = new Hashtable();
                    }
                    groupRowCount = 0;

                    netPayment            = 0;
                    employerContribution  = 0;
                    currentHierarchyGroup = tmpHierarchy;
                    currentEmployeeNo     = tmpEmployeeNo;

                    companyNetPayment           = 0;
                    companyEmployerContribution = 0;
                    currentCompany = tmpCompany;
                    rowPos         = GenerateColumnHeader(workSheet, dataSet, currentCompany, hierarchyLevelGroupingFieldName, rowPos, groupHeaderStyle, columnHeaderStyle);
                }
                else if (!currentHierarchyGroup.Equals(tmpHierarchy, StringComparison.CurrentCultureIgnoreCase))
                {
                    groupRowCount++;

                    rowPos = GenerateHierarchyTotal(workSheet, paymentTable, currentHierarchyGroup, currentEmployeeNo, paymentHashTable, netPayment, employerContribution, rowPos, detailStyle);

                    paymentHashTable      = new Hashtable();
                    netPayment            = 0;
                    employerContribution  = 0;
                    currentHierarchyGroup = tmpHierarchy;
                    currentEmployeeNo     = tmpEmployeeNo;
                }
                DataRow[] paymentRows = paymentTable.Select("[EmpPayrollID]='" + row["EmpPayrollID"].ToString() + "'");

                foreach (DataRow paymentRow in paymentRows)
                {
                    foreach (DataColumn column in paymentTable.Columns)
                    {
                        if (column.ColumnName.Equals("EmpPayrollID"))
                        {
                            continue;
                        }
                        double amount             = 0;
                        double companyTotalAmount = 0;
                        if (paymentHashTable.ContainsKey(column.ColumnName))
                        {
                            amount = (double)paymentHashTable[column.ColumnName];
                        }
                        else
                        {
                            paymentHashTable.Add(column.ColumnName, amount);
                        }

                        if (companyTotalHashTable.ContainsKey(column.ColumnName))
                        {
                            companyTotalAmount = (double)companyTotalHashTable[column.ColumnName];
                        }
                        else
                        {
                            companyTotalHashTable.Add(column.ColumnName, companyTotalAmount);
                        }

                        if (!string.IsNullOrEmpty(paymentRow[column.ColumnName].ToString()))
                        {
                            amount             += (double)paymentRow[column.ColumnName];
                            companyTotalAmount += (double)paymentRow[column.ColumnName];
                            netPayment         += (double)paymentRow[column.ColumnName];
                            companyNetPayment  += (double)paymentRow[column.ColumnName];
                            paymentHashTable[column.ColumnName]      = amount;
                            companyTotalHashTable[column.ColumnName] = companyTotalAmount;
                        }
                    }
                }
                if (!row.IsNull("MCER"))
                {
                    double contribution = (double)row["MCER"];
                    employerContribution        += contribution;
                    companyEmployerContribution += contribution;
                }
                if (!row.IsNull("VCER"))
                {
                    double contribution = (double)row["VCER"];
                    employerContribution        += contribution;
                    companyEmployerContribution += contribution;
                }
                if (!row.IsNull("PFundER"))
                {
                    double contribution = (double)row["PFundER"];
                    employerContribution        += contribution;
                    companyEmployerContribution += contribution;
                }
            }

            if (paymentHashTable.Count > 0)
            {
                groupRowCount++;

                rowPos = GenerateHierarchyTotal(workSheet, paymentTable, currentHierarchyGroup, currentEmployeeNo, paymentHashTable, netPayment, employerContribution, rowPos, detailStyle);

                paymentHashTable     = new Hashtable();
                netPayment           = 0;
                employerContribution = 0;
            }

            if (groupRowCount > 0)
            {
                rowPos = GenerateCompanyTotal(workSheet, paymentTable, groupRowCount, companyEmployerContribution, rowPos, subTotalStyle);
                companyTotalRowNumList.Add(rowPos);
            }
            if (companyTotalRowNumList.Count > 0)
            {
                rowPos = GenerateTotal(workSheet, paymentTable, companyTotalRowNumList, rowPos, subTotalStyle);
            }
            //workSheet.Cells.Merge(1, 1, 1, paymentTable.Columns.Count + 2);
            //workSheet.Rows[1].GetCell(1).Font.Bold = true;
            //workSheet.Rows[1].GetCell(1).Font.Height = 300;
            //workSheet.Rows[1].GetCell(1).HorizontalAlignment = org.in2bits.MyXls.HorizontalAlignments.Centered;
            workSheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, paymentTable.Columns.Count + 1));


            // checking the number of column to resize for better user view
            int resizeColumn = paymentTable.Columns.Count + 1;

            if (intHierarchyLevelID == STAFF_LEVEL_ID)
            {
                resizeColumn++;
            }

            for (int i = 0; i <= resizeColumn; i++)
            {
                //org.in2bits.MyXls.ColumnInfo columnInfo = new org.in2bits.MyXls.ColumnInfo(xlsDoc, workSheet);
                //workSheet.AddColumnInfo(columnInfo);
                //columnInfo.ColumnIndexStart = 0;
                //columnInfo.ColumnIndexEnd = (ushort)(paymentTable.Columns.Count + 2);
                //columnInfo.Width = 15 * 254;
                //columnInfo.Collapsed = true;
                workSheet.SetColumnWidth(i, 15 * 254);
            }
        }