예제 #1
0
        private void GenerateConfigurationCode_Click(object sender, EventArgs e)
        {
            TryRun(() =>
            {
                string filePath = filePath1.Text.Trim().移除路径前后引号();
                if (string.IsNullOrEmpty(filePath))
                {
                    MessageBox.Show("路径不能为空");
                    return;
                }
                var dataConfigInfo = new List <ExcelDataConfigInfo>();
                for (int i = 0; i < dgv1.Rows.Count; i++)
                {
                    dataConfigInfo.Add(new ExcelDataConfigInfo()
                    {
                        WorkSheetIndex = i + 1,
                        TitleLine      = Convert.ToInt32(dgv1.Rows[i].Cells[2].Value),
                        TitleColumn    = Convert.ToInt32(dgv1.Rows[i].Cells[3].Value)
                    });
                }

                string fileOutDirectoryName = Path.GetDirectoryName(Path.GetFullPath(filePath));
                var defaultConfigList       = EPPlusHelper.FillExcelDefaultConfig(filePath, fileOutDirectoryName, dataConfigInfo);

                var haveConfig = defaultConfigList.Find(a => a.ClassPropertyList.Count > 0) != null;
                if (!haveConfig)
                {
                    MessageBox.Show("未检测到配置信息");
                    return;
                }

                //将字符串写入文件
                StringBuilder errMsg = new StringBuilder();
                var filePathPrefix   = $@"{fileOutDirectoryName}\{Path.GetFileNameWithoutExtension(filePath)}_Result";

                foreach (var item in defaultConfigList)
                {
                    if (item.ClassPropertyList.Count > 0)
                    {
                        File.WriteAllText($@"{filePathPrefix}_{nameof(item.CrateDataTableSnippe)}_{item.WorkSheetName}.txt", item.CrateDataTableSnippe);
                        File.WriteAllText($@"{filePathPrefix}_{nameof(item.CrateClassSnippe)}_{item.WorkSheetName}.txt", item.CrateClassSnippe);
                    }
                    else
                    {
                        errMsg.Append(item.WorkSheetName + "、");
                    }
                }

                errMsg.RemoveLastChar('、');
                if (errMsg.Length > 0)
                {
                    MessageBox.Show($@"下列工作簿未生成配置项:{errMsg}");
                }
                if (!filePath.GetDirectoryName().Contains(@"\Desktop\"))
                {
                    WinFormHelper.OpenFilePath(filePath.GetDirectoryName());
                }
            });
        }