Exemplo n.º 1
0
        /// <summary>
        /// 读取所有Excel文件内部信息
        /// </summary>
        private bool readAllExcelFilesInfo()
        {
            ExcelsInfoMap.Clear();
            bool issuccess = true;

            foreach (var excelfile in AllExcelFilesList)
            {
                // 如果配置有问题,直接退出
                if (issuccess == false)
                {
                    Console.WriteLine("配置有问题,请先修正配置后再导出!");
                    break;
                }
                FileStream       fs          = File.Open(excelfile, FileMode.Open, FileAccess.Read);
                IExcelDataReader excelreader = ExcelReaderFactory.CreateOpenXmlReader(fs);
                if (!excelreader.IsValid)
                {
                    Console.WriteLine(string.Format("Excel文件:{0}读取失败!", excelfile));
                    issuccess = false;
                    break;
                }
                else
                {
                    #if DEBUG
                    Console.WriteLine(string.Format("Excel文件.Name:{0}", excelreader.Name));
                    #endif
                    var dataset = excelreader.AsDataSet();
                    for (int index = 0, length = dataset.Tables.Count; index < length; index++)
                    {
                        if (isValideSheet(excelreader.Name))
                        {
                            if (hasSheetNameExist(excelreader.Name))
                            {
                                Console.WriteLine(string.Format("Excel:{0}有重名Sheet:{1}!", excelfile, excelreader.Name));
                                issuccess = false;
                                break;
                            }
                            else
                            {
                                var excelinfo = new ExcelInfo();
                                excelinfo.ExcelName = excelreader.Name;
                                int currentlinenumber = 1;
                                while (excelreader.Read())
                                {
                                    //读取每一行的数据
                                    string[] datas = new string[excelreader.FieldCount];
                                    for (int i = 0; i < excelreader.FieldCount; i++)
                                    {
                                        datas[i] = excelreader.GetString(i);
                                    }

                                    // 字段信息行
                                    if (currentlinenumber == FieldNameLineNumber)
                                    {
                                        excelinfo.FieldNames = datas;
                                    }
                                    // 字段注释信息行
                                    else if (currentlinenumber == FieldNotationLineNumber)
                                    {
                                        excelinfo.FieldNotations = datas;
                                    }
                                    // 字段类型信息行
                                    else if (currentlinenumber == FieldTypeLineNumber)
                                    {
                                        excelinfo.FieldTypes = datas;
                                    }
                                    // 字段分隔符信息行
                                    else if (currentlinenumber == FieldSpliterLineNumber)
                                    {
                                        excelinfo.FieldSpliters = datas;
                                    }
                                    // 字段占位符1信息行
                                    else if (currentlinenumber == FieldPlaceHolder1LineNumber)
                                    {
                                        excelinfo.FieldPlaceholder1s = datas;
                                    }
                                    // 字段占位符2信息行
                                    else if (currentlinenumber == FieldPlaceHolder2LineNumber)
                                    {
                                        excelinfo.FieldPlaceholder2s = datas;
                                    }
                                    else if (currentlinenumber >= DataLineNumber)
                                    {
                                        // 存储数据之前,检查一次各字段名字,字段信息等配置是否正确
                                        if (currentlinenumber == DataLineNumber)
                                        {
                                            if (hasInvalideName(excelinfo.FieldNames, excelinfo.FieldTypes))
                                            {
                                                Console.WriteLine(string.Format("Excel Table:{0}", excelreader.Name));
                                                issuccess = false;
                                                break;
                                            }
                                            if (hasInvalideType(excelinfo.FieldTypes))
                                            {
                                                Console.WriteLine(string.Format("Excel Table:{0}", excelreader.Name));
                                                issuccess = false;
                                                break;
                                            }
                                            if (hasInvalideSpliter(excelinfo.FieldSpliters))
                                            {
                                                Console.WriteLine(string.Format("Excel Table:{0}", excelreader.Name));
                                                issuccess = false;
                                                break;
                                            }
                                        }

                                        // 记录每一行所有数据的字段名,字段类型,字段数据
                                        ExcelData[] exceldatas = new ExcelData[datas.Length];
                                        for (int m = 0; m < datas.Length; m++)
                                        {
                                            ExcelData cd = new ExcelData();
                                            cd.Type       = excelinfo.FieldTypes[m];
                                            cd.Name       = excelinfo.FieldNames[m];
                                            cd.Spliter    = excelinfo.FieldSpliters[m];
                                            cd.Data       = datas[m];
                                            exceldatas[m] = cd;
                                        }

                                        if (issuccess == false)
                                        {
                                            break;
                                        }
                                        else
                                        {
                                            excelinfo.addData(exceldatas);
                                        }
                                    }
                                    else
                                    {
                                        Console.WriteLine(string.Format("无效的行号:{0}", currentlinenumber));
                                        issuccess = false;
                                        break;
                                    }
                                    currentlinenumber++;
                                }

                                // 检查是否有重复的id
                                if (!isIdValide(excelinfo))
                                {
                                    Console.WriteLine($"Excel Sheet:{excelinfo.ExcelName}的配置有问题!");
                                    issuccess = false;
                                    break;
                                }

                                ExcelsInfoMap.Add(excelreader.Name, excelinfo);
                            }
                        }
                        excelreader.NextResult();
                    }
                }
            }

            if (!issuccess)
            {
                clearExcelInfo();
                return(false);
            }
            else
            {
#if DEBUG
                //打印表格数据信息
                //foreach(var excelinfo in ExcelsInfoMap)
                //{
                //    excelinfo.Value.printOutAllExcelInfo();
                //}
#endif
                return(true);
            }
        }