Example #1
0
        /// <summary>
        /// 导出学习师文件
        /// </summary>
        /// <returns>是否导出成功</returns>
        public bool ExportMasterFiles()
        {
            Hashtable     fileTable = new Hashtable();
            List <string> dataList;
            DataTable     masterTable = null;
            string        headLine    = "CraftID\tRecipeID\tProfessionID\tProfessionLevel\tBranchID\tReputationID\tnReputationLevel\tnPrice";
            string        newLine;
            string        fileName;
            string        folderName;
            string        filePath;

            // 读取数据库数据
            try
            {
                if (conn.State == ConnectionState.Closed) // 打开sql连接
                {
                    conn.Open();
                }

                string sqlString = string.Format("SELECT * FROM {0} ORDER BY {1}", "tbl_master_skills", "ProfessionLevel");
                masterTable = GetDataTable(sqlString);
            }
            catch (SqlException ex)
            {
                MessageBox.Show("读取学习师信息时产生sql异常: " + ex.ToString());
            }
            finally
            {
                if (conn.State == ConnectionState.Open) // 关闭sql连接
                {
                    conn.Close();
                }
            }

            foreach (DataRow r in masterTable.Rows)
            {
                newLine = r["CraftID"].ToString() + "\t" + r["RecipeID"].ToString() + "\t" + r["ProfessionID"].ToString() + "\t" +
                          r["ProfessionLevel"].ToString() + "\t" + r["BranchID"].ToString() + "\t" + r["ReputationID"].ToString() + "\t" +
                          r["nReputationLevel"].ToString() + "\t" + r["nPrice"].ToString();
                fileName   = r["filename"].ToString();
                folderName = r["foldername"].ToString();
                filePath   = Path.Combine(folderName, fileName);

                if (fileTable[filePath] != null)
                {
                    dataList = fileTable[filePath] as List <string>;
                    dataList.Add(newLine);
                }
                else
                {
                    dataList = new List <string>();
                    dataList.Add(headLine);
                    dataList.Add(newLine);
                    fileTable[filePath] = dataList;
                }
            }

            List <string> fileList = new List <string>();

            foreach (object o in fileTable.Keys)
            {
                fileList.Add(o.ToString());
            }

            // 选择导出的路径
            string resultFolder = "";
            FolderBrowserDialog openFolderDialog1 = new FolderBrowserDialog();

            openFolderDialog1.Description         = "请选择导出文件的保存目录";
            openFolderDialog1.ShowNewFolderButton = true;
            if (rootPath == null)
            {
                openFolderDialog1.RootFolder = Environment.SpecialFolder.MyComputer;
            }
            else
            {
                openFolderDialog1.SelectedPath = rootPath;
            }

            if (openFolderDialog1.ShowDialog() != DialogResult.OK)
            {
                return(false);
            }
            resultFolder = openFolderDialog1.SelectedPath;
            rootPath     = resultFolder;

            // 选择要导出的文件
            ChooseFileForm cForm  = new ChooseFileForm("请选择要导出的文件", fileList);
            DialogResult   result = cForm.ShowDialog();

            if (result != DialogResult.OK)
            {
                return(false);
            }

            List <string> fileNameList = new List <string>();

            foreach (string s in fileList)
            {
                dataList = fileTable[s] as List <string>;
                filePath = Path.Combine(resultFolder, s);
                fileNameList.Add(filePath);
                SaveDataToFile(filePath, dataList, false);
            }

            // 显示导出文件列表
            string message = "下列文件:\n\n";

            foreach (string s in fileNameList)
            {
                message = message + string.Format("{0}\n", s);
            }
            message = message + "\n\n";
            message = message + "已经成功导出!";
            MessageBox.Show(message);

            return(true);
        }
Example #2
0
        /// <summary>
        /// 导出学习师文件
        /// </summary>
        /// <returns>是否导出成功</returns>
        public bool ExportMasterFiles()
        {
            Hashtable fileTable = new Hashtable();
            List<string> dataList;
            DataTable masterTable = null;
            string headLine = "CraftID\tRecipeID\tProfessionID\tProfessionLevel\tBranchID\tReputationID\tnReputationLevel\tnPrice";
            string newLine;
            string fileName;
            string folderName;
            string filePath;

            // 读取数据库数据
            try
            {
                if(conn.State == ConnectionState.Closed) // 打开sql连接
                {
                    conn.Open();
                }

                string sqlString = string.Format("SELECT * FROM {0} ORDER BY {1}", "tbl_master_skills", "ProfessionLevel");
                masterTable = GetDataTable(sqlString);
            }
            catch (SqlException ex)
            {
                MessageBox.Show("读取学习师信息时产生sql异常: " + ex.ToString());
            }
            finally
            {
                if(conn.State == ConnectionState.Open) // 关闭sql连接
                {
                    conn.Close();
                }
            }

            foreach(DataRow r in masterTable.Rows)
            {
                newLine = r["CraftID"].ToString() + "\t" + r["RecipeID"].ToString() + "\t" + r["ProfessionID"].ToString() + "\t" +
                    r["ProfessionLevel"].ToString() + "\t" + r["BranchID"].ToString() + "\t" + r["ReputationID"].ToString() + "\t" +
                    r["nReputationLevel"].ToString() + "\t" + r["nPrice"].ToString();
                fileName = r["filename"].ToString();
                folderName = r["foldername"].ToString();
                filePath = Path.Combine(folderName, fileName);

                if(fileTable[filePath] != null)
                {
                    dataList = fileTable[filePath] as List<string>;
                    dataList.Add(newLine);
                }
                else
                {
                    dataList = new List<string>();
                    dataList.Add(headLine);
                    dataList.Add(newLine);
                    fileTable[filePath] = dataList;
                }
            }

            List<string> fileList = new List<string>();
            foreach(object o in fileTable.Keys)
            {
                fileList.Add(o.ToString());
            }

            // 选择导出的路径
            string resultFolder = "";
            FolderBrowserDialog openFolderDialog1 = new FolderBrowserDialog();
            openFolderDialog1.Description = "请选择导出文件的保存目录";
            openFolderDialog1.ShowNewFolderButton = true;
            if(rootPath == null)
            {
                openFolderDialog1.RootFolder = Environment.SpecialFolder.MyComputer;
            }
            else
            {
                openFolderDialog1.SelectedPath = rootPath;
            }

            if (openFolderDialog1.ShowDialog() != DialogResult.OK)
            {
                return false;
            }
            resultFolder = openFolderDialog1.SelectedPath;
            rootPath = resultFolder;

            // 选择要导出的文件
            ChooseFileForm cForm = new ChooseFileForm("请选择要导出的文件", fileList);
            DialogResult result = cForm.ShowDialog();
            if(result != DialogResult.OK)
            {
                return false;
            }

            List<string> fileNameList = new List<string>();
            foreach(string s in fileList)
            {
                dataList = fileTable[s] as List<string>;
                filePath = Path.Combine(resultFolder, s);
                fileNameList.Add(filePath);
                SaveDataToFile(filePath, dataList, false);
            }

            // 显示导出文件列表
            string message = "下列文件:\n\n";
            foreach (string s in fileNameList)
            {
                message = message + string.Format("{0}\n", s);
            }
            message = message + "\n\n";
            message = message + "已经成功导出!";
            MessageBox.Show(message);

            return true;
        }