您的位置:首页 > 其它

GridView匯出Excel(NPOI篇)[3]

2013-10-24 14:27 225 查看
        /// <summary>

        /// 轉出GridView的Header

        /// </summary>

        /// <param name="sheet">HSSFSheet object</param>

        private void RenderHeaderRow(ISheet sheet)

        {

            sheet.CreateFreezePane(0, 1);

            ICellStyle style = GetTitleStyle();

            IRow headerRow = sheet.CreateRow(0);

            int cellCount = 0;

            GridViewRow row = _gvData.HeaderRow;

            for (int i = 0, iCount = row.Cells.Count; i < iCount; i++)

            {

                if (EnableShowHiddenField || (_gvData.Columns[i].Visible && row.Cells[i].Visible))

                {

                    ICell cell = headerRow.CreateCell(cellCount);

                    cell.CellStyle = style;

                    

                    cell.SetCellValue(HttpUtility.HtmlDecode(row.Cells[i].Text.Trim()));

                    cellCount++;

                }

            }

        }

        /// <summary>

        /// 轉出GridView的資料

        /// </summary>

        /// <param name="sheet">HSSFSheet object</param>

        private void RenderDataRow(ISheet sheet)

        {

            GridViewRowCollection rows = _gvData.Rows;

            int columnsCount = _gvData.Columns.Count;

            string[] newLine = new string[] { Environment.NewLine };

            for (int rowIdx = 0, iCount = rows.Count; rowIdx < iCount; rowIdx++)

            {  

                IRow row = sheet.CreateRow(rowIdx + 1);

                int iMaxRowHight = 1;

                int cellCount = 0;

                Color rowColor = GetRowColor(rows[rowIdx]);

                for (int colIdx = 0; colIdx < columnsCount; colIdx++)

                {

                    if (EnableShowHiddenField || (_gvData.Columns[colIdx].Visible && rows[rowIdx].Cells[colIdx].Visible))

                    {

                        TableCell dataCell = rows[rowIdx].Cells[colIdx];

                        ICell cell = row.CreateCell(cellCount);

                        Color cellColor = GetTableCellColor(_gvData.Columns[colIdx], dataCell);

                        string cellValue =

                            HttpUtility.HtmlDecode(dataCell.Text.Trim()).Replace("<br />", Environment.NewLine);

                        SetCellStyle(cell, rowColor, cellColor);

                        SetCellValue(colIdx, cell, cellValue);

                        int newLineCount = cellValue.Split(newLine, StringSplitOptions.None).Length;

                        if (newLineCount > iMaxRowHight)

                        {

                            iMaxRowHight = newLineCount;

                        }

                        if (rows[rowIdx].Cells[colIdx].ColumnSpan > 1)

                        {

                            sheet.AddMergedRegion(new CellRangeAddress(rowIdx + 1, rowIdx + 1, (cellCount + 1), (cellCount + rows[rowIdx].Cells[colIdx].ColumnSpan)));

                        }

                        if (rows[rowIdx].Cells[colIdx].RowSpan > 1)

                        {

                            sheet.AddMergedRegion(new CellRangeAddress(rowIdx + 1, (rowIdx + rows[rowIdx].Cells[colIdx].RowSpan), cellCount, cellCount));

                        }

                    }

                    if (EnableShowHiddenField || (_gvData.Columns[colIdx].Visible && rows[rowIdx].Cells[colIdx].Visible))

                    {

                        cellCount++;

                    }

                }

                row.HeightInPoints = (iMaxRowHight + 1) * sheet.DefaultRowHeight / 20;

            }

        }

        /// <summary>

        /// 轉出GridView的Footer

        /// </summary>

        /// <param name="sheet">HSSFSheet object</param>

        private void RenderFooterRow(ISheet sheet)

        {

            if (_gvData.FooterRow.Visible && _gvData.FooterRow != null)

            {

                ICellStyle style = GetFooterStyle(_gvData.FooterRow.BackColor);

                sheet.CreateFreezePane(0, 1);

                IRow footerRow = sheet.CreateRow(_gvData.Rows.Count + 1);

                GridViewRow headerRow = _gvData.HeaderRow;

                GridViewRow row = _gvData.FooterRow;

                int cellCount = 0;

                for (int i = 0, iCount = row.Cells.Count; i < iCount; i++)

                {

                    if (EnableShowHiddenField || headerRow.Cells[i].Visible)

                    {

                        ICell cell = footerRow.CreateCell(cellCount);

                        if (row.Cells[i].ForeColor != Color.Empty)

                        {

                            style.SetFont(GetCellFontColor(row.Cells[i].ForeColor));

                        }

                        cell.CellStyle = style;

                        cell.SetCellValue(HttpUtility.HtmlDecode(row.Cells[i].Text.Trim()));

                        if (row.Cells[i].ColumnSpan > 1)

                        {

                            int rowIdx = _gvData.Rows.Count + 1;

                            sheet.AddMergedRegion(new CellRangeAddress(rowIdx, rowIdx, cellCount, cellCount + row.Cells[i].ColumnSpan - 1));

                        }

                        cellCount++;

                    }

                }

            }

        }

        /// <summary>

        /// 設定Cell的文字為元件的Text屬性

        /// </summary>

        private void RenderDataToText()

        {

            foreach (GridViewRow row in _gvData.Rows)

            {

                foreach (TableCell cell in row.Cells)

                {

                    if (cell.HasControls())

                    {

                        foreach (Control ctl in cell.Controls)

                        {

                            string value = "";

                            if (ctl.GetType().GetProperty("SelectedItem") != null)

                            {

                                value =

                                    ctl.GetType().GetProperty("SelectedItem").GetValue(ctl, null).ToString();

                            }

                            else if (ctl.GetType().GetProperty("Text") != null)

                            {

                                value =

                                    ctl.GetType().GetProperty("Text").GetValue(ctl, null).ToString();

                            }

                            if (value.Trim().Length > 0)

                            {

                                cell.Text = TrimHref(value.Trim());

                                break;

                            }

                        }

                    }

                    else

                    {

                        cell.Text = TrimHref(cell.Text);

                    }

                }

            }

        }

        /// <summary>

        /// 取得資料列Title樣式

        /// </summary>

        /// <returns>資料列Title樣式</returns>

        private ICellStyle GetTitleStyle()

        {

            // 先設定顏色

            HSSFPalette palette = _workBook.GetCustomPalette();

            palette.SetColorAtIndex(HSSFColor.LIGHT_GREEN.index,

                (byte)80, (byte)123, (byte)190);

            ICellStyle style = _workBook.CreateCellStyle();

            style.Alignment = HorizontalAlignment.CENTER;

            style.VerticalAlignment = VerticalAlignment.CENTER;

            style.BorderBottom = NPOI.SS.UserModel.BorderStyle.DOUBLE;

            style.FillPattern = FillPatternType.SOLID_FOREGROUND;

            style.FillForegroundColor = HSSFColor.LIGHT_GREEN.index;

            style.BorderBottom = NPOI.SS.UserModel.BorderStyle.THIN;

            style.BorderLeft = NPOI.SS.UserModel.BorderStyle.THIN;

            style.BorderRight = NPOI.SS.UserModel.BorderStyle.THIN;

            style.BorderTop = NPOI.SS.UserModel.BorderStyle.THIN;

            IFont font = _workBook.CreateFont();

            font.Color = HSSFColor.WHITE.index;

            style.SetFont(font);

            

            return style;

        }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: