コード例 #1
0
ファイル: Creater.cs プロジェクト: liu2359/AutoCSharp
 /// <summary>
 /// Sub Excel -> .cs
 /// </summary>
 /// <param name="inPath"></param>
 /// <param name="inFolderName"></param>
 /// <param name="inNameSpace"></param>
 /// <param name="inHeritNames"></param>
 /// <returns></returns>
 public bool SubExcel(string inPath, string inFolderName, string inNameSpace, string inHeritNames)
 {
     Assist.CheckFolderExist(inFolderName);
     Assist.GetObjPaths(".xls", inPath).ForEach(delegate(string path)
     {
         DataSet ds      = Assist.ExcelToData(path);
         DataTable dt    = ds.Tables[0];
         ExcelToCSharp e = new ExcelToCSharp(inHeritNames, dt.TableName, inFolderName);
         e.SetInherit(inHeritNames);
         List <string[]> values = new List <string[]>();
         for (int x = 0; x < dt.Columns.Count; x++)
         {
             string[] v = new string[3];
             v[0]       = dt.Rows[0][x].ToString();
             v[1]       = dt.Rows[2][x].ToString();
             v[2]       = dt.Rows[1][x].ToString();
             values.Add(v);
         }
         e.SetValue(values);
     });
     return(true);
 }
コード例 #2
0
ファイル: Creater.cs プロジェクト: TonyDongGuaPi/joework
        /// <summary>
        /// Excel -> .cs
        /// </summary>
        /// <param name="inFolderPath">输出文件夹名</param>
        /// <param name="suffixName">文件后缀名</param>
        /// <param name="inNameSpace">命名空间</param>
        /// <param name="inHeritNames">继承</param>
        /// <returns></returns>
        public bool Excel(string inPath, string inFolderName, string suffixName, string inNameSpace = "", string inHeritNames = "")
        {
            Assist.CheckFolderExist(inFolderName);
            Assist.DeleteFilesInFolder(inFolderName);

            Dictionary <string, string[]> finalclassname = new Dictionary <string, string[]>();

            //Load Cfg XML
            string cfgPath = Assist.RootPath + "Cfg";;
            Dictionary <string, XmlDocument> dicXML = Assist.GetXml(cfgPath);
            string      xmlName;
            XmlNodeList nodeList;
            Dictionary <string, string> dicFunctions = new Dictionary <string, string>();

            foreach (XmlDocument xml in dicXML.Values)
            {
                xmlName = xml.DocumentElement.Name;
                if (xmlName == "Functions")
                {
                    //保证先Load Functions.xml
                    nodeList = xml.GetElementsByTagName("function");
                    foreach (XmlNode node in nodeList)
                    {
                        foreach (XmlAttribute attr in node.Attributes)
                        {
                            if (attr.Name == "name")
                            {
                                dicFunctions[attr.Value] = node.InnerText;
                                break;
                            }
                        }
                    }
                    break;
                }
            }

            Dictionary <string, ExcelToCSharp> dicEc = new Dictionary <string, ExcelToCSharp>();
            string tableName = "";
            string tableDes  = "";

            string          cfgName = "", desName = "", fieldName;
            ExcelToCSharp   e;
            List <string[]> values;

            foreach (XmlDocument xml in dicXML.Values)
            {
                xmlName = xml.DocumentElement.Name;
                if (xmlName == "CfgItems")
                {
                    nodeList = xml.GetElementsByTagName("cfg");
                    foreach (XmlNode node in nodeList)
                    {
                        foreach (XmlAttribute attr in node.Attributes)
                        {
                            if (attr.Name == "name")
                            {
                                cfgName = attr.Value;
                            }
                            else if (attr.Name == "des")
                            {
                                desName = attr.Value;
                            }
                        }
                        //finalclassname.Add(cfgName, desName);
                        e = new ExcelToCSharp(inHeritNames, "Cfg" + Assist.FirstLetterUp(cfgName), inFolderName, dicFunctions);
                        e.SetInherit(inHeritNames);
                        values = new List <string[]>();
                        foreach (XmlNode paramNode in node.ChildNodes)
                        {
                            string[] v = new string[4];
                            foreach (XmlAttribute attr in paramNode.Attributes)
                            {
                                fieldName = attr.Name;
                                if (fieldName == "name")
                                {
                                    v[0] = attr.Value;
                                }
                                else if (fieldName == "type")
                                {
                                    v[1] = attr.Value;
                                }
                                else if (fieldName == "des")
                                {
                                    v[2] = attr.Value;
                                }
                                else if (fieldName == "link")
                                {
                                    v[3] = attr.Value;
                                }
                            }
                            values.Add(v);
                        }
                        e.SetValue(cfgName, values);
                        dicEc.Add(cfgName, e);
                    }
                }
            }

            try
            {
                Assist.GetObjPaths(suffixName, inPath).ForEach(delegate(string path)
                {
                    DataTable dt = null;
                    bool bExcel  = true;
                    if (suffixName == ".xlsx")
                    {
                        DataSet ds = Assist.ExcelToData(path);
                        dt         = ds.Tables[0];
                    }
                    else if (suffixName == ".txt")
                    {
                        tableDes = path;
                        dt       = Assist.TxtToData(path);
                        bExcel   = false;
                    }

                    if (dt != null)
                    {
                        // ===================== 类名注释 ========================

                        string acn  = path.Contains("(") ? path.Split('(')[1] : path.Split('(')[1];
                        string acnn = acn.Contains(")") ? acn.Split(')')[0] : acn.Split(')')[0];
                        tableDes    = acnn;
                        // =======================================================
                        tableName = Assist.FirstLetterUp(dt.TableName);
                        e         = new ExcelToCSharp(inHeritNames, "Cfg" + tableName, inFolderName, dicFunctions);
                        e.SetInherit(inHeritNames);

                        DataRowCollection rows = dt.Rows;
                        values              = new List <string[]>();
                        string note         = null;
                        string strFieldName = null;
                        for (int x = 0; x < dt.Columns.Count; x++)
                        {
                            if (rows[2][x].ToString() != "")
                            {
                                string[] v = new string[4];
                                v[0]       = rows[0][x].ToString().Trim();
                                v[1]       = rows[2][x].ToString().Trim();
                                if (bExcel)
                                {
                                    v[2] = rows[1][x].ToString().Trim();
                                    v[3] = rows[3][x].ToString().Trim();
                                }
                                else
                                {
                                    strFieldName = rows[1][x].ToString().Trim();
                                    note         = rows[3][x].ToString().Trim();
                                    v[2]         = note == "" ? strFieldName : string.Concat(strFieldName, ";", note);
                                    v[3]         = rows[4][x].ToString().Trim();
                                }

                                values.Add(v);
                            }
                        }
                        e.SetValue(acnn, values);
                        dicEc.Add(tableName, e);

                        finalclassname.Add(dt.TableName, new string[] { acnn, e.GetKeyType() });
                    }
                });
            }
            catch (Exception ex)
            {
                throw new Exception(tableDes + ex.Message + "可能是Excel打开表左下角的名字跟其它表重复。");
            }

            foreach (ExcelToCSharp ec in dicEc.Values)
            {
                ec.CreateCode(dicEc);
            }

            CreatePBData final = new CreatePBData(inHeritNames, "PBData", inFolderName);

            final.SetValue(finalclassname);
            return(true);
        }
コード例 #3
0
ファイル: Creater.cs プロジェクト: killliu/AutoCSharp
 /// <summary>
 /// Sub Excel -> .cs
 /// </summary>
 /// <param name="inPath"></param>
 /// <param name="inFolderName"></param>
 /// <param name="inNameSpace"></param>
 /// <param name="inHeritNames"></param>
 /// <returns></returns>
 public bool SubExcel(string inPath, string inFolderName, string inNameSpace, string inHeritNames)
 {
     Assist.CheckFolderExist(inFolderName);
     Assist.GetObjPaths(".xls", inPath).ForEach(delegate(string path)
     {
         DataSet ds = Assist.ExcelToData(path);
         DataTable dt = ds.Tables[0];
         ExcelToCSharp e = new ExcelToCSharp(inHeritNames, dt.TableName, inFolderName);
         e.SetInherit(inHeritNames);
         List<string[]> values = new List<string[]>();
         for (int x = 0; x < dt.Columns.Count; x++)
         {
             string[] v = new string[3];
             v[0] = dt.Rows[0][x].ToString();
             v[1] = dt.Rows[2][x].ToString();
             v[2] = dt.Rows[1][x].ToString();
             values.Add(v);
         }
         e.SetValue(values);
     });
     return true;
 }