Ejemplo n.º 1
0
        // 获取excel文件并存到列表里面
        private void getExcelFile(string excelName, string codeName)
        {
            this.createFolder(this.excelPath);
            /* .xls文件 */
            string xlsSet = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + this.excelPath + excelName + ";" + "Extended Properties='Excel 8.0;HDR=No;IMEX=1'";
            /* .xlsx文件 */
            string           xlsxSet = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + this.excelPath + excelName + ";" + ";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\"";
            DataSet          ds      = new DataSet();
            OleDbDataAdapter oada    = new OleDbDataAdapter("select * from [Sheet1$]", xlsxSet);

            try
            {
                oada.Fill(ds);
            }
            catch (Exception e)
            {
                Console.WriteLine("\n" + excelName + "获取失败" + "\n");
                return;
            }
            int rows = ds.Tables[0].Rows.Count;
            int cols = ds.Tables[0].Columns.Count;

            configAttributeArr.Clear();

            var time1 = System.DateTime.Now;

            Console.WriteLine(" ----- 读取配置文件 " + excelName + "      " + time1);
            for (int i = 0; i < rows; i++)
            {
                if (ds.Tables[0].Rows[i].ItemArray[0].ToString() == "")
                {
                    break;
                }

                if (i >= 3)
                {
                    ConfigAttribute configAttribute = new ConfigAttribute();
                    configAttribute.nameArr = new List <string>();
                    configAttribute.usedArr = new List <string>();
                    configAttribute.typeArr = new List <string>();
                    configAttribute.dataArr = new List <List <string> >();
                    List <string> list = new List <string>();
                    for (int j = 0; j < cols; j++)
                    {
                        string used = ds.Tables[0].Rows[2][j].ToString();
                        if (used == "s")
                        {
                            continue;
                        }
                        configAttribute.usedArr.Add(used);
                        string name = ds.Tables[0].Rows[0][j].ToString();
                        configAttribute.nameArr.Add(name);
                        string type = ds.Tables[0].Rows[1][j].ToString();
                        configAttribute.typeArr.Add(type);
                        string data = ds.Tables[0].Rows[i][j].ToString();
                        list.Add(data);
                    }
                    configAttribute.dataArr.Add(list);
                    configAttributeArr.Add(configAttribute);
                }
            }

            CreateClassFile.instance.createTxtFile(codeName);
        }