Exemple #1
0
        public static Dictionary <string, string> getXLSDGVDic()
        {
            LogFile lf = new LogFile();
            Dictionary <string, string> tmpDic = new Dictionary <string, string>();

            if (Program.keyValueStr.StartsWith("{") && Program.keyValueStr.EndsWith("}"))
            {
                string tmpStr = Program.keyValueStr.Substring(1);
                tmpStr = tmpStr.Remove(tmpStr.Length - 1);
                string[]        tmpStrs = tmpStr.Split(',');
                Regex           rgx     = new Regex(@"""([\w]+)""-""([\w]+)""");
                MatchCollection mc      = rgx.Matches(tmpStr);
                if (mc.Count == Program.XLSColsCount)
                {
                    //    Console.Write(mc.Count);
                    //    Console.Write(mc[0].Groups.Count);
                    for (int i = 0; i < tmpStrs.Length; i++)
                    {
                        tmpDic.Add(mc[i].Groups[1].ToString(), mc[i].Groups[2].ToString());
                    }
                }
                else
                {
                    lf.write("声明的XLS列数与对照字符串列数不同!");
                }
            }
            else
            {
                lf.write("XLS转程序DGV显示的列名键值对左右{}不匹配!");
            }



            return(tmpDic);
        }
Exemple #2
0
        public static DataTable getImportXLS()
        {
            DataTable dT      = new DataTable();
            string    strConn = "";
            LogFile   lf      = new LogFile();

            if (!File.Exists(Program.ImportXLSPath))
            {
                lf.write("导入的EXCEL文件不存在");
                return(null);
            }
            else
            {
                FileInfo tmpFI = new FileInfo(Program.ImportXLSPath);
                switch (tmpFI.Extension)
                {
                case ".xls":
                    strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Program.ImportXLSPath + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'";
                    break;

                case ".xlsx":
                    strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Program.ImportXLSPath + ";Extended Properties='Excel 12.0;HDR=Yes;IMEX=1;'";
                    break;

                default:
                    strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Program.ImportXLSPath + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'";
                    break;
                }
                OleDbConnection cnnxls = new OleDbConnection(strConn);
                //读取Excel里面有 表Sheet1
                System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
                sw.Start();
                try
                {
                    cnnxls.Open();
                    DataTable tmpDT = cnnxls.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                    foreach (DataRow row in tmpDT.Rows)
                    {
                        //遍历弹出各Sheet的名称
                        Console.Write(row["TABLE_NAME"].ToString());
                    }
                    OleDbCommand     odc = new OleDbCommand("select * from [data$]", cnnxls);
                    OleDbDataAdapter oda = new OleDbDataAdapter(odc);
                    oda.Fill(dT);
                    sw.Stop();
                    lf.write("读取文件:" + Program.ImportXLSPath + " 耗时:" + sw.ElapsedMilliseconds + "毫秒");
                    lf.write("表格共有" + dT.Rows.Count + "行," + dT.Columns.Count + "列");
                    //将Excel里面有表内容装载到内存表中!
                }
                catch (Exception ex)
                {
                    lf.write("通过SQL方式读取EXCEL错误:" + ex.Message);
                }

                return(dT);
            }
        }
Exemple #3
0
        public static void getConfig()
        {
            LogFile lf = new LogFile();

            //  Program.ConfigFilePath = Environment.CurrentDirectory + "\\config.txt";

            if (!File.Exists(Program.ConfigFilePath))
            {
                Program.ConfigFileReadError = "配置文件不存在";
            }
            else
            {
                string[] configStr = File.ReadAllLines(Program.ConfigFilePath, Encoding.GetEncoding("GB2312"));
                foreach (string tmpstr in configStr)
                {
                    if (tmpstr.StartsWith("DataBasePath="))
                    {
                        Program.ServerPath = getAttr(tmpstr);
                    }
                    else
                    if (tmpstr.StartsWith("DataBasePort="))
                    {
                        Program.ServerPort = getAttr(tmpstr);
                    }
                    else
                    if (tmpstr.StartsWith("DataBaseUser="******"DateBasePWD="))
                    {
                        Program.Pwd = getAttr(tmpstr);
                    }
                    else
                    if (tmpstr.StartsWith("DataBaseName="))
                    {
                        Program.DBName = getAttr(tmpstr);
                    }
                    else
                    if (tmpstr.StartsWith("LogFilePath="))
                    {
                        Program.LogFilePath = getAttr(tmpstr);
                    }
                    else
                    if (tmpstr.StartsWith("XLSSHSToDGVCOL="))
                    {
                        Program.keyValueStr = getAttr(tmpstr);
                    }
                    else
                    if (tmpstr.StartsWith("XLSColsCount="))
                    {
                        Int16 parseR = 0;
                        if (Int16.TryParse(getAttr(tmpstr), out parseR))
                        {
                            Program.XLSColsCount = parseR;
                        }
                        else
                        {
                            lf.write("XLSColsCount为空或者不为Int16整数");
                        }
                    }
                    else
                    if (getAttr(tmpstr) == "false")
                    {
                        Program.WriteLogFile = false;
                    }
                }
            }
        }