EPPlus LoadFromDataTable()是双转义“&"号的 [英] EPPlus LoadFromDataTable() is double escaping ampersands
问题描述
这是我的代码:
ExcelPackage pck = new ExcelPackage(stream);
ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Rules");
ws.Cells["A1"].LoadFromDataTable(_rules, true);
ws.Cells[ws.Dimension.Address].AutoFitColumns();
ws.Cells[ws.Dimension.Address].Style.WrapText = true;
pck.Save();
_rules
数据表具有其文本包含与号的行:&.例如说AT&T
.在Excel中查看文件时,文本显示为AT&T
.我在 sharedStrings.xml 上进行了追溯,发现文本被两次转义到AT&T
.
The _rules
DataTable has rows whose text contains an ampersand: &. Let's say AT&T
for example. When viewing the file in Excel, the text appears as AT&T
. I drilled down on the sharedStrings.xml and found the text was double-escaped to AT&T
.
如何防止这种行为?我尝试将文本用双引号引起来,并注释掉AutoFitColumns()
和WrapText
行.
How can I prevent this behavior? I've tried wrapping the text in double-quotes as well as commenting out the AutoFitColumns()
and WrapText
lines.
推荐答案
您确定它不在源表中吗?由于共享字符串是xml,因此必须转义所有与"号.因此,如果源表中已经包含AT&T
,您将看到的正是您所期望的.
Are you positive it is not in the source table? Since shared strings is xml it has to escape any ampersand. So what you are seeing is exactly what you would expect if the source table has AT&T
already in it.
因此,在这里,当我在格式好的文本和坏的格式之间切换时,您会看到同一件事:
So, here when I alternate between good and bad formatted text you see the same thing:
[TestMethod]
public void Amp_String_Test()
{
//http://stackoverflow.com/questions/32569450/epplus-loadfromdatatable-is-double-escaping-ampersands
//Throw in some data
var datatable = new DataTable("tblData");
datatable.Columns.AddRange(new[] { new DataColumn("Col1", typeof(int)), new DataColumn("Col2", typeof(int)), new DataColumn("Col3", typeof(object)) });
for (var i = 0; i < 10; i++)
{
var row = datatable.NewRow(); row[0] = i; row[1] = i * 10;
//Alternate text
row[2] = i%2 == 0 ? "AT&T": "AT&T";
datatable.Rows.Add(row);
}
//Create a test file
var fi = new FileInfo(@"c:\temp\amptest.xlsx");
if (fi.Exists)
fi.Delete();
using (var pck = new ExcelPackage(fi))
{
var worksheet = pck.Workbook.Worksheets.Add("Sheet1");
worksheet.Cells.LoadFromDataTable(datatable, true);
pck.Save();
}
}
这篇关于EPPlus LoadFromDataTable()是双转义“&"号的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!