Beispiel #1
0
        public IDictionary ReadDictionary(ISheet sheet, Schema schema, string keyField, int dataStart, int colStart, int colEnd, List <string> header, bool removeKeyInElement = false, int dataEnd = -1)
        {
            if (header == null || header.Count == 0)
            {
                header = ReadHelper.GetHeader(sheet, 0, colStart, colEnd);
            }

            List <Field> headerFields = ReadHelper.PrepareHeaderFields(header, schema);

            //如果没指定key,则默认使用第一个
            if (string.IsNullOrEmpty(keyField))
            {
                keyField = header[0];
            }

            Dictionary <object, object> dict = new Dictionary <object, object>();
            int l = dataEnd <= 0 ? sheet.LastRowNum : (dataEnd < sheet.LastRowNum ? dataEnd : sheet.LastRowNum);

            for (int i = sheet.FirstRowNum + dataStart; i <= l; ++i)
            {
                Dictionary <string, object> record = ReadRowData(sheet.GetRow(i), headerFields, colStart, colEnd);
                object key = record[keyField];
                dict[key] = record;
                if (removeKeyInElement)
                {
                    record.Remove(keyField);
                }
            }
            return(dict);
        }
Beispiel #2
0
        public List <object> ReadList(ISheet sheet, Schema schema, int dataStart, int dataEnd, int colStart, int colEnd, List <string> header)
        {
            if (header == null || header.Count == 0)
            {
                header = ReadHelper.GetHeader(sheet, 0, colStart, colEnd);
            }

            List <Field> headerFields = ReadHelper.PrepareHeaderFields(header, schema);

            List <object> list = new List <object>();
            int           l    = dataEnd <= 0 ? sheet.LastRowNum : (dataEnd < sheet.LastRowNum ? dataEnd : sheet.LastRowNum);

            for (int i = sheet.FirstRowNum + dataStart; i <= l; ++i)
            {
                Dictionary <string, object> record = ReadRowData(sheet.GetRow(i), headerFields, colStart, colEnd);
                list.Add(record);
            }
            return(list);
        }