Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

convert excel to datatable without xml configuration

  		DataTable dt = new Datatable();
        /// <summary>
        /// Convert excel file to DataTable
        /// </summary>
        /// <param name="filePath">excel file path</param>
        /// <returns>datatable</returns>
        public DataTable Read(string filePath)
        {

            try
            {
                FileInfo excelFile = new FileInfo(filePath);
                ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
                using (var package = new ExcelPackage(excelFile))
                {
                    ExcelWorksheet workSheet = package.Workbook.Worksheets[0];
                    //add column header
                    foreach (var firstRC in workSheet.Cells[1, 1, 1, workSheet.Dimension.End.Column])
                    {
                        dt.Columns.Add(firstRC.Text);
                    }

                    // add rows
                    foreach (ExcelWorksheet workSheetR in package.Workbook.Worksheets)
                    {
                        for (int rN = 2; rN <= workSheetR.Dimension.End.Row; rN++)
                        {
                            ExcelRange row = workSheetR.Cells[rN, 1, rN, workSheetR.Dimension.End.Column];
                            DataRow newR = dt.NewRow();
                            foreach (var cell in row)
                            {
                                newR[cell.Start.Column - 1] = cell.Text;
                            }

                            dt.Rows.Add(newR);
                        }
                    }
                }

                return dt;
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
        }
Comment

PREVIOUS NEXT
Code Example
Csharp :: how to twist a image in the code behind C# 
Csharp :: c# get Full Exception message if InnerException is not NULL 
Csharp :: f sharp functions calling each other 
Csharp :: esc exit winform 
Csharp :: How to do a comment in c# 
Csharp :: chaine de connexion sql server c# 
Csharp :: Find Number of Repetitions of Substring 
Csharp :: Initalize C# project in VS Code 
Csharp :: remove language folders build visual studio 
Csharp :: c# asqueryable select 
Csharp :: how to check if string from textbox exists in db 
Csharp :: sliding window algorithm in c# 
Csharp :: How to convert output of HttpClient PostAsJsonAsync() into user defined list of object 
Csharp :: c# custom comment tags 
Csharp :: afaik 
Csharp :: c# control datagridview null value 
Csharp :: unity matchinfo 
Csharp :: what is difference between int.Parse and toint32 in c# 
Csharp :: f# set function how to ignore duplicates 
Csharp :: cs foreach int 
Csharp :: change color unity back and forth 
Csharp :: compass direction mobile unity 
Csharp :: All and Any linq c# examlpe replace 
Csharp :: how to access a dictionary in c# 
Csharp :: UnitType parameter revit 2022 
Csharp :: tab key navigation C# winforms 
Csharp :: c# extension method in non static class 
Csharp :: asp.net list find 
Csharp :: how to textbox anywhere on chart in c# 
Csharp :: extension method c# 
ADD CONTENT
Topic
Content
Source link
Name
9+8 =