Epplus如何删除我的excel& resx文件

时间:2017-05-19 09:42:49

标签: c# xml excel windows-forms-designer epplus

我开始学习c#和Windows表单。我创建了一个将resx(XML)文件转换为Excel的应用程序。

我的所有代码都有效,我的Excel文件已创建,我可以将其转换为resx文件。

但是,当我打开我的Excel文件时,我的数据之前和之后的空格都是这样添加的:Excel cell example。当我将其转换为resx文件时,确实如此 Resx file example

这是我的resx => excel代码:

//I use a application WindowsForm so any 'LBL' / 'TXT make reference to label or textBox I use them to set file or folder path
private void writeExcel()
    {
        Dictionary<string, string> dataSave = new Dictionary<string, string>();
        var path = LBL_DocumentPath.Text;
        XDocument doc = XDocument.Load(path);
        IEnumerable<XNode> nodes = doc.Descendants("data");
        foreach (XElement node in nodes)
        {
            string name = node.Attribute("name").Value;
            string value = node.Value;
            dataSave.Add(name, value);
        }
        CreateExcel(dataSave);
    }

    private void CreateExcel(Dictionary<string, string> dico)
    {
        int i = 1;
        FileInfo newFile = new FileInfo(LBL_FolderPath.Text + "/" + TXT_FileName.Text + ".xlsx");
        using (ExcelPackage package = new ExcelPackage(newFile))
        {
            try
            {
                ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Inventry");
                worksheet.Cells[1, 1].Value = "Name";
                worksheet.Cells[1, 2].Value = "value";
                worksheet.Cells[1, 3].Value = "translation";
                foreach (KeyValuePair<string, string> data in dico)
                {
                    string testMessage = String.Format("{0}", data.Value);
                    string delSpace = testMessage;
                    Regex regex = new Regex(@"(\s){2,}");
                    testMessage = regex.Replace(delSpace, "&");
                    i++;
                    worksheet.Cells[i, 1].Value = String.Format("{0}", data.Key);
                    worksheet.Cells[i, 2].Value = String.Format("{0}", testMessage);
                    worksheet.Cells.AutoFitColumns();
                }


                package.Save();
                MessageBox.Show("File created ! " + LBL_FolderPath.Text + "\\" + TXT_FileName.Text);
            }
            catch (Exception)
            {
                MessageBox.Show("File already exist, checks : " + LBL_DocumentPath.Text + "\\" + TXT_FileName.Text);
            }
        }
    }    

如果你想要我的所有代码,我可以给你一个dropbox链接。

提前感谢您提供的任何帮助。

数学式

Ps:道歉,我的英语不太好。我希望你能正确理解我

1 个答案:

答案 0 :(得分:0)

好的朋友给我解决方案。

这是我的正则表达式不起作用所以我替换

string testMessage = String.Format("{0}", data.Value);                                   
string delSpace = testMessage;
Regex regex = new Regex(@"(\s){2,}");
testMessage = regex.Replace(delSpace, "&");

通过

string testMessage = String.Format("{0}", data.Value);               
testMessage = testMessage.Replace("\n",string.Empty);
testMessage = testMessage.Replace("\r", string.Empty);
testMessage = testMessage.Replace("  ", string.Empty);