简体   繁体   English

SpreadsheetLight更新Excel文件

[英]SpreadsheetLight Updating excel file

I would like to read the excel file then find the correct row and add a value to that row But I am getting a error message file is used by another process. 我想阅读excel文件,然后找到正确的行并向该行添加一个值,但是我收到一个错误消息文件,该文件被另一个进程使用。

I have add the file to the FileStream so I am not sure why I am getting this error? 我已将文件添加到FileStream中,所以我不确定为什么会收到此错误?

System.IO.IOException: 'The process cannot access the file 'D:\\repos\\FHIRVal\\Update.xlsx' because it is being used by another process.' System.IO.IOException:'该进程无法访问文件'D:\\ repos \\ FHIRVal \\ Update.xlsx',因为它正在被另一个进程使用。

public static int UpdateExecelFile(string id, string status)
    {
        string FilePath = "D:\\repos\\FHIRVal\\Update.xlsx";


        using (SLDocument sl = new SLDocument())
        {
            FileStream fs = new FileStream(FilePath, FileMode.Open);
            SLDocument sheet = new SLDocument(fs, "Sheet");

            SLWorksheetStatistics stats = sheet.GetWorksheetStatistics();
            for (int j = 1; j < stats.EndRowIndex; j++)
            {
                var value = sheet.GetCellValueAsString(j, 2);

                if (value == id)
                {
                    Console.WriteLine(string.Format("{0} --- {1}", "Updating File", id));
                    string updateRow = string.Format("{0}{1}", "C",j);

                    sl.SetCellValue(updateRow, status);
                }

            }
            sheet.SaveAs(FilePath);
            fs.Close();
        }

我的订单不正确,我需要在sheetsaveas之前先打开fs.close。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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