Monday, September 11, 2023

Excel read using epplus /json

Import license related line before you read excel file 



csharp
Copy code
using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

public class ExcelReader
{
    public List<List<string>> ReadExcelFile(string filePath)
    {
        List<List<string>> excelData = new List<List<string>>();

        FileInfo fileInfo = new FileInfo(filePath);
        using (ExcelPackage package = new ExcelPackage(fileInfo))
        {
            ExcelWorksheet worksheet = package.Workbook.Worksheets.FirstOrDefault();

            if (worksheet != null)
            {
                int rowCount = worksheet.Dimension.Rows;
                int columnCount = worksheet.Dimension.Columns;

                for (int row = 1; row <= rowCount; row++)
                {
                    List<string> rowData = new List<string>();
                    for (int col = 1; col <= columnCount; col++)
                    {
                        string cellValue = worksheet.Cells[row, col].Text;
                        rowData.Add(cellValue);
                    }
                    excelData.Add(rowData);
                }
            }
        }

        return excelData;
    }


Call

LIST<string > colum =ReadExcelFile(string filePath)[0] 



No comments:

Post a Comment