Npoi复制行

孤者浪人 提交于 2020-04-05 23:28:15
 private void CopyRange(HSSFWorkbook myHSSFWorkBook, int fromRowIndex, int fromColIndex, int toRowIndex, int toColIndex, bool onlyData, bool copyComment)
        {
            HSSFRow sourceRow 
= myHSSFWorkBook.GetSheetAt(myHSSFWorkBook.ActiveSheetIndex).GetRow(fromRowIndex);
            HSSFCell sourceCell 
= sourceRow.GetCell(fromColIndex);
            
if (sourceRow != null && sourceCell != null)
            {
                HSSFRow changingRow 
= null;
                HSSFCell changingCell 
= null;
                changingRow 
= myHSSFWorkBook.GetSheetAt(myHSSFWorkBook.ActiveSheetIndex).GetRow(toRowIndex);
                
if (changingRow == null)
                    changingRow 
= myHSSFWorkBook.GetSheetAt(myHSSFWorkBook.ActiveSheetIndex).CreateRow(toRowIndex);
                changingCell 
= changingRow.GetCell(toColIndex);
                
if (changingCell == null)
                    changingCell 
= changingRow.CreateCell(toColIndex);

                
if (onlyData)//仅数据
                {
                    
//对单元格的值赋值
                    changingCell.SetCellValue(sourceCell.StringCellValue);
                }
                
else         //非仅数据
                {
                    
//单元格的编码
                    changingCell.Encoding = sourceCell.Encoding;
                    
//单元格的格式
                    changingCell.CellStyle = sourceCell.CellStyle;
                    
//单元格的公式
                    if (sourceCell.CellFormula == "")
                        changingCell.SetCellValue(sourceCell.StringCellValue);
                    
else
                        changingCell.SetCellFormula(sourceCell.CellFormula);
                    
//对单元格的批注赋值
                    if (copyComment)
                    {
                        
if (sourceCell.CellComment != null)
                        {
                            HSSFPatriarch patr 
= myHSSFWorkBook.GetSheetAt(myHSSFWorkBook.ActiveSheetIndex).CreateDrawingPatriarch();
                            HSSFComment comment 
= patr.CreateComment(new HSSFClientAnchor(0000, toColIndex, toRowIndex, toColIndex + 1, toRowIndex + 1));

                            comment.String 
= new HSSFRichTextString(sourceCell.CellComment.String.ToString());
                            comment.Author 
= sourceCell.CellComment.Author;

                            changingCell.CellComment 
= comment;
                        }
                    }
                }
            }
        }


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!