【发布时间】:2017-05-19 09:42:49
【问题描述】:
我开始学习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:对不起,我的英语不是很好。希望你能正确理解我的意思
【问题讨论】:
-
我认为你的英语完全没问题。易于理解,没有明显的语法错误。 Waaay 比我在这里看到的很多英语(包括我的)都好
标签: c# xml excel windows-forms-designer epplus