简体   繁体   中英

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");

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM