Esempio n. 1
0
        void ParseCell(ICell cell, string template, List<ExcelCell> listCell)
        {
            if (cell == null)
                return;
            template = template ?? GetTemplate(cell);
            if (string.IsNullOrEmpty(template))
                return;
            var cellTemplate = new ExcelCell() { RowIndex = cell.RowIndex, ColIndex = cell.ColumnIndex };
            var templates = template.Split(new char[] { ',', ',' });
            var cellstyle = cell.CellStyle;
            if (cellstyle == null)
                cellstyle = cell.Sheet.Workbook.CreateCellStyle();
            else
            {
                var cellstyle1 = cell.Sheet.Workbook.CreateCellStyle();
                cellstyle1.CloneStyleFrom(cellstyle);
                cellstyle = cellstyle1;
            }

            cellstyle.IsLocked = false;
            foreach (var rawtemp in templates)
            {
                var temp = rawtemp.Trim();
                if (temp.IndexOf(":") > 0)
                {
                    var pair = temp.Split(':');
                    switch (pair[0].ToLower())
                    {

                        case "islock":
                            var islock = pair[1].ToLower();
                            cellstyle.IsLocked = cellTemplate.IsLock = islock == "true" || islock == "1";
                            break;
                        case "ishide":
                            var ishide = pair[1].ToLower();
                            cellTemplate.IsHide = ishide == "true" || ishide == "1";
                            cell.Sheet.SetColumnHidden(cell.ColumnIndex, true);
                            break;
                        case "bind":
                            cellTemplate.Bind = pair[1];
                            break;
                        case "width":
                            cellTemplate.Width = Convert.ToInt32(pair[1]);
                            cell.Sheet.SetColumnWidth(cell.ColumnIndex, cellTemplate.Width * 32);
                            break;
                    }
                }
                else
                {
                    cellTemplate.Bind = temp;
                }
            }
            if (cellstyle.IsLocked)
            {
                cellstyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Grey25Percent.Index;
                cellstyle.FillPattern = FillPattern.ThickBackwardDiagonals;// NPOI.SS.UserModel.FillPatternType.THICK_BACKWARD_DIAG
                cellstyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.Grey25Percent.Index;
                //cellstyle.BorderLeft = NPOI.SS.UserModel.CellBorderType.NONE
                //cellstyle.BorderTop = NPOI.SS.UserModel.CellBorderType.NONE
                //cellstyle.BorderRight = NPOI.SS.UserModel.CellBorderType.NONE
                //cellstyle.BorderBottom = NPOI.SS.UserModel.CellBorderType.NONE

            }

            cell.CellStyle = cellstyle;
            listCell.Add(cellTemplate);
        }
Esempio n. 2
0
        void ParseCell(ICell cell, string template, List <ExcelCell> listCell)
        {
            if (cell == null)
            {
                return;
            }
            template = template ?? GetTemplate(cell);
            if (string.IsNullOrEmpty(template))
            {
                return;
            }
            var cellTemplate = new ExcelCell()
            {
                RowIndex = cell.RowIndex, ColIndex = cell.ColumnIndex
            };
            var templates = template.Split(new char[] { ',', ',' });
            var cellstyle = cell.CellStyle;

            if (cellstyle == null)
            {
                cellstyle = cell.Sheet.Workbook.CreateCellStyle();
            }
            else
            {
                var cellstyle1 = cell.Sheet.Workbook.CreateCellStyle();
                cellstyle1.CloneStyleFrom(cellstyle);
                cellstyle = cellstyle1;
            }

            cellstyle.IsLocked = false;
            foreach (var rawtemp in templates)
            {
                var temp = rawtemp.Trim();
                if (temp.IndexOf(":") > 0)
                {
                    var pair = temp.Split(':');
                    switch (pair[0].ToLower())
                    {
                    case "islock":
                        var islock = pair[1].ToLower();
                        cellstyle.IsLocked = cellTemplate.IsLock = islock == "true" || islock == "1";
                        break;

                    case "ishide":
                        var ishide = pair[1].ToLower();
                        cellTemplate.IsHide = ishide == "true" || ishide == "1";
                        cell.Sheet.SetColumnHidden(cell.ColumnIndex, true);
                        break;

                    case "bind":
                        cellTemplate.Bind = pair[1];
                        break;

                    case "width":
                        cellTemplate.Width = Convert.ToInt32(pair[1]);
                        cell.Sheet.SetColumnWidth(cell.ColumnIndex, cellTemplate.Width * 32);
                        break;
                    }
                }
                else
                {
                    cellTemplate.Bind = temp;
                }
            }
            if (cellstyle.IsLocked)
            {
                cellstyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Grey25Percent.Index;
                cellstyle.FillPattern         = FillPattern.ThickBackwardDiagonals;// NPOI.SS.UserModel.FillPatternType.THICK_BACKWARD_DIAG
                cellstyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.Grey25Percent.Index;
                //cellstyle.BorderLeft = NPOI.SS.UserModel.CellBorderType.NONE
                //cellstyle.BorderTop = NPOI.SS.UserModel.CellBorderType.NONE
                //cellstyle.BorderRight = NPOI.SS.UserModel.CellBorderType.NONE
                //cellstyle.BorderBottom = NPOI.SS.UserModel.CellBorderType.NONE
            }


            cell.CellStyle = cellstyle;
            listCell.Add(cellTemplate);
        }