Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# read excel file using epplus save to datatable


// Pass to param the excel file location (path)
public DataTable Read(string filePath)
        {
            try
            {
                DataTable dt = new DataTable();
                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
                    for (int rN = 2; rN <= workSheet.Dimension.End.Row; rN++)
                    {
                        ExcelRange row = workSheet.Cells[rN, 1, rN, workSheet.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

Convert DataTable to excel file c# using epplus

 /// <summary>
        /// Convert DataTable to Excel File
        /// </summary>
        /// <param name="filePath">Excel FilePath</param>
        /// <param name="table">DataTable</param>
        /// <param name="fileName">Excel Filename</param>
        /// <returns></returns>
        public bool Write(string filePath, DataTable table, string fileName)
        {
            try
            {
                bool success = false;
                var today = Convert.ToDateTime(DateTime.Now).ToString("yyyyMMddHHmmss");
                //delete existing files
                // Util_Directory.DeleteFiles(filePath);
                var newDir = filePath + "" + today;
                //if dir not exist create
                Util_Directory.CreateDirectory(newDir);
                FileInfo file = new FileInfo(Path.Combine(newDir, fileName));
                ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
          
                using (var package = new ExcelPackage(file))
                {
                    var workSheet = package.Workbook.Worksheets.Add("Sheet1");
                    //add column headers
                    var columns = table.Columns.Cast<DataColumn>().Select(x => x.ColumnName).ToList();
                    int i = 1;
                    foreach (var column in columns)
                    {
                        workSheet.Cells[1, i].Value = column;
                        i++;
                    }

                    // add row values
                    int startRow = 2;
                    foreach (DataRow row in table.Rows)
                    {
                        int startColumn = 1;
                        foreach (var item in row.ItemArray)
                        {
                            workSheet.Cells[startRow, startColumn].Value = item;
                            startColumn++;
                        }
                        startRow++;
                    }

                    workSheet.Cells.AutoFitColumns();
                    package.Save();
                    success = true;
                }

                return success;
            }
            catch (Exception)
            {
                throw;
            }
        }
Comment

PREVIOUS NEXT
Code Example
Csharp :: global exception handler c# 
Csharp :: substring in c# 
Csharp :: select specific columns from datatable in c# using lambda 
Csharp :: IsInstanceOf nunit 
Csharp :: render world space UI infront of everything unity 
Csharp :: c#l list<string initialize 
Csharp :: how to write text in specific position in c# 
Csharp :: C# Calculate MD5 Checksum For A File 
Csharp :: monogame print 
Csharp :: give an alias in model .net 
Csharp :: how to subtract two dates in dart 
Csharp :: convert getdate to ist c# 
Csharp :: redis cache repository .net 
Csharp :: change color unity over time 
Csharp :: how to check if an integer is in array c# 
Csharp :: convert string into float C# 
Csharp :: c# try parse date yyyymmdd 
Csharp :: minimum of three numbers 
Csharp :: use or in shell script 
Csharp :: c# get executing file name 
Csharp :: Count the Number of Duplicate Characters 
Csharp :: dictionary.add values to array c# 
Csharp :: c# list get last element 
Csharp :: display image script unity 
Csharp :: C# Async Function with await 
Csharp :: ssis sql query in script task 
Csharp :: c# convert bitmap to image 
Csharp :: inheritance in c# 
Csharp :: camelCase and snakeCase 
Csharp :: sqlite execute 
ADD CONTENT
Topic
Content
Source link
Name
5+7 =