欢迎使用CSDN-markdown编辑器

来源:互联网 发布:用手机淘宝注册账号 编辑:程序博客网 时间:2024/06/10 16:08

代码块

/*====================== 复制execl到指定行====================*/using NPOI.HSSF.UserModel;using NPOI.SS.UserModel;using NPOI.SS.Util;using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace UavSystem.UavModel.Export{    public class CopyExcel    {    //参数 1.要复制的开始行2.要复制的结束行3.复制到新execl第几行4.要复制的execl的sheet        public void copyRows(int startRow, int endRow, int pPosition, ISheet sheet)        {            int pStartRow = startRow - 1;            int pEndRow = endRow - 1;            int targetRowFrom;            int targetRowTo;            int columnCount;            CellRangeAddress region = null;            int i;            int j;            if (pStartRow == -1 || pEndRow == -1)            {                return;            }            // 拷贝合并的单元格              for (i = 0; i < sheet.NumMergedRegions; i++)            {                region = sheet.GetMergedRegion(i); //sheet.getMergedRegion(i);                  if ((region.FirstRow >= pStartRow) && (region.LastRow <= pEndRow))                {                    targetRowFrom = region.FirstRow - pStartRow + pPosition;                    targetRowTo = region.LastRow - pStartRow + pPosition;                    CellRangeAddress newRegion = region.Copy();                    newRegion.FirstRow = targetRowFrom;                    newRegion.FirstColumn = region.FirstColumn;                    newRegion.LastRow = targetRowTo;                    newRegion.LastColumn = region.LastColumn;                    sheet.AddMergedRegion(newRegion);                }            }            // 设置列宽              for (i = pStartRow; i <= pEndRow; i++)            {                IRow sourceRow = sheet.GetRow(i);                columnCount = sourceRow.LastCellNum;                if (sourceRow != null)                {                    IRow newRow = sheet.CreateRow(pPosition - pStartRow + i);                    newRow.Height = sourceRow.Height;                    for (j = 0; j < columnCount; j++)                    {                        ICell templateCell = sourceRow.GetCell(j);                        if (templateCell != null)                        {                            ICell newCell = newRow.CreateCell(j);                            copyCell(templateCell, newCell);                        }                    }                }            }        }        private void copyCell(ICell srcCell, ICell distCell)        {            distCell.CellStyle = srcCell.CellStyle;            if (srcCell.CellComment != null)            {                distCell.CellComment = srcCell.CellComment;            }            // 不同数据类型处理            CellType srcCellType = srcCell.CellType;            distCell.SetCellType(srcCellType);            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            {             }        }    }}

原创粉丝点击