如何使用EPPlus将文本转换为列?

时间:2013-12-05 10:12:09

标签: c# excel export-to-excel epplus

在MS Excel中使用“数据”选项卡,我可以执行“文本到列”。如何使用c#代码?

1 个答案:

答案 0 :(得分:0)

基于此example

var names = new[]
{
    "Brady, Tom",
    "Manning, Peyton",
    "Peterson, Adrian",
    "Lewis, Ray",
    "Reed, Ed",
    "Polamalu, Troy",
    "Johnson, Andre",
    "Revis, Darrelle",
    "Brees, Drew",
    "Peppers, Julius"
};
// Write names to a file
using (var excelPackage = new ExcelPackage(new FileInfo(@"d:\tmp\TextToColumns.xlsx")))
{
    var worksheet = excelPackage.Workbook.Worksheets.Add("TextToColumns");
    for (int i = 1; i < names.Length; i++)
    {
        worksheet.Cells[String.Format("A{0}", i)].Value = names[i - 1];
    }
    excelPackage.Save();
}
// Split names
using (var excelPackage = new ExcelPackage(new FileInfo(@"d:\tmp\TextToColumns.xlsx")))
{
    var worksheet = excelPackage.Workbook.Worksheets.First();
    foreach (var cell in worksheet.Cells)
    {
        var splittedValues = ((String)cell.Value).Split(',');
        // Write last name to the first column
        cell.Value = splittedValues[0];
        // Write first name to the next one column
        worksheet.Cells[cell.Start.Row, cell.Start.Column + 1].Value = splittedValues[1].TrimStart();
    }
    excelPackage.Save();
}