使用EPPlus读取Excel文件时如何从对象访问单个数据?

时间:2014-10-16 07:16:14

标签: c# excel object objectdatasource epplus

我试图从c#中的excel文件中读取一行并使用EPPLUS存储在对象中。

现在,当我打印对象的内容时,它会将该行的所有内容显示为单个字符串。

如何限制对单细胞内容的访问?即单个单元格内容,以便我可以根据我的要求使用这些特定字段。

var existingFile = new FileInfo("D:\\Fields.xlsx"); 
using (var package = new ExcelPackage(existingFile)) 
{
    ExcelWorkbook workBook = package.Workbook;
    if (workBook != null) 
    { 
        if (workBook.Worksheets.Count > 0) 
        { 
            ExcelWorksheet currentWorksheet = workBook.Worksheets.First(); 
            var lastrow = currentWorksheet.Dimension.End.Row; 
            var lastcol = currentWorksheet.Dimension.End.Column; 
            for (int i = 1; i <= lastrow; i++) 
            {
                for (int j = 1; j <= lastcol; j++) 
                {
                    object asd = new object(); 
                    asd = currentWorksheet.Cells[i, j].Value; 
                    Console.WriteLine(asd);
                }
            }
        }
     }
}

1 个答案:

答案 0 :(得分:0)

您的代码应该可以正常运行。如果您看到excel文件的整行打印为单个字符串,那么我的猜测是excel文件本身的内容存在问题,在这种情况下您将不得不以某种方式解析字符串。

发布一个屏幕截图或你的,甚至更好的excel文件本身在网上的某个地方。

在此期间,请查看此代码,该代码首先创建自己的excel文件,然后使用您的代码重新打开并保存:

public void ConsoleWriteTest()
{
    //Create some data
    var existingFile = new FileInfo(@"c:\temp\temp.xlsx");
    if (existingFile.Exists)
        existingFile.Delete();

    using (var pck = new ExcelPackage(existingFile))
    {
        var workbook = pck.Workbook;
        var worksheet = workbook.Worksheets.Add("newsheet");

        #region Data

        //The data
        worksheet.Cells["A1"].Value = "Col1";
        worksheet.Cells["A2"].Value = "sdf";
        worksheet.Cells["A3"].Value = "ghgh";
        worksheet.Cells["A4"].Value = "sdf";
        worksheet.Cells["A5"].Value = "wer";

        worksheet.Cells["B1"].Value = "Col2";
        worksheet.Cells["B2"].Value = "Group B";
        worksheet.Cells["B3"].Value = "Group A";
        worksheet.Cells["B4"].Value = "Group C";
        worksheet.Cells["B5"].Value = "Group A";

        worksheet.Cells["C1"].Value = "Col3";
        worksheet.Cells["C2"].Value = 634.5;
        worksheet.Cells["C3"].Value = 274.5;
        worksheet.Cells["C4"].Value = 453.5;
        worksheet.Cells["C5"].Value = 634.5;

        worksheet.Cells["D1"].Value = "Col4";
        worksheet.Cells["D2"].Value = 996440;
        worksheet.Cells["D3"].Value = 185780;
        worksheet.Cells["D4"].Value = 686468;
        worksheet.Cells["D5"].Value = 996440;

        #endregion

        pck.Save();
    }

    //Reopen the file
    using (var package = new ExcelPackage(existingFile))
    {
        ExcelWorkbook workBook = package.Workbook;
        if (workBook != null)
        {
            if (workBook.Worksheets.Count > 0)
            {
                ExcelWorksheet currentWorksheet = workBook.Worksheets.First();
                var lastrow = currentWorksheet.Dimension.End.Row;
                var lastcol = currentWorksheet.Dimension.End.Column;
                for (int i = 1; i <= lastrow; i++)
                {
                    for (int j = 1; j <= lastcol; j++)
                    {
                        object asd = new object();
                        asd = currentWorksheet.Cells[i, j].Value;
                        Console.WriteLine(asd);
                    }
                }
            }
        }
    }
}

编辑1:行或列作为集合

//Get entire rows or columns as collection and then print by casting
using (var package = new ExcelPackage(existingFile))
{
    ExcelWorkbook workBook = package.Workbook;
    if (workBook != null)
    {
        if (workBook.Worksheets.Count <= 0) 
            return;

        ExcelWorksheet currentWorksheet = workBook.Worksheets.First();
        var lastrow = currentWorksheet.Dimension.End.Row;
        var lastcol = currentWorksheet.Dimension.End.Column;

        //get the row of column headers which are strings
        var asdrange = currentWorksheet.Cells[1, 1, 1, lastcol];
        Console.WriteLine("As cell objects");
        foreach (var cell in asdrange)
            Console.WriteLine(cell.Value);

        object asd = new object();
        asd = currentWorksheet.Cells[1, 1, 1, lastcol].Value;

        object[,] cellObjects = (object[,])asd;
        List<string> stringList = cellObjects.Cast<string>().ToList();

        Console.WriteLine(Environment.NewLine + "As casted to a List");
        Console.WriteLine(stringList[0]);
        Console.WriteLine(stringList[1]);
        Console.WriteLine(stringList[2]);
        Console.WriteLine(stringList[3]);

        //get the second row which is a mix of strings and double
        asdrange = currentWorksheet.Cells[2, 1, 2, lastcol];
        Console.WriteLine(Environment.NewLine + "As cell objects");
        foreach (var cell in asdrange)
            Console.WriteLine(cell.Value); 

        asd = currentWorksheet.Cells[2, 1, 2, lastcol].Value;
        cellObjects = (object[,])asd;
        List<object> objectList = cellObjects.Cast<object>().ToList();

        Console.WriteLine(Environment.NewLine + "As casted to a List");
        Console.WriteLine(objectList[0]);
        Console.WriteLine(objectList[1]);
        Console.WriteLine(objectList[2]);
        Console.WriteLine(objectList[3]);

        //Get Col3 which are doubles
        asdrange = currentWorksheet.Cells[2, 3, lastrow, 3];
        Console.WriteLine(Environment.NewLine + "As cell objects");
        foreach (var cell in asdrange)
            Console.WriteLine(cell.Value); 

        asd = currentWorksheet.Cells[2, 3, lastrow, 3].Value;
        cellObjects = (object[,])asd;
        List<double> doubleList = cellObjects.Cast<double>().ToList();

        Console.WriteLine(Environment.NewLine + "As casted to a List");
        Console.WriteLine(doubleList[0]);
        Console.WriteLine(doubleList[1]);
        Console.WriteLine(doubleList[2]);
        Console.WriteLine(doubleList[3]);
        Console.WriteLine(doubleList[4]);
        Console.WriteLine(doubleList[5]);
        Console.WriteLine(doubleList[6]);
        Console.WriteLine(doubleList[7]);
        Console.WriteLine(doubleList[8]);
    }
}