Search code examples
c#xmlexcelwindows-forms-designerepplus

Epplus how to remove spaces into my excel & resx file


I'm starting to learn c # and Windows form. I create an application that transforms a resx (XML) file into an Excel.

All my code works, my Excel file is created and I can convert it to a resx file.

But, when I open my Excel file, spaces before and after my data has been added like this : Excel cell example. And when I convert it to resx file, it does Resx file example

Here is my resx => excel code :

//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);
            }
        }
    }    

If you want all my code, I can give you a dropbox link.

Thanks in advance for any help you can give me.

Math.

Ps: My apologies, my English is not very good. I hope you will understand me correctly


Solution

  • Ok a friend give me solution.

    It's my regex which does not work so I replace

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

    by

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