【问题标题】:Reading Excel spreasheet using EPPlus使用 EPPlus 读取 Excel 电子表格
【发布时间】:2018-08-22 17:41:51
【问题描述】:

有人能指出我如何阅读 Excel 电子表格、遍历所有行和列以使用 EPPlus 和 MVC 检索值的正确方向吗?到目前为止,我看到了创建电子表格的示例,但在打开 excel 文件并从中读取值时没有找到任何示例。任何帮助将不胜感激。

TIA 苏..

【问题讨论】:

    标签: model-view-controller epplus


    【解决方案1】:

    简单示例

    // Get the file we are going to process
    var existingFile = new FileInfo(filePath);
    // Open and read the XlSX file.
    using (var package = new ExcelPackage(existingFile))
    {
        // Get the work book in the file
        var workBook = package.Workbook;
        if (workBook != null)
        {
            if (workBook.Worksheets.Count > 0)
            {
                // Get the first worksheet
                var currentWorksheet = workBook.Worksheets.First();
    
                // read some data
                object col1Header = currentWorksheet.Cells[1, 1].Value;
    

    【讨论】:

    • 太棒了..谢谢..我可以从这里根据我的要求把它拿下来。
    • 请注意,行和列的索引从 1 开始,因此“Cells[0, 1]”应该是“Cells[1, 1]”
    • 对于刚接触此主题的开发人员,您将需要 System; 的“使用”语句;系统.IO;和 OfficeOpenXml;运行此示例。
    【解决方案2】:

    一个简单的例子,你如何在 .net 4.5 中使用 EPPlus 读取 excel 文件

    public void readXLS(string FilePath)
    {
        FileInfo existingFile = new FileInfo(FilePath);
        using (ExcelPackage package = new ExcelPackage(existingFile))
        {
            //get the first worksheet in the workbook
            ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
            int colCount = worksheet.Dimension.End.Column;  //get Column Count
            int rowCount = worksheet.Dimension.End.Row;     //get row count
            for (int row = 1; row <= rowCount; row++)
            {
                for (int col = 1; col <= colCount; col++)
                {
                    Console.WriteLine(" Row:" + row + " column:" + col + " Value:" + worksheet.Cells[row, col].Value.ToString().Trim());
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多