简体   繁体   English

如何从 Visual Studio 中删除 Excel 文件?

[英]How do I delete Excel file from Visual Studio?

VS and Excel Noob. VS 和 Excel 菜鸟。 When I use an Excel file in my Visual Studio, the file refuses to get deleted even after I stop running the code and even close Visual Studio.当我在 Visual Studio 中使用 Excel 文件时,即使我停止运行代码甚至关闭 Visual Studio,该文件也拒绝被删除。

I've tried to delete the file, stop code and then delete the file, closed Visual Studio.我试图删除文件,停止代码,然后删除文件,关闭 Visual Studio。 I haven't rebooted yet but I don't want to have to do that.我还没有重新启动,但我不想这样做。

        Excel.Application oXL = new Excel.Application();

        Excel.Workbook oWB = oXL.Workbooks.Open(xlsmfilePath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

        Excel.Worksheet oWS = oWB.Worksheets[1] as Excel.Worksheet;

        Excel.Range range;

        range = oWS.UsedRange;

        //read first row, first cell value 
        int rowCount = range.Rows.Count;
        int colCount = range.Columns.Count;
        string roadName = string.Empty;
        //iterate over the rows and columns and print to the console as it appears in the file
        //excel is not zero based!!
        for (int i = 2; i <= rowCount; i++)
        {
            for (int j = 1; j <= colCount; j++)
            {
                if (j == 17)
                {
                    if (range.Cells[i, j].Value2 == null || range.Cells[i, j].Value2.ToString() == "")
                        break;
                    //write the value to the console
                    if (i > 1 && range.Cells[i, j] != null && range.Cells[i, j].Value2 != null && range.Cells[i, j].Value2 != "" && range.Cells[i, j].Value2.Contains(fileName))
                    {
                        roadName=range.Cells[i, 4].Value2.ToString();
                        break;
                    }
                }
            }
        }

I wish I could do screenshots here.我希望我可以在这里做截图。

It just asks to "Try Again" to delete but it doesn't delete.它只是要求“再试一次”删除,但它不会删除。

How about this code:这段代码怎么样:

string authorsFile = "file.xlsx";

try
{
    if (File.Exists(Path.Combine(rootFolder, authorsFile)))
    {
        File.Delete(Path.Combine(rootFolder, authorsFile));
    }
    //nothing   
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

In order to make it so your excel document isn't stuck open and returning the error about "file in use by another process", you need to close it in your code.为了使您的 excel 文档不会卡在打开状态并返回有关“另一个进程正在使用文件”的错误,您需要在代码中关闭它。

Try closing your work book and then application.尝试关闭您的工作簿,然后申请。 This should release your excel document.这应该会释放您的 Excel 文档。 Add the following to the end of your method: oWB.Close(0);将以下内容添加到您的方法末尾: oWB.Close(0); then oXL.Quit();然后oXL.Quit();

Your code:您的代码:

Excel.Application oXL = new Excel.Application();
Excel.Workbook oWB = oXL.Workbooks.Open(xlsmfilePath, ... Missing Values ...);
...
// Code for altering workbook
... 
oWB.Close(0);
oXL.Quit();

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

相关问题 如何从 Visual Studio 的表中删除特定记录? - How do I delete specific records from a table in visual studio? Visual Studio 2012:如何打开与项目关联的Excel文件? - Visual Studio 2012: How do I open an excel file that is associated with my project? 如何使用Visual Studio项目资源中的Excel文件 - How to use Excel file from visual studio project resource 如何设置Visual Studio以便能够操作Excel文件? - What do I need to do to set up Visual Studio to be able to manipulate an Excel File? 从 Visual Studio 2019 运行控制台应用程序时如何加载 usersecrets 文件 - How do I get the usersecrets file to load when running a console app from Visual Studio 2019 Visual Studio 2010:如何通过编辑器扩展在项目中创建和/或更新特定文件? - Visual Studio 2010: How do I create and/or update a specific file within a Project from an Editor Extension? 从 Visual Studio 发布代码时如何将文件上传到 Azure 函数? - How do I upload file to Azure function while publishing the code from Visual Studio? 如何以编程方式在Visual Studio解决方案/程序集中加载文件? - How do I load a file in a Visual Studio solution/assembly programmatically? 如何在visual studio中编译和运行ashx文件? - How do I compile and run an ashx file in visual studio? 如何在Visual Studio 2015中重新生成Designer文件 - How do I regenerate designer File in Visual Studio 2015
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM