当我的数据包含delmiter值时,我遇到了问题,即这些单元格将被填充到错误的位置。
我正在从我的应用程序导出和导入excel数据。以下是我的导出的工作片段。我将分隔符设置为“;”但是如果我的任何属性值包含此字符,都会引起问题。因此,我想知道是否有更好的方法来加载数据,从而允许我在单元格中使用定界符值。
public void Export()
{
using (ExcelPackage excel = new ExcelPackage())
{
excel.Workbook.Worksheets.Add("Worksheet1");
var headerRow = new List<string[]>()
{
new string[] { "Id", "FirstName", "LastName", }
};
// Determine the header range (e.g. A1:D1)
string headerRange = "A1:" + Char.ConvertFromUtf32(headerRow[0].Length + 64) + "1";
var worksheet = excel.Workbook.Worksheets["Worksheet1"];
worksheet.Cells[headerRange].LoadFromArrays(headerRow);
worksheet.DefaultColWidth = 20.0;
//TODO examine delimiter
var counter = 2;
var format = new OfficeOpenXml.ExcelTextFormat();
format.Delimiter = ';';
//format.TextQualifier = '"';
format.DataTypes = new[] { eDataTypes.String };
foreach (Attendee item in context.Attendees.Include(attendee => attendee.Tags))
{
worksheet.Cells[counter++, 1].LoadFromText($"{item.Id};{item.FirstName};{item.LastName}", format);
}
//Create the response
Response.Clear();
Response.ContentEncoding = Encoding.UTF8;
Response.HeaderEncoding = Encoding.UTF8;
Response.Charset = Encoding.UTF8.WebName;
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("Content-Disposition", "attachment; filename=people.xlsx");
Response.BinaryWrite(excel.GetAsByteArray());
Response.Flush();
Response.End();
}
}
这有效,但是如果我的任何字段包含“;”我会有问题。我可以使用某些古老的定界符值还是一种更安全的方式来加载数据?我可能会编写两个嵌套的for循环,并在每个单元上调用称重传感器,但这会很丑陋,因为我的真实代码中大约有15列/字段。我也无法从集合中加载数据,因为有些字段我不想显示,它们最终以错误的顺序显示。
答案 0 :(得分:0)
为什么不逐个使用电池?
foreach (Attendee item in context.Attendees.Include(attendee => attendee.Tags))
{
int column = 1;
worksheet.Cells[counter, column++].Value = item.Id;
worksheet.Cells[counter, column++].Value = item.FirstName;
worksheet.Cells[counter++, column++].Value = item.LastName;
}
编辑-完整代码
public void Export()
{
using (ExcelPackage excel = new ExcelPackage())
{
excel.Workbook.Worksheets.Add("Worksheet1");
var headerRow = new List<string[]>()
{
new string[] { "Id", "FirstName", "LastName", }
};
// Determine the header range (e.g. A1:D1)
string headerRange = "A1:" + Char.ConvertFromUtf32(headerRow[0].Length + 64) + "1";
var worksheet = excel.Workbook.Worksheets["Worksheet1"];
worksheet.Cells[headerRange].LoadFromArrays(headerRow);
worksheet.DefaultColWidth = 20.0;
var counter = 2;
foreach (Attendee item in context.Attendees.Include(attendee => attendee.Tags))
{
int column = 1;
worksheet.Cells[counter, column++].Value = item.Id;
worksheet.Cells[counter, column++].Value = item.FirstName;
worksheet.Cells[counter++, column++].Value = item.LastName;
}
//Create the response
Response.Clear();
Response.ContentEncoding = Encoding.UTF8;
Response.HeaderEncoding = Encoding.UTF8;
Response.Charset = Encoding.UTF8.WebName;
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("Content-Disposition", "attachment; filename=people.xlsx");
Response.BinaryWrite(excel.GetAsByteArray());
Response.Flush();
Response.End();
}
}
答案 1 :(得分:0)
有一个ascii“ VTAB”字符(chr(11),0x0B),可以安全地用作分隔符,否则您可能永远不会遇到它了:)
有多少人真的知道如何按住ALT键(在Windows中)并在小键盘上键入“ 0011”来输入该控制字符?