Earth
Earth

Reputation: 3571

Add row manually in excel and finally update into table using C#.net

I am working on an application where I will be clicking a button on the web form to update time entry. On clicking the "Update time entry" button, an Excel file gets opened and I need to update today's time sheet details and again when saving and closing the Excel file, the details should be update in the database.

My question is how can I add another row to entering the time sheet after clicking like "+" button in the Excel? By that I can enter additional tasks on the same date.

I planned to use the following code:

// Load excel file.
var file = ExcelFile.Load("input.xlsx");
var sheet = file.Worksheets.ActiveWorksheet;

// Set worksheet without grid lines.
sheet.ViewOptions.ShowGridLines = false;

// Set worksheet with protection.
sheet.Protected = true;

// Iterate through the required cells, set their borders and unlock them.
var range = sheet.Cells.GetSubrange("A1", "C" + sheet.Rows.Count);

foreach (var cell in range)
{
    cell.SetBorders(MultipleBorders.Outside, Color.Black, LineStyle.Thin);
    cell.Style.Locked = false;
}

// Save Excel file
file.Save("output.xlsx");

Upvotes: 0

Views: 169

Answers (1)

Cussa Mitre
Cussa Mitre

Reputation: 36

If you don't have problems to use external libraries, I recommend you to use the ClosedXml, that is a very simple library to work with Excel.

Upvotes: 1

Related Questions