无法在 c# 中将 excel 文件另存为只读错误?

Cannot SaveAs an excel file in c# without it giving a read-only error?

    private void button1_Click(object sender, EventArgs e)
    {
        xlApp = new Excel.Application();
        xlWorkBook = xlApp.Workbooks.Open(fileName, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
        range = xlWorkSheet.UsedRange;

        xlAppDestination = new Excel.Application();
        xlAppDestination.DisplayAlerts = false;
        xlWorkBookDestination = xlAppDestination.Workbooks.Open(fileNameDestination, 0, false, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
        xlWorkSheetDestination = (Excel.Worksheet)xlWorkBookDestination.Worksheets.get_Item(1);
        rangeDestination = xlWorkSheetDestination.UsedRange;

        rangeDestination.Cells[1, 1] = (string)(range.Cells[14, 3] as Excel.Range).Text.ToString();
        textBox1.Text = fileNameDestination;

        //xlAppDestination.ActiveWorkbook.Save();
        xlWorkBookDestination.SaveAs(fileNameDestination);
        xlWorkBookDestination.Close(0);
        xlWorkBook.Close(0);


        xlCleanup();
    }

我正在尝试打开一个 excel 文件,并将该文件中的信息传输到另一个 excel 文件,即目标 excel 文件。唯一的问题是在储蓄方面。我以非只读方式打开目标文件,但当我尝试另存为时,它说它是只读的。我确保后台没有额外的 excel 任务 运行,它仍然会发生。当我使用保存时它可以工作,但它不能可靠地保存在同一位置的目标文件。我还没有确定它采用什么模式,但我需要能够每次都在同一个地方覆盖目标文件,因为这将附加到已经存在的旧 excel 文件。无论我对 saveas 语句做什么,它总是让我返回只读参数。

编辑-我尝试使用 .Save() 选项并关闭警报。但它并没有忽略覆盖目标文件的警告,而是将其粘贴在 Documents 文件夹中。有没有人对常规 Save() 方法的行为有经验?

您是否右键单击 excel 文件并检查其属性以确保未设置 "Read-Only" 属性?

rangeDestination.Cells[1, 1] = (string)(range.Cells[14, 3] as Excel.Range).Text.ToString(); textBox1.Text = fileNameDestination & ".xls";