
[HttpPost]
public ActionResult Imports()
{
    //判斷上傳的檔案是否存在
    if (fullFilePath != null)
    {
        file_paths = fullFilePath.ToString().Replace("\\", "\\\\");
        XLWorkbook workbook = new XLWorkbook(file_paths);
        // 讀取第一個 Sheet
        IXLWorksheet worksheet = workbook.Worksheet(1);
        // 定義資料起始/結束 Cell
        var firstCell = worksheet.FirstCellUsed();
        var lastCell = worksheet.LastCellUsed();
        // 使用資料起始/結束 Cell,來定義出一個資料範圍
        var data = worksheet.Range(firstCell.Address, lastCell.Address);
        // 將資料範圍轉型
        var table = data.AsTable();
        //讀取資料
        string Excel = "";
        Excel = table.Cell(6, 1).Value.ToString();
        //寫入資料
        //table.Cell(2, 1).Value = "test";
        //資料顯示
        Response.Write("<script language=javascript>alert('"+ Excel + "');</" +  "script>");
                
        if (System.IO.File.Exists(fullFilePath))
        {
            workbook.SaveAs(file_paths);
        }
    }
    else
    {
        Response.Write("<script language=javascript>alert('請先上傳檔案');</" +  "script>");
    }
    return View("ExcelImport");
}
<!--點選匯入檔案按鈕後POST到後端的Imports的Action-->
@using (Html.BeginForm("Imports", "Excel", FormMethod.Post, new { enctype =  "multipart/form-data" }))
{
    <div class="import_button">
        <button class="btn btn-primary" id="import_button">匯入檔案</button>
    </div>
}
首先先做讀取Excel資料,把寫入資料的部分先註解掉,這邊要讀取的對象為 row:6 / column:1

進入網頁中,將檔案上傳後,點選匯入檔案,將會顯示出Excel相對應的欄位值

接下來要做寫入資料至Excel的動作,把讀取資料的部分先註解掉,這邊要寫入的對象為 row:2 / column:1

進入網頁中,將檔案上傳後,點選匯入檔案,進到上傳成功的Excel檔案中就可以看到相對應欄位的資料成功被修改

