NPOI 複製 工作表

複製原有的列寬,隱藏列,但沒實現形狀的複製。

    /// <summary>
    /// 複製表
    /// </summary>
    /// <param name="wb"></param>
    /// <param name="fromSheet"></param>
    /// <param name="toSheet"></param>
    /// <param name="copyValueFlag"></param>
    /// <param name="cellnum">預計總列數</param>
    public static void CopySheet(IWorkbook wb, ISheet fromSheet, ISheet toSheet, bool copyValueFlag,int cellnum=40)
    {
        //合併區域處理
        MergerRegion(fromSheet, toSheet);
       
        System.Collections.IEnumerator rows = fromSheet.GetRowEnumerator();
      
       
        //一行最後一個方格的編號(即總的列數)       
       
        for (int i =0; i< cellnum; i++)  // 設置隱藏的列  
        {
            if (fromSheet.IsColumnHidden(i))
            {              
                toSheet.SetColumnHidden(i, true);
            }
        }
        while (rows.MoveNext())
        {
            IRow row = null;
            if (wb is HSSFWorkbook)
                row = rows.Current as HSSFRow;
            else
                row = rows.Current as NPOI.XSSF.UserModel.XSSFRow;
            IRow newRow = toSheet.CreateRow(row.RowNum);
     
            CopyRow(wb, row, newRow, copyValueFlag, fromSheet, toSheet);
        }
    }  
    /// <summary>
    /// 複製行 帶設置列寬度
    /// </summary>
    /// <param name="wb"></param>
    /// <param name="fromRow"></param>
    /// <param name="toRow"></param>
    /// <param name="copyValueFlag"></param>
    /// <param name="fromSheet"></param>
    /// <param name="toSheet"></param>
    public static void CopyRow(IWorkbook wb, IRow fromRow, IRow toRow, bool copyValueFlag, ISheet fromSheet, ISheet toSheet)
    {
        System.Collections.IEnumerator cells = fromRow.GetEnumerator(); //.GetRowEnumerator();
        toRow.Height = fromRow.Height;
        while (cells.MoveNext())
        {
            ICell cell = null;
            if (wb is HSSFWorkbook) cell = cells.Current as HSSFCell;
            else cell = cells.Current as NPOI.XSSF.UserModel.XSSFCell;
            int column = cell.ColumnIndex; int row = cell.RowIndex; int coun = fromSheet.GetColumnWidth(row);//獲取列寬 
          
            toSheet.SetColumnWidth(row, coun);//設置列寬 
           
            ICell newCell = toRow.CreateCell(cell.ColumnIndex);

            CopyCell(wb, cell, newCell, copyValueFlag);
        }
    }

        /// <summary>
        /// 複製原有sheet的合併單元格到新創建的sheet
        /// </summary>
        /// <param name="fromSheet"></param>
        /// <param name="toSheet"></param>
        public static void MergerRegion(ISheet fromSheet, ISheet toSheet)
    {
        int sheetMergerCount = fromSheet.NumMergedRegions;
        for (int i = 0; i < sheetMergerCount; i++)
        {
            //Region mergedRegionAt = fromSheet.GetMergedRegion(i); //.MergedRegionAt(i);
            //CellRangeAddress[] cra = new CellRangeAddress[1];
            //cra[0] = fromSheet.GetMergedRegion(i);
            //Region[] rg = Region.ConvertCellRangesToRegions(cra);
            
            toSheet.AddMergedRegion(fromSheet.GetMergedRegion(i));
        }
    }
    /// <summary>
    /// 複製單元格
    /// </summary>
    /// <param name="wb"></param>
    /// <param name="srcCell"></param>
    /// <param name="distCell"></param>
    /// <param name="copyValueFlag"></param>
    public static void CopyCell(IWorkbook wb, ICell srcCell, ICell distCell, bool copyValueFlag)
    {
        ICellStyle newstyle = wb.CreateCellStyle();
        CopyCellStyle(wb, srcCell.CellStyle, newstyle);
        
        //樣式
        distCell.CellStyle = newstyle;
        //評論
        if (srcCell.CellComment != null)
        {
            distCell.CellComment = srcCell.CellComment;
        }
        // 不同數據類型處理
        CellType srcCellType = srcCell.CellType;
      
        distCell.SetCellType(srcCellType);
        if (copyValueFlag)
        {
            if (srcCellType == CellType.Numeric)
            {

                if (HSSFDateUtil.IsCellDateFormatted(srcCell))
                {
                    distCell.SetCellValue(srcCell.DateCellValue);
                }
                else
                {
                    distCell.SetCellValue(srcCell.NumericCellValue);
                }
            }
            else if (srcCellType == CellType.String)
            {
                distCell.SetCellValue(srcCell.RichStringCellValue);
            }
            else if (srcCellType == CellType.Blank)
            {
                // nothing21
            }
            else if (srcCellType == CellType.Boolean)
            {
                distCell.SetCellValue(srcCell.BooleanCellValue);
            }
            else if (srcCellType == CellType.Error)
            {
                distCell.SetCellErrorValue(srcCell.ErrorCellValue);
            }
            else if (srcCellType == CellType.Formula)
            {
                distCell.SetCellFormula(srcCell.CellFormula);
            }
            else
            { // nothing29
            }
        }
    }


    public static void CopyCellStyle(IWorkbook wb, ICellStyle fromStyle, ICellStyle toStyle)
    {
        toStyle.Alignment = fromStyle.Alignment;
        
        //邊框和邊框顏色
        toStyle.BorderBottom = fromStyle.BorderBottom;
        toStyle.BorderLeft = fromStyle.BorderLeft;
        toStyle.BorderRight = fromStyle.BorderRight;
        toStyle.BorderTop = fromStyle.BorderTop;
        toStyle.TopBorderColor = fromStyle.TopBorderColor;
        toStyle.BottomBorderColor = fromStyle.BottomBorderColor;
        toStyle.RightBorderColor = fromStyle.RightBorderColor;
        toStyle.LeftBorderColor = fromStyle.LeftBorderColor;       
        //背景和前景
        toStyle.FillBackgroundColor = fromStyle.FillBackgroundColor;
        toStyle.FillForegroundColor = fromStyle.FillForegroundColor;

        toStyle.DataFormat = fromStyle.DataFormat;
        toStyle.FillPattern = fromStyle.FillPattern;
        //toStyle.Hidden=fromStyle.Hidden;
        toStyle.IsHidden = fromStyle.IsHidden;
        toStyle.Indention = fromStyle.Indention;//首行縮進
        toStyle.IsLocked = fromStyle.IsLocked;
        toStyle.Rotation = fromStyle.Rotation;//旋轉
        toStyle.VerticalAlignment = fromStyle.VerticalAlignment;
        toStyle.WrapText = fromStyle.WrapText;
        toStyle.SetFont(fromStyle.GetFont(wb));
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章