[英]Add row manually in excel and finally update into table using C#.net
我正在開發一個應用程序,我將單擊 web 表單上的按鈕來更新時間條目。 單擊“更新時間條目”按鈕后,將打開 Excel 文件,我需要更新今天的時間表詳細信息,並在保存和關閉 Excel 文件時再次更新,詳細信息應在數據庫中更新。
我的問題是如何在單擊 Excel 中的“+”按鈕后添加另一行以輸入時間表? 這樣我就可以在同一日期輸入其他任務。
我計划使用以下代碼:
// 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");
如果您在使用外部庫時沒有問題,我建議您使用ClosedXml ,這是一個非常簡單的庫,可以與 Excel 一起使用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.