示例#1
0
 public ImportFileFormat AddSheet(SheetFormat sheet)
 {
     if (sheet == null)
     {
         throw new ArgumentNullException("sheet格式定义对象不能为空");
     }
     this.Sheets.Add(sheet);
     return(this);
 }
示例#2
0
        protected virtual SheetImportResult ImportSheet(ISheet sheet, SheetFormat format)
        {
            SheetImportResult result = new SheetImportResult();

            result.Name  = format.Name;
            result.Index = format.Index;
            int           dataColumnEnd;
            List <string> headerData = this.GetHeaderData(sheet, format, out dataColumnEnd);
            int           rowIndex   = format.DataRowStart;
            bool          isEnd      = false;
            IRow          dataRow    = sheet.GetRow(rowIndex);

            while (!isEnd && dataRow != null)
            {
                List <object>     rowData = this.GetRowData(dataRow, format, rowIndex, dataColumnEnd);
                ImportRowEventArg arg     = new ImportRowEventArg();
                arg.RowData    = rowData;
                arg.HeaderData = headerData;
                arg.Format     = format;
                arg.RowIndex   = rowIndex;
                if (this.OnDataRowImporting != null)
                {
                    this.OnDataRowImporting(this, arg);
                }
                if (arg.IsSkip)
                {
                    result.IncreaseSkiped();
                }
                else if (!string.IsNullOrEmpty(arg.Error))
                {
                    result.AddError(string.Format("第{0}处理失败:{1}", rowIndex, arg.Error));
                }
                else if (arg.IsSuccess)
                {
                    result.IncreaseSuccess();
                }
                isEnd = arg.IsEnd;
                if (!isEnd && format.AllEmptyIsEnd && rowData == null)
                {
                    isEnd = true;
                }
                if (!isEnd)
                {
                    rowIndex++;
                }
                dataRow = sheet.GetRow(rowIndex);
            }
            return(result);
        }
示例#3
0
        protected virtual List <object> GetRowData(IRow row, SheetFormat format, int rowIndex, int dataColumnEnd)
        {
            List <object> data    = new List <object>();
            bool          allNull = true;

            for (int i = format.DataColumnStart; i <= dataColumnEnd; i++)
            {
                ICell  cell = row.GetCell(i);
                object one  = this.GetCellValue(cell);
                if (one != null)
                {
                    allNull = false;
                }
                data.Add(one);
            }
            if (!allNull)
            {
                return(data);
            }
            return(null);
        }
示例#4
0
        protected virtual List <string> GetHeaderData(ISheet sheet, SheetFormat format, out int dataColumnEnd)
        {
            dataColumnEnd = 0;
            List <string> rtn = new List <string>();
            IRow          row = sheet.GetRow(format.HeaderRowIndex);

            if (row == null)
            {
                throw new ApplicationException("无法找到header行");
            }
            bool hasEnd = format.DataColumnEnd.HasValue;

            if (hasEnd)
            {
                dataColumnEnd = format.DataColumnEnd.Value;
                for (int cellNum = format.DataColumnStart; cellNum <= format.DataColumnEnd; cellNum++)
                {
                    ICell  cell = row.GetCell(cellNum);
                    string data = this.GetStringFromCell(cell);
                    rtn.Add(data);
                }
            }
            else
            {
                int    cellNum2 = format.DataColumnStart;
                ICell  cell2    = row.GetCell(cellNum2);
                string data2    = this.GetStringFromCell(cell2);
                while (cell2 != null && !string.IsNullOrEmpty(data2))
                {
                    rtn.Add(data2);
                    dataColumnEnd = cellNum2;
                    cellNum2++;
                    cell2 = row.GetCell(cellNum2);
                    data2 = this.GetStringFromCell(cell2);
                }
            }
            return(rtn);
        }