Ejemplo n.º 1
0
        /// <summary>
        /// 导出文件
        /// </summary>
        /// <param name="folderName">包含文件夹名的列</param>
        /// <param name="fileName">包含文件名的列</param>
        /// <returns>是否导出成功</returns>
        public bool ExportFiles(string folderName, string fileName)
        {
            Hashtable fileTable = new Hashtable();
            string resultFolder = "";

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

            if (openFolderDialog1.ShowDialog() == DialogResult.OK)
            {
                resultFolder = openFolderDialog1.SelectedPath + "\\";
                exportPath = openFolderDialog1.SelectedPath;

                string headline = string.Empty;
                int fileNameIndex = -1;
                int folderNameIndex = -1;

                for (int i = 0; i < fields.Count; i++)
                {
                    string field = (fields[i] as string).TrimEnd(new char[] { '\r', '\n' });
                    if (excludeFields.Contains(field))
                    {
                        excludeIndexList.Add(i);
                    }
                    else
                    {
                        headline = headline + field + "\t";
                    }

                    if (field == fileName)
                    {
                        fileNameIndex = i;
                    }
                    else if(field == folderName)
                    {
                        folderNameIndex = i;
                    }
                }

                if (fileNameIndex == -1 || folderNameIndex == -1) // 没有找到导出文件名所在列
                {
                    MessageBox.Show("没有找到导出文件夹和文件名所在列!");
                    return false;
                }

                headline = headline.TrimEnd(new char[] { '\r', '\n', '\t' }); // 去掉冗余信息

                foreach (string s in values) // 添加所有数据行到hash表中
                {
                    if (s == "") // 忽略空行
                    {
                        continue;
                    }
                    string[] data = s.Split('\t');
                    string fullPath = Path.Combine(resultFolder, Path.Combine(data[folderNameIndex], data[fileNameIndex]));

                    if (fileTable[fullPath] != null)
                    {
                        List<string> lines = fileTable[fullPath] as List<string>;
                        string t = string.Empty;
                        for (int i = 0; i < data.Length; i++)
                        {
                            if (!excludeIndexList.Contains(i))
                            {
                                t = t + data[i] + "\t";
                            }
                        }
                        //lines.Add(t.TrimEnd('\t'));
                        lines.Add(t.Substring(0, t.Length - 1));
                    }
                    else
                    {
                        List<string> lines = new List<string>();
                        lines.Add(headline);
                        string t = string.Empty;
                        for (int i = 0; i < data.Length; i++)
                        {
                            if (!excludeIndexList.Contains(i))
                            {
                                t = t + data[i] + "\t";
                            }
                        }
                        //lines.Add(t.TrimEnd('\t'));
                        lines.Add(t.Substring(0, t.Length - 1));
                        fileTable[fullPath] = lines;
                    }
                }

                List<string> fileNameList = new List<string>();
                foreach (string s in fileTable.Keys)
                {
                    fileNameList.Add(s);
                }

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

                // 导出文件
                foreach (string s in fileTable.Keys)
                {
                    if (fileNameList.Contains(s))
                    {
                        List<string> lines = fileTable[s] as List<string>;
                        SaveData(s, lines);
                    }
                }

                // 显示导出文件列表
                string message = "下列文件:\n\n";
                foreach (string s in fileNameList)
                {
                    message = message + string.Format("{0}.tab\n", s);
                }
                message = message + "\n\n";
                message = message + "已经成功导出!";
                MessageBox.Show(message);
            }
 
            return true;             
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 导入文件
        /// </summary>
        /// <param name="fileName">记录文件名的列名</param>
        /// <returns>是否导入成功</returns>
        public bool ImportFiles(string fileName)
        {
            bool success   = true; // 是否导入成功
            bool firstFile = true; // 是否正在读取第一个文件

            FolderBrowserDialog openFolderDialog1 = new FolderBrowserDialog();

            openFolderDialog1.Description         = "请选择导入文件所在的目录";
            openFolderDialog1.ShowNewFolderButton = false;

            if (rootPath == null)
            {
                openFolderDialog1.RootFolder = Environment.SpecialFolder.MyComputer;
            }
            else
            {
                openFolderDialog1.SelectedPath = rootPath;
            }

            if (openFolderDialog1.ShowDialog() == DialogResult.OK)
            {
                importPath = openFolderDialog1.SelectedPath;
                DirectoryInfo di    = new DirectoryInfo(importPath);
                FileInfo[]    files = di.GetFiles();
                List <string> data;                               // 行数据
                string[]      headLine;                           // 保存列数据的第一行
                List <string> valueList    = new List <string>(); // 保存行数据的链表
                List <string> fileNameList = new List <string>(); // 准备导入的文件链表

                foreach (FileInfo fi in files)
                {
                    fileNameList.Add(fi.FullName);
                }


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

                if (result == DialogResult.OK) // 用户确认要导入的文件
                {
                    fileNameList = cForm.FileNameList;
                    foreach (string s in fileNameList)
                    {
                        if (firstFile) // 判断是否正在读取第一个文件
                        {
                            data     = ReadFile(s);
                            headLine = data[0].Split(new char[] { '\t' });

                            fields = new string[headLine.Length + 1];
                            for (int i = 0; i < headLine.Length; i++)
                            {
                                fields[i] = headLine[i];
                            }
                            fields[headLine.Length] = fileName;

                            valueList.Add(data[0] + "\t" + fileName);
                            for (int i = 1; i < data.Count; i++)
                            {
                                valueList.Add(data[i] + "\t" + Path.GetFileName(s));
                            }

                            firstFile = false;
                        }
                        else
                        {
                            data = ReadFile(s);

                            for (int i = beginLine; i < data.Count; i++)
                            {
                                valueList.Add(data[i] + "\t" + Path.GetFileName(s));
                            }
                        }
                    }

                    values = new string[valueList.Count];
                    for (int i = 0; i < valueList.Count; i++)
                    {
                        values[i] = valueList[i];
                    }
                }
                else
                {
                    success = false;
                }
            }
            else
            {
                success = false;
            }

            return(success);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 导出文件
        /// </summary>
        /// <param name="fileName">包含文件名的列</param>
        /// <returns>是否导出成功</returns>
        public bool ExportFiles(string fileName)
        {
            Hashtable fileTable = new Hashtable();

            if (exportType == ExportType.None)
            {
                MessageBox.Show("没有设置导出数据的类型!", "导出数据",
                                MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            if (!autoExport)
            {
                FolderBrowserDialog openFolderDialog = new FolderBrowserDialog();
                openFolderDialog.Description = "请选择导出文件的保存目录";
                openFolderDialog.ShowNewFolderButton = true;
                
                if (string.IsNullOrEmpty(path))
                {
                    openFolderDialog.RootFolder = Environment.SpecialFolder.MyComputer;
                }
                else
                {
                    openFolderDialog.SelectedPath = path;
                }

                if (openFolderDialog.ShowDialog() == DialogResult.OK)
                {
                    path = openFolderDialog.SelectedPath;
                }
                else
                {
                    return false;
                }
            }                                      

            StringBuilder headLine = new StringBuilder();
            int fileNameIndex = -1;

            for (int i = 0; i < fields.Count; i++)
            {
                string field = (fields[i] as string).TrimEnd(new char[] { '\r', '\n' });

                switch (exportType)
                {
                    case ExportType.Exclude:
                        {
                            if (excludeFields.Contains(field))
                            {
                                excludeIndexList.Add(i);
                            }
                            else
                            {                                    
                                headLine.Append(string.Format("{0}\t", field));
                            }

                            break;
                        }
                    case ExportType.Include:
                        {
                            if (includeFields.Contains(field))
                            {
                                includeIndexList.Add(i);
                                headLine.Append(string.Format("{0}\t", field));
                            }

                            break;
                        }
                }

                if (field == fileName)
                {
                    fileNameIndex = i;
                }
            }

            if (fileNameIndex == -1) // 没有找到导出文件名所在列
            {
                MessageBox.Show("没有找到导出文件名所在列!");
                return false;
            }

            headLine.Remove(headLine.Length - 1, 1);                

            foreach (string s in values) // 添加所有数据行到hash表中
            {
                if (string.IsNullOrEmpty(s)) // 忽略空行
                {
                    continue;
                }

                string[] data = s.Split('\t');

                if (fileTable[Path.Combine(path, data[fileNameIndex])] != null)
                {
                    List<string> lines = fileTable[Path.Combine(path, data[fileNameIndex])] as List<string>;
                    StringBuilder line = new StringBuilder();

                    for (int i = 0; i < data.Length; i++)
                    {
                        switch (exportType)
                        {
                            case ExportType.Exclude:
                                {
                                    if (!excludeIndexList.Contains(i))
                                    {                                            
                                        line.Append(string.Format("{0}\t", data[i]));
                                    }

                                    break;
                                }
                            case ExportType.Include:
                                {
                                    if (includeIndexList.Contains(i))
                                    {
                                        line.Append(string.Format("{0}\t", data[i]));
                                    }

                                    break;
                                }
                        }                            
                    }

                    line.Remove(line.Length - 1, 1);
                    lines.Add(line.ToString());
                }
                else
                {
                    List<string> lines = new List<string>();
                    lines.Add(headLine.ToString());
                    StringBuilder line = new StringBuilder();

                    for (int i = 0; i < data.Length; i++)
                    {
                        switch (exportType)
                        {
                            case ExportType.Exclude:
                                {
                                    if (!excludeIndexList.Contains(i))
                                    {
                                        line.Append(string.Format("{0}\t", data[i]));
                                    }

                                    break;
                                }
                            case ExportType.Include:
                                {
                                    if (includeIndexList.Contains(i))
                                    {
                                        line.Append(string.Format("{0}\t", data[i]));
                                    }

                                    break;
                                }
                        }
                    }

                    line.Remove(line.Length - 1, 1);
                    lines.Add(line.ToString());
                    fileTable[Path.Combine(path, data[fileNameIndex])] = lines;
                }
            }

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

            foreach (string s in fileTable.Keys)
            {
                fileNameList.Add(s);
            }

            // 选择要导出的文件
            if (!autoExport)
            {
                ChooseFileForm cForm = new ChooseFileForm("请选择要导出的文件", fileNameList);
                DialogResult result = cForm.ShowDialog();

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

            // 导出文件
            foreach (string s in fileTable.Keys)
            {
                if (fileNameList.Contains(s))
                {
                    List<string> lines = fileTable[s] as List<string>;
                    SaveData(s, lines);
                }
            }

            // 显示导出文件列表
            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;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 导出文件
        /// </summary>
        /// <param name="folderName">包含文件夹名的列</param>
        /// <param name="fileName">包含文件名的列</param>
        /// <returns>是否导出成功</returns>
        public bool ExportFiles(string folderName, string fileName)
        {
            Hashtable fileTable    = new Hashtable();
            string    resultFolder = "";

            FolderBrowserDialog openFolderDialog1 = new FolderBrowserDialog();

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

            if (openFolderDialog1.ShowDialog() == DialogResult.OK)
            {
                resultFolder = openFolderDialog1.SelectedPath + "\\";
                exportPath   = openFolderDialog1.SelectedPath;

                string headline        = string.Empty;
                int    fileNameIndex   = -1;
                int    folderNameIndex = -1;

                for (int i = 0; i < fields.Count; i++)
                {
                    string field = (fields[i] as string).TrimEnd(new char[] { '\r', '\n' });
                    if (excludeFields.Contains(field))
                    {
                        excludeIndexList.Add(i);
                    }
                    else
                    {
                        headline = headline + field + "\t";
                    }

                    if (field == fileName)
                    {
                        fileNameIndex = i;
                    }
                    else if (field == folderName)
                    {
                        folderNameIndex = i;
                    }
                }

                if (fileNameIndex == -1 || folderNameIndex == -1) // 没有找到导出文件名所在列
                {
                    MessageBox.Show("没有找到导出文件夹和文件名所在列!");
                    return(false);
                }

                headline = headline.TrimEnd(new char[] { '\r', '\n', '\t' }); // 去掉冗余信息

                foreach (string s in values)                                  // 添加所有数据行到hash表中
                {
                    if (s == "")                                              // 忽略空行
                    {
                        continue;
                    }
                    string[] data     = s.Split('\t');
                    string   fullPath = Path.Combine(resultFolder, Path.Combine(data[folderNameIndex], data[fileNameIndex]));

                    if (fileTable[fullPath] != null)
                    {
                        List <string> lines = fileTable[fullPath] as List <string>;
                        string        t     = string.Empty;
                        for (int i = 0; i < data.Length; i++)
                        {
                            if (!excludeIndexList.Contains(i))
                            {
                                t = t + data[i] + "\t";
                            }
                        }
                        //lines.Add(t.TrimEnd('\t'));
                        lines.Add(t.Substring(0, t.Length - 1));
                    }
                    else
                    {
                        List <string> lines = new List <string>();
                        lines.Add(headline);
                        string t = string.Empty;
                        for (int i = 0; i < data.Length; i++)
                        {
                            if (!excludeIndexList.Contains(i))
                            {
                                t = t + data[i] + "\t";
                            }
                        }
                        //lines.Add(t.TrimEnd('\t'));
                        lines.Add(t.Substring(0, t.Length - 1));
                        fileTable[fullPath] = lines;
                    }
                }

                List <string> fileNameList = new List <string>();
                foreach (string s in fileTable.Keys)
                {
                    fileNameList.Add(s);
                }

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

                // 导出文件
                foreach (string s in fileTable.Keys)
                {
                    if (fileNameList.Contains(s))
                    {
                        List <string> lines = fileTable[s] as List <string>;
                        SaveData(s, lines);
                    }
                }

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

            return(true);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 导出文件
        /// </summary>
        /// <param name="fileName">包含文件名的列</param>
        /// <returns>是否导出成功</returns>
        public bool ExportFiles(string fileName)
        {
            Hashtable fileTable = new Hashtable();

            if (exportType == ExportType.None)
            {
                MessageBox.Show("没有设置导出数据的类型!", "导出数据",
                                MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            if (!autoExport)
            {
                FolderBrowserDialog openFolderDialog = new FolderBrowserDialog();
                openFolderDialog.Description         = "请选择导出文件的保存目录";
                openFolderDialog.ShowNewFolderButton = true;

                if (string.IsNullOrEmpty(path))
                {
                    openFolderDialog.RootFolder = Environment.SpecialFolder.MyComputer;
                }
                else
                {
                    openFolderDialog.SelectedPath = path;
                }

                if (openFolderDialog.ShowDialog() == DialogResult.OK)
                {
                    path = openFolderDialog.SelectedPath;
                }
                else
                {
                    return(false);
                }
            }

            StringBuilder headLine      = new StringBuilder();
            int           fileNameIndex = -1;

            for (int i = 0; i < fields.Count; i++)
            {
                string field = (fields[i] as string).TrimEnd(new char[] { '\r', '\n' });

                switch (exportType)
                {
                case ExportType.Exclude:
                {
                    if (excludeFields.Contains(field))
                    {
                        excludeIndexList.Add(i);
                    }
                    else
                    {
                        headLine.Append(string.Format("{0}\t", field));
                    }

                    break;
                }

                case ExportType.Include:
                {
                    if (includeFields.Contains(field))
                    {
                        includeIndexList.Add(i);
                        headLine.Append(string.Format("{0}\t", field));
                    }

                    break;
                }
                }

                if (field == fileName)
                {
                    fileNameIndex = i;
                }
            }

            if (fileNameIndex == -1) // 没有找到导出文件名所在列
            {
                MessageBox.Show("没有找到导出文件名所在列!");
                return(false);
            }

            headLine.Remove(headLine.Length - 1, 1);

            foreach (string s in values)     // 添加所有数据行到hash表中
            {
                if (string.IsNullOrEmpty(s)) // 忽略空行
                {
                    continue;
                }

                string[] data = s.Split('\t');

                if (fileTable[Path.Combine(path, data[fileNameIndex])] != null)
                {
                    List <string> lines = fileTable[Path.Combine(path, data[fileNameIndex])] as List <string>;
                    StringBuilder line  = new StringBuilder();

                    for (int i = 0; i < data.Length; i++)
                    {
                        switch (exportType)
                        {
                        case ExportType.Exclude:
                        {
                            if (!excludeIndexList.Contains(i))
                            {
                                line.Append(string.Format("{0}\t", data[i]));
                            }

                            break;
                        }

                        case ExportType.Include:
                        {
                            if (includeIndexList.Contains(i))
                            {
                                line.Append(string.Format("{0}\t", data[i]));
                            }

                            break;
                        }
                        }
                    }

                    line.Remove(line.Length - 1, 1);
                    lines.Add(line.ToString());
                }
                else
                {
                    List <string> lines = new List <string>();
                    lines.Add(headLine.ToString());
                    StringBuilder line = new StringBuilder();

                    for (int i = 0; i < data.Length; i++)
                    {
                        switch (exportType)
                        {
                        case ExportType.Exclude:
                        {
                            if (!excludeIndexList.Contains(i))
                            {
                                line.Append(string.Format("{0}\t", data[i]));
                            }

                            break;
                        }

                        case ExportType.Include:
                        {
                            if (includeIndexList.Contains(i))
                            {
                                line.Append(string.Format("{0}\t", data[i]));
                            }

                            break;
                        }
                        }
                    }

                    line.Remove(line.Length - 1, 1);
                    lines.Add(line.ToString());
                    fileTable[Path.Combine(path, data[fileNameIndex])] = lines;
                }
            }

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

            foreach (string s in fileTable.Keys)
            {
                fileNameList.Add(s);
            }

            // 选择要导出的文件
            if (!autoExport)
            {
                ChooseFileForm cForm  = new ChooseFileForm("请选择要导出的文件", fileNameList);
                DialogResult   result = cForm.ShowDialog();

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

            // 导出文件
            foreach (string s in fileTable.Keys)
            {
                if (fileNameList.Contains(s))
                {
                    List <string> lines = fileTable[s] as List <string>;
                    SaveData(s, lines);
                }
            }

            // 显示导出文件列表
            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);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 重构要保存的文件
        /// </summary>
        /// <param name="path">文件路径</param>
        public void RestructFiles(string path)
        {
            string resultFolder = "";
            FolderBrowserDialog openFolderDialog1 = new FolderBrowserDialog();

            openFolderDialog1.Description         = "请选择导出文件的保存目录";
            openFolderDialog1.ShowNewFolderButton = true;
            openFolderDialog1.RootFolder          = Environment.SpecialFolder.MyComputer;
            if (openFolderDialog1.ShowDialog() == DialogResult.OK)
            {
                resultFolder = openFolderDialog1.SelectedPath + "\\";

                // 计算有效属性的个数
                int      attributeNumber = 0; // 有效属性个数
                string[] datas           = values[0].Split('\t');
                foreach (string s in datas)
                {
                    if (s == "-1")
                    {
                        attributeNumber++;
                    }
                }
                attributeNumber--;

                List <string> fileNameList = new List <string>(); // 文件名链表
                foreach (string s in values)
                {
                    string[] tempData     = s.Split('\t');
                    string   tempFileName = tempData[0];
                    if (!fileNameList.Contains(tempFileName))
                    {
                        fileNameList.Add(tempFileName);
                    }
                }
                ChooseFileForm cForm  = new ChooseFileForm("请选择要导入的文件", fileNameList);
                DialogResult   result = cForm.ShowDialog();
                if (result != DialogResult.OK)
                {
                    return;
                }

                foreach (string s in values)
                {
                    if (s.Contains("-1\t")) // 读取默认数据行
                    {
                        string[]      data = s.Split('\t');
                        List <string> list = new List <string>();
                        list.Add("[Main]");
                        if (!fileNameList.Contains(data[0]))
                        {
                            continue;
                        }
                        string fileName = resultFolder + data[0];
                        files.Add(fileName);
                        for (int i = 2; i < fields.Count - attributeNumber; i++)
                        {
                            list.Add(fields[i].ToString() + "=" + data[i]);
                        }
                        list.Add("");                    // 添加新行
                        SaveSpecialData(fileName, list); // 保存默认数据行
                    }
                    else // 读取普通数据行
                    {
                        string[]      data = s.Split('\t');
                        List <string> list = new List <string>();
                        list.Add("[" + data[1] + "]");
                        if (!fileNameList.Contains(data[0]))
                        {
                            continue;
                        }
                        string fileName = resultFolder + data[0];
                        for (int i = fields.Count - attributeNumber; i < fields.Count; i++)
                        {
                            list.Add(fields[i].ToString() + "=" + data[i]);
                        }
                        list.Add("");                   // 添加新行
                        SaveNormalData(fileName, list); // 保存普通数据行
                    }
                }
                string message = "下列文件:\n\n";
                foreach (string s in files)
                {
                    message = message + string.Format("{0}\n", s);
                }
                message = message + "\n\n";
                message = message + "已经成功导出!";
                MessageBox.Show(message);
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 导入文件
        /// </summary>
        /// <param name="fileName">记录文件名的列名</param>
        /// <returns>是否导入成功</returns>
        public bool ImportFiles(string fileName)
        {
            bool success = true; // 是否导入成功
            bool firstFile = true; // 是否正在读取第一个文件

            FolderBrowserDialog openFolderDialog1 = new FolderBrowserDialog();
            openFolderDialog1.Description = "请选择导入文件所在的目录";
            openFolderDialog1.ShowNewFolderButton = false;

            if (rootPath == null)
            {
                openFolderDialog1.RootFolder = Environment.SpecialFolder.MyComputer;
            }
            else
            {
                openFolderDialog1.SelectedPath = rootPath;
            }

            if (openFolderDialog1.ShowDialog() == DialogResult.OK)
            {
                importPath = openFolderDialog1.SelectedPath;
                DirectoryInfo di = new DirectoryInfo(importPath);
                FileInfo[] files = di.GetFiles();
                List<string> data; // 行数据
                string[] headLine; // 保存列数据的第一行
                List<string> valueList = new List<string>(); // 保存行数据的链表
                List<string> fileNameList = new List<string>(); // 准备导入的文件链表

                foreach(FileInfo fi in files)
                {
                    fileNameList.Add(fi.FullName);          
                }

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

                if(result == DialogResult.OK) // 用户确认要导入的文件
                {
                    fileNameList = cForm.FileNameList;
                    foreach(string s in fileNameList)
                    {
                        if (firstFile) // 判断是否正在读取第一个文件
                        {
                            data = ReadFile(s);
                            headLine = data[0].Split(new char[] { '\t' });

                            fields = new string[headLine.Length + 1];
                            for (int i = 0; i < headLine.Length; i++)
                            {
                                fields[i] = headLine[i];
                            }
                            fields[headLine.Length] = fileName;

                            valueList.Add(data[0] + "\t" + fileName);
                            for (int i = 1; i < data.Count; i++)
                            {
                                valueList.Add(data[i] + "\t" + Path.GetFileName(s));
                            }

                            firstFile = false;
                        }
                        else
                        {
                            data = ReadFile(s);

                            for (int i = beginLine; i < data.Count; i++)
                            {
                                valueList.Add(data[i] + "\t" + Path.GetFileName(s));
                            }
                        }
                    }

                    values = new string[valueList.Count];
                    for (int i = 0; i < valueList.Count; i++)
                    {
                        values[i] = valueList[i];
                    }
                }
                else
                {
                    success = false;
                } 
            }
            else
            {
                success = false;
            }

            return success;
        }
Ejemplo n.º 8
0
        /// <summary>
        /// 重构要保存的文件
        /// </summary>
        /// <param name="path">文件路径</param>
        public void RestructFiles(string path)
        {
            string resultFolder = "";
            FolderBrowserDialog openFolderDialog1 = new FolderBrowserDialog();
            openFolderDialog1.Description = "请选择导出文件的保存目录";
            openFolderDialog1.ShowNewFolderButton = true;
            openFolderDialog1.RootFolder = Environment.SpecialFolder.MyComputer;
            if (openFolderDialog1.ShowDialog() == DialogResult.OK)
            {
                resultFolder = openFolderDialog1.SelectedPath + "\\";

                // 计算有效属性的个数
                int attributeNumber = 0; // 有效属性个数
                string[] datas = values[0].Split('\t');
                foreach (string s in datas)
                {
                    if (s == "-1")
                    {
                        attributeNumber++;
                    }
                }
                attributeNumber--;

                List<string> fileNameList = new List<string>(); // 文件名链表
                foreach(string s in values)
                {
                    string[] tempData = s.Split('\t');
                    string tempFileName = tempData[0];
                    if(!fileNameList.Contains(tempFileName))
                    {
                        fileNameList.Add(tempFileName);
                    }
                }
                ChooseFileForm cForm = new ChooseFileForm("请选择要导入的文件", fileNameList);
                DialogResult result = cForm.ShowDialog();
                if(result != DialogResult.OK)
                {
                    return;
                }

                foreach (string s in values)
                {
                    if (s.Contains("-1\t")) // 读取默认数据行
                    {
                        string[] data = s.Split('\t');
                        List<string> list = new List<string>();
                        list.Add("[Main]");
                        if(!fileNameList.Contains(data[0]))
                        {
                            continue;
                        }
                        string fileName = resultFolder + data[0];
                        files.Add(fileName);
                        for (int i = 2; i < fields.Count - attributeNumber; i++)
                        {
                            list.Add(fields[i].ToString() + "=" + data[i]);
                        }
                        list.Add(""); // 添加新行
                        SaveSpecialData(fileName, list); // 保存默认数据行
                    }
                    else // 读取普通数据行
                    {
                        string[] data = s.Split('\t');
                        List<string> list = new List<string>();
                        list.Add("[" + data[1] + "]");
                        if (!fileNameList.Contains(data[0]))
                        {
                            continue;
                        }
                        string fileName = resultFolder + data[0];
                        for (int i = fields.Count - attributeNumber; i < fields.Count; i++)
                        {
                            list.Add(fields[i].ToString() + "=" + data[i]);
                        }
                        list.Add(""); // 添加新行
                        SaveNormalData(fileName, list); // 保存普通数据行
                    }
                }
                string message = "下列文件:\n\n";
                foreach (string s in files)
                {
                    message = message + string.Format("{0}\n", s);
                }
                message = message + "\n\n";
                message = message + "已经成功导出!";
                MessageBox.Show(message);
            }
        }