Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have multiple excel files located in a folder, each file have 2 sheets.

what i want, is to select the folder in which all my excel files are located and import excel sheet1 content to 1st table and sheet 2 content in 2nd table at one click.

I'm new to C# need urgent help plz.
Posted
Comments
Kevin Marois 20-Jan-16 14:50pm    
Take a look at SQL Server Integration Services (SSIS)
ZurdoDev 20-Jan-16 14:53pm    
Either SSIS or write a lot of code. Either way, it's not a simple job. Where are you stuck?
Member 11746885 20-Jan-16 15:12pm    
I don't know how to use SSIS, if any help me to achieve this by web application

Use SQL Server BULK INSERT:

BULK INSERT (Transact-SQL)[^]

Create a stored procedure (SP); pass the file name or names to the stored procedure. Call the SP from your C# code.
 
Share this answer
 
v2
First, I'd suggest using EPPlus for managing those Excel files. You can get it via NuGet. Add a reference to OfficeOpenXml in your using statements.

So you'll want to read in those files one at a time, get the values from each worksheet, add to a list of objects, and run an INSERT statement against your SQL database. There are many ways to do this, but this is quick and dirty using ADO.NET and dynamics. Code sample assumes you're using a console application and Microsoft.NET 4.6.

Code below is not tested, just an example of how you might do this.

C#
private static void ProcessFiles()
{
    // Get a reference to your directory
    var directory = new DirectoryInfo(@"C:\MyFiles");

    // Get all Excel files
    foreach (var file in directory.GetFiles("*.xslx"))
    {
        // Create a new package instance from the given file
        using (var package = new ExcelPackage(file))
        {
            // Get a local reference to the worksheet
            var workSheet1 = package.Workbook.Worksheets[0];

            // Create a list to hold your objects
            var objectList = new List<dynamic>();

            // Loop over the rows
            for (var x = workSheet1.Dimension.Start.Row; x <= workSheet1.Dimension.End.Row; x++)
            {
                // This is the sample value of the entry on sheet 1, row x. You can 
                // use this to create a dynamic object to insert into your database

                dynamic objectToInsert = new ExpandoObject();
                objectToInsert.Value1 = workSheet1.Cells[x, 0].Value;
                objectToInsert.Value2 = workSheet1.Cells[x, 1].Value;
                // .. Continue as needed

                // Once you have all your values in your object, add it to the list
                objectList.Add(objectToInsert);
            }

            // Insert into your database
            using (var conn = new SqlConnection("MyConnectionString"))
            {
                foreach (var row in objectList)
                {
                    // Better idea is to use a stored procedure, but this will get the                     
                    // job done
                    using (var cmd = new SqlCommand($"INSERT INTO dbo.MyTable (Value1, Value2) VALUES ({row.Value1}, {row.Value2})", conn))
                    {
                        cmd.ExecuteNonQuery();
                    }
                }
            }

            // .. Repeat for the next worksheet
            var workSheet2 = package.Workbook.Worksheets[1];
        }
    }
}
 
Share this answer
 
Comments
Member 11746885 20-Jan-16 15:14pm    
i want to achieve this by using a web application.kindly assist
DreamInHex 20-Jan-16 15:16pm    
Put the code I just gave you in a method in a web application. Works as a controller action or a webforms button click event.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900