예제 #1
0
        static void Main(string[] args)
        {
            string app_location = AppPathHelper.GetAppPath();
            //Console.WriteLine(app_location);

            // current pdf file name
            string pdf_file = "\\Excel\\stocklist_netto-3.xlsx";

            ExtractExcelNettoExtractor extractor = new ExtractExcelNettoExtractor();

            ReadExcelHelper.ReadExcel(app_location + pdf_file, extractor);

            //Console.WriteLine(app_location + pdf_file);

            Console.WriteLine("Program Terminated.");
            Console.ReadKey();
        }
예제 #2
0
        static void Main(string[] args)
        {
            string app_location = AppPathHelper.GetAppPath();
            //Console.WriteLine(app_location);

            // current pdf file name
            string pdf_file = "\\Excel\\ATS Cultured Price List 2016-1.xls";

            ExcelATSExtractor extractor = new ExcelATSExtractor();

            ReadExcelHelper.ReadExcel(app_location + pdf_file, extractor);

            //Console.WriteLine(app_location + pdf_file);

            Console.WriteLine("Program Terminated.");
            Console.ReadKey();
        }
예제 #3
0
    /// <summary>
    /// 解析所有配置表
    /// </summary>
    /// <param name="ExportTables"></param>
    public static void AnalyzeAllTable(Dictionary <string, string> ExportTables)
    {
        //检测Excel是否打开
        ReadExcelHelper.GetFileState(ExcelFolder.ExportTables);

        AppLog.Log("\n开始解析Excel文件:");
        Stopwatch stopwatch = new Stopwatch();//计算运行时间

        foreach (KeyValuePair <string, string> kvp in ExportTables)
        {
            AppLog.Log(string.Format("解析表格\"{0}\":", kvp.Key), ConsoleColor.Green);
            string excelName = Path.GetFileNameWithoutExtension(kvp.Key);

            /*
             * if (AppValues.IsMoreLanguage==true)//指定是多语言情况下才处理
             * {
             *  if (AppValues.NeedLanguage == null)
             *  {
             *      bool Current_Language = true;
             *      if(AppValues.OtherLanguage!=null)
             *      {
             *          foreach (string str in AppValues.OtherLanguage)
             *          {
             *              if (excelName.EndsWith(str))
             *              {
             *                  Current_Language = false;
             *                  break;
             *              }
             *          }
             *          if (Current_Language == false)
             *          {
             *              continue;
             *          }
             *      }
             *  }
             *  else//
             *  {
             *      bool Current_Language = true;
             *      if (AppValues.OtherLanguage != null)
             *      {
             *          foreach (string str in AppValues.OtherLanguage)
             *          {
             *              if (excelName.EndsWith(str))
             *              {
             *                  Current_Language = false;
             *                  break;
             *              }
             *          }
             *      }
             *
             *      if (Current_Language == false)//如果是属于多语言
             *      {
             *          if (excelName.EndsWith(AppValues.NeedLanguage))//是指定的多语言
             *          {
             *              excelName.Substring(0, excelName.Length - AppValues.NeedLanguage.Length);
             *              Current_Language = true;
             *              break;
             *          }
             *      }
             *
             *      if (Current_Language == false)//如果不是默认语言或指定的多语言则忽略,不解析
             *      {
             *          continue;
             *      }
             *  }
             * }
             */
            stopwatch.Reset();//时间重置
            stopwatch.Start();

            string errorString = null;

            string  tableName = null;
            DataSet ds;
            if (AppValues.App_Config_ReadExcelType == "ExcelDataReader")
            {
                ds = ReadExcelHelper.ReadXlsxFileForExcelDataReader(kvp.Value, excelName, ref tableName, out errorString);
            }
            else if (AppValues.App_Config_ReadExcelType == "OleDb")
            {
                ds = ReadExcelHelper.ReadXlsxFileForOleDb(kvp.Value, excelName, ref tableName, out errorString);
            }
            else
            {
                ds = ReadExcelHelper.ReadXlsxFileForOleDb(kvp.Value, excelName, ref tableName, out errorString);
            }

            if (string.IsNullOrEmpty(errorString))
            {
                int tbNum = 0;
                foreach (DataTable tb in ds.Tables)
                {
                    if (tb.TableName != ExcelTableSetting.ExcelConfigSheetName)
                    {
                        TableInfo tableInfo = AnalyzeTable(tb, kvp.Value, out errorString);
                        if (errorString != null)
                        {
                            AppLog.LogErrorAndExit(string.Format("错误:解析{0}失败\n{1}", kvp.Value, errorString));
                        }
                        else
                        {
                            tbNum++;
                            //tableInfo.ExcelFilePath = kvp.Key;
                            //tableInfo.ExcelName = excelName;
                            //tableInfo.TableName = tableName;
                            // 如果有表格配置进行解析
                            if (ds.Tables[ExcelTableSetting.ExcelConfigSheetName] != null)
                            {
                                // AppLog.Log("    解析config配置...", ConsoleColor.Green);
                                tableInfo.TableConfigData  = GetTableConfigData(ds.Tables[ExcelTableSetting.ExcelConfigSheetName], out errorString);
                                tableInfo.TableConfigData2 = GetTableConfigOfFirstColumn(ds.Tables[ExcelTableSetting.ExcelConfigSheetName], out errorString);
                                if (!string.IsNullOrEmpty(errorString))
                                {
                                    AppLog.LogErrorAndExit(string.Format("错误:解析表格{0}的配置失败\n{1}", kvp.Key, errorString));
                                }
                                else
                                {
                                    tableInfo.TableConfig = ds.Tables[ExcelTableSetting.ExcelConfigSheetName];
                                }
                            }
                            if (!AppValues.TableInfoList.ContainsKey(tableName))
                            {
                                AppValues.TableInfoList.Add(tableName, new List <TableInfo> {
                                    tableInfo
                                });
                            }
                            else
                            {
                                AppValues.TableInfoList[tableName].Add(tableInfo);
                            }
                        }
                    }
                }
                TableInfo tableInfo2;
                if (tbNum == 0)
                {
                    AppLog.LogErrorAndExit(string.Format("错误:读取{0}失败,没有找到有效数据表\n", kvp.Value));
                }
                else if (tbNum > 1)
                {
                    tableInfo2 = TableInfo.Merge(tableName, AppValues.TableInfoList[tableName], out errorString);
                    // 唯一性检查
                    //FieldCheckRule uniqueCheckRule = new FieldCheckRule();
                    //uniqueCheckRule.CheckType = TableCheckType.Unique;
                    //uniqueCheckRule.CheckRuleString = "unique";
                    //TableCheckHelper.CheckUnique(tableInfo2.GetKeyColumnFieldInfo(), uniqueCheckRule, out errorString);
                    //if (errorString != null)
                    //{

                    //    errorString = _GetTableAnalyzeErrorString(tableName, "", 0) + "主键列存在重复错误\n" + errorString;
                    //    AppLog.LogErrorAndExit(string.Format("错误:合并{0}失败\n{1}", tableName, errorString));
                    //}

                    AppValues.TableInfo.Add(tableName, tableInfo2);
                }
                else
                {
                    if (!AppValues.TableInfo.ContainsKey(tableName))
                    {
                        AppValues.TableInfo.Add(tableName, AppValues.TableInfoList[tableName][0]);
                    }
                    else
                    {
                        AppLog.LogErrorAndExit(string.Format("错误:存在多个以{0}为名的表格,请检查配置\n", tableName));
                        // if ( AppValues.App_Config_MergeTable==false)
                        //     AppLog.LogErrorAndExit(string.Format("错误:存在多个以{0}为名的表格,请检查配置\n", tableName));
                        //else
                        // {
                        //     AppValues.TableInfo[tableName] = TableInfo.Merge(tableName,AppValues.TableInfoList[tableName],out errorString);

                        // }
                    }
                }
                //检查服务端字段名是否重复
                List <string>    sf = new List <string>();
                List <FieldInfo> allServerFieldInfo = AppValues.TableInfo[tableName].GetAllServerFieldInfo();
                foreach (FieldInfo f in allServerFieldInfo)
                {
                    if (sf.Contains(f.DatabaseFieldName))
                    {
                        AppLog.LogErrorAndExit(string.Format("错误:存在多个以{0}为名的服务端字段,请检查配置\n", f.DatabaseFieldName));
                    }
                    else
                    {
                        sf.Add(f.DatabaseFieldName);
                    }
                }

                stopwatch.Stop();
                AppLog.Log(string.Format("成功,耗时:{0}毫秒", stopwatch.ElapsedMilliseconds));
            }
            else
            {
                AppLog.LogErrorAndExit(string.Format("错误:读取{0}失败\n{1}", kvp.Value, errorString));
            }

            //导出txt用
            AppValues.ExcelDataSet.Add(kvp.Value, ds);
        }
        AppLog.Log("\n所有配置表解析完毕!!!\n");
    }