Esempio n. 1
0
        public static DataTable LoadOneExcelEx(FileInfo fInfo, string declareDir, string dataDir)
        {
            string name = "";

            try
            {
                // 拼接具体的Excel路径
                string str = fInfo.DirectoryName + "/" + fInfo.Name;
                str = str.Replace('\\', '/');
                // 一张表的数据
                Dictionary <int, string[]> sheet = new Dictionary <int, string[]>();
                sheet = ExcelFile.ReadExcel(str);
                if (sheet == null)
                {
                    return(null);
                }
                string[] headerRow;
                // 获取表头标题行的数据
                sheet.TryGetValue(sheetHeadRow, out headerRow);
                if (headerRow == null)
                {
                    return(null);
                }
                //截取生成Excel的名字,用来作为CS的文件名,exsample AAA.xlsx->AAA
                name = fInfo.Name.Substring(0, fInfo.Name.IndexOf('.'));
                // 行数
                int rowCount = sheet.Count;
                // 列数
                int colCount = headerRow.Length;
                // 一张Excel表的数据
                DataTable table = new DataTable(name);

                // 把一张表表头的数据放进来
                for (int i = 0; i < colCount; i++)
                {
                    string colName = i.ToString();
                    string col     = headerRow[i].ToString();
                    if (col != null)
                    {
                        colName = col.ToString();
                    }
                    DataColumn column = new DataColumn(colName);
                    table.Columns.Add(column);
                }

                // 这里把表里面具体内容的每行数据写进数据结构,从第五行开始读取和存放数据
                for (int i = sheetRow + 1; i < rowCount; i++)
                {
                    DataRow dataRow = table.NewRow();
                    if (!sheet.ContainsKey(i))
                    {
                        continue;
                    }
                    // 获取每行的数据
                    var row = sheet[i];
                    // 根据列数读取一行的数据保存下来
                    for (int j = 0; j < colCount; j++)
                    {
                        string value = string.Empty;
                        if (row != null && row[j] != null)
                        {
                            value = row[j];
                        }
                        dataRow[j] = value;
                    }
                    // 每行的数据添加进数据结构
                    table.Rows.Add(dataRow);
                }

                // 初始化表头信息
                bool hasIndex = false;
                List <ColumnInfo> colInfoAry = new List <ColumnInfo>();
                for (int i = colCount - 1; i >= 0; i--)
                {
                    ColumnInfo cInfo = GetFieldInfo(i, sheet, colInfoAry, name);

                    if (cInfo != null)
                    {
                        colInfoAry.Insert(0, cInfo);
                        if (cInfo.m_isIndex)
                        {
                            hasIndex = true;
                        }
                    }
                    else
                    {
                        table.Columns.RemoveAt(i);
                    }
                }

                if (!hasIndex)
                {
                    throw new Exception("没有找到被设置为1的索引列!");
                }

                if (colInfoAry.Count > 0)
                {
                    //TableChecker.CheckOut(table,colInfoAry);

                    ReferenceHandle.ReplaceReference(table, colInfoAry);
                    // 根据数据生成CS文件
                    CSharpWriter.WriteDataDeclareCSEx(declareDir, dataDir, table, colInfoAry);
                }

                sheet.Clear();
                sheet = null;
                return(table);
            }
            catch (Exception ex)
            {
                UnityEngine.Debug.LogError(string.Format("加载{0}表异常: {1}", fInfo.Name, ex.ToString()));
            }
            return(null);
        }