예제 #1
0
        public bool ExportTable(string path)
        {
            if (m_bIsExporting)
            {
                Clog.Instance.LogError("请等待上一次导出完成");
                return(false);
            }
            m_bIsExporting = true;
            if (!CExcelManager.Instance.Open(path))
            {
                m_bIsExporting = false;
                Clog.Instance.LogError("Excel打开失败");
                return(false);
            }
            Sheets sheets = CExcelManager.Instance.GetSheets();

            if (sheets == null)
            {
                m_bIsExporting = false;
                Clog.Instance.LogError("Excel找不到有效页签供导出");
                return(false);
            }
            int sheetsCount = sheets.Count;

            for (int i = 2; i <= sheetsCount; i++)
            {
                Worksheet sheet = sheets[i];
                if (sheet.Name.Contains("~"))
                {
                    continue;
                }
                string exportPath = CCommon.GetValue(CCommon.key_tableEP);
                if (!CFileManager.DirectorExist(exportPath))
                {
                    Clog.Instance.LogError("导出路径不存在:" + exportPath);
                    continue;
                }
                CTableTemplate tableTemplate = new CTableTemplate(sheet);
                if (tableTemplate == null)
                {
                    Clog.Instance.LogError(sheet.Name + "导出失败");
                    continue;
                }
                string        excelFilePath = Path.Combine(exportPath, sheet.Name + ".txt");
                FileStream    fs            = CFileManager.Open(excelFilePath, FileMode.Create);
                StreamWriter  st            = new StreamWriter(fs);
                StringBuilder s             = new StringBuilder();
                CCell         sheetBound    = tableTemplate.RangeBound;

                string strEnd = _GetColumnChar(sheetBound.Column) + sheetBound.Row;
                Range  cells  = sheet.Range["A1", strEnd].Cells;
                object[,] realCells = cells.Value;
                for (int r = 1; r <= sheetBound.Row; r++)
                {
                    s.Clear();
                    string cellValue = string.Empty;
                    object o         = realCells[r, 1];
                    if (o != null)
                    {
                        cellValue = o.ToString();
                    }
                    if (cellValue == CCommon.StrSkipRows)
                    {
                        continue;
                    }
                    for (int c = 1; c <= sheetBound.Column; c++)
                    {
                        cellValue = string.Empty;
                        o         = realCells[r, c];
                        if (o != null)
                        {
                            cellValue = o.ToString();
                        }
                        s.Append(cellValue);
                        s.Append(",");
                    }
                    st.WriteLine(s.ToString());
                }
                st.Close();
                fs.Close();
                st.Dispose();
                fs.Dispose();
                Clog.Instance.Log(sheet.Name + "导出成功");
            }
            m_bIsExporting = false;
            return(true);
        }
예제 #2
0
        public bool CreateCode(string path)
        {
            if (m_bIsGenerate)
            {
                Clog.Instance.LogError("请等待上一次生成代码完成");
                return(false);
            }
            m_bIsGenerate = true;
            if (!CExcelManager.Instance.Open(path))
            {
                m_bIsGenerate = false;
                Clog.Instance.LogError("Excel打开失败");
                return(false);
            }
            Sheets sheets = CExcelManager.Instance.GetSheets();

            if (sheets == null)
            {
                m_bIsGenerate = false;
                Clog.Instance.LogError("Excel找不到有效页签供生成代码");
                return(false);
            }
            int sheetsCount = sheets.Count;

            for (int i = 2; i <= sheetsCount; i++)
            {
                Worksheet sheet = sheets[i];
                if (sheet.Name.Contains("~"))
                {
                    continue;
                }
                string exportPath = CCommon.GetValue(CCommon.key_codeEP);
                if (!CFileManager.DirectorExist(exportPath))
                {
                    Clog.Instance.LogError("导出路径不存在:" + exportPath);
                    continue;
                }
                CTableTemplate tableTemplate = new CTableTemplate(sheet);
                if (tableTemplate == null)
                {
                    Clog.Instance.LogError(sheet.Name + "生成失败");
                    continue;
                }
                #region 1.write table

                FileStream    fs;
                StreamWriter  st;
                bool          isOverride = true;
                List <string> dataName   = tableTemplate.GetRowContent(CTableTemplate.KeyColumn);
                List <string> dataType   = tableTemplate.GetRowContent(CTableTemplate.KeyDataType);
                if (dataName == null || dataType == null)
                {
                    Clog.Instance.LogError("列名和列类型获取失败");
                    return(false);
                }
                string fileName      = "C" + sheet.Name + "Table.cs";
                string excelFilePath = Path.Combine(exportPath, fileName);
                if (File.Exists(excelFilePath))
                {
                    DialogResult r = MessageBox.Show("是否覆盖文件 " + fileName, "", MessageBoxButtons.YesNo);
                    isOverride = r == DialogResult.Yes;
                }
                if (isOverride)
                {
                    string tableScript = CFileManager.ReadAllText("Template/TableTemplate.cs");
                    tableScript = tableScript.Replace("#author#", "TM");
                    tableScript = tableScript.Replace("#datetime#", DateTime.Now.ToString("yyyy-MM-dd"));
                    tableScript = tableScript.Replace("#tablename#", sheet.Name);
                    tableScript = tableScript.Replace("#tableinforow_fields#", GetTableRowInfoFields(dataName, dataType));
                    tableScript = tableScript.Replace("#tableinforow_init#", GetTableRowInfoInit(dataName, dataType));

                    fs = CFileManager.Open(excelFilePath, FileMode.Create);
                    st = new StreamWriter(fs);
                    st.Write(tableScript);
                    st.Close();
                    fs.Close();
                    st.Dispose();
                    fs.Dispose();
                }
                #endregion

                #region 2.write tableinfo

                isOverride    = true;
                fileName      = "C" + sheet.Name + "TableInfo.cs";
                excelFilePath = Path.Combine(exportPath, fileName);
                string matchInit = string.Empty;
                string matchPro  = string.Empty;
                if (File.Exists(excelFilePath))
                {
                    DialogResult r = MessageBox.Show("是否覆盖文件 " + fileName, "", MessageBoxButtons.YesNo);
                    isOverride = r == DialogResult.Yes;
                    string input = File.ReadAllText(excelFilePath);

                    Match match = Regex.Match(input, @"#region Init([\s\S]*)#endregion Init");
                    if (match.Groups.Count >= 2)
                    {
                        matchInit = match.Groups[1].ToString();
                        matchInit = matchInit.Replace("\r\n\r\n", "");
                        int idx = matchInit.IndexOf("public");
                        matchInit = matchInit.Substring(idx);
                    }
                    match = Regex.Match(input, @"#region Properties([\s\S]*)#endregion Properties");
                    if (match.Groups.Count >= 2)
                    {
                        matchPro = match.Groups[1].ToString();
                        matchPro = matchPro.Replace("\r\n\r\n", "");
                        int idx = matchPro.IndexOf("public");
                        matchPro = matchPro.Substring(idx);
                    }
                }
                if (isOverride)
                {
                    string tableInfoScript = CFileManager.ReadAllText("Template/TableInfoTemplate.cs");
                    tableInfoScript = tableInfoScript.Replace("#author#", "TM");
                    tableInfoScript = tableInfoScript.Replace("#datetime#", DateTime.Now.ToString("yyyy-MM-dd"));
                    tableInfoScript = tableInfoScript.Replace("#tablename#", sheet.Name);
                    tableInfoScript = tableInfoScript.Replace("#tableinfo_fields#", GetFields(dataName, dataType));
                    tableInfoScript = tableInfoScript.Replace("#tableinfo_serialize#", GetSerialize(dataName));
                    tableInfoScript = tableInfoScript.Replace("#tableinfo_unserialize#", GetUnSerialize(dataName));
                    tableInfoScript = tableInfoScript.Replace("#tableinfo_assign#", GetAssign(dataName));

                    if (string.IsNullOrEmpty(matchPro))
                    {
                        tableInfoScript = tableInfoScript.Replace("#tableinfo_properties#", GetPropertiess(dataName, dataType));
                    }
                    else
                    {
                        tableInfoScript = tableInfoScript.Replace("#tableinfo_properties#", matchPro);
                    }

                    if (string.IsNullOrEmpty(matchInit))
                    {
                        tableInfoScript = tableInfoScript.Replace("#tableinfo_init#", GetInit());
                    }
                    else
                    {
                        tableInfoScript = tableInfoScript.Replace("#tableinfo_init#", matchInit);
                    }

                    fs = CFileManager.Open(excelFilePath, FileMode.Create);
                    st = new StreamWriter(fs);
                    st.Write(tableInfoScript);
                    st.Close();
                    fs.Close();
                    st.Dispose();
                    fs.Dispose();
                }
                #endregion
                Clog.Instance.Log(sheet.Name + "生成成功");
            }
            m_bIsGenerate = false;
            return(true);
        }