Microsoft Excel 无法访问文件“C:BBAC500”

本文关键字:BBAC500 文件 Excel 访问 Microsoft | 更新日期: 2023-09-27 17:56:38

我有一种方法可以将数据集导出到 excel 文件。数据集没有任何问题。我遇到的问题是我在保存文件时遇到一个奇怪的错误。它正在尝试访问正确的目录,但是看起来它正在向目录中添加一个字符串并完全忽略我的文件名。

public static void ExportDataSetToExcel(DataSet ds, string filename)
    {
        //Creae an Excel application instance
        Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
        //Create an Excel workbook instance and open it from the predefined location
        Microsoft.Office.Interop.Excel.Workbook excelWorkBook = excelApp.Workbooks.Add();
        foreach (DataTable table in ds.Tables)
        {
            //Add a new worksheet to workbook with the Datatable name
            Microsoft.Office.Interop.Excel.Worksheet excelWorkSheet = excelWorkBook.Sheets.Add();
            excelWorkSheet.Name = table.TableName;
            for (int i = 1; i < table.Columns.Count + 1; i++)
            {
                excelWorkSheet.Cells[1, i] = table.Columns[i - 1].ColumnName;
            }
            for (int j = 0; j < table.Rows.Count; j++)
            {
                for (int k = 0; k < table.Columns.Count; k++)
                {
                    excelWorkSheet.Cells[j + 2, k + 1] = table.Rows[j].ItemArray[k].ToString();
                }
            }
        }
        excelWorkBook.SaveAs(filename);
        excelWorkBook.Close();
        excelApp.Quit();
    }

我尝试过的事情:

excelWorkBook.SaveAs(filename, Type.Missing, Type.Missing, Type.Missing,
        Type.Missing, Type.Missing, XlSaveAsAccessMode.xlNoChange, 
        Type.Missing, Type.Missing, Type.Missing, Type.Missing,
        Type.Missing);

excelWorkBook.SaveAs("C:''" + filename)

excelWorkBook.SaveAs("C:''" + filename + ".xlsx")

excelWorkBook.SaveAs("C:''myfile.xlsx")

感谢您的帮助。

完整异常消息:

Microsoft Excel cannot access the file 'C:'CEF7C500'. There are several possible reasons:
• The file name or path does not exist.
• The file is being used by another program.
• The workbook you are trying to save has the same name as a currently open workbook.
   at Microsoft.Office.Interop.Excel._Workbook.SaveAs(Object Filename, Object FileFormat, Object Password, Object WriteResPassword, Object ReadOnlyRecommended, Object CreateBackup, XlSaveAsAccessMode AccessMode, Object ConflictResolution, Object AddToMru, Object TextCodepage, Object TextVisualLayout, Object Local)
   at MAP.ToExcel.ExportDataSetToExcel(DataSet ds, String filename) in c:'Users'tmitchell'Source'Workspaces'import'MAP'MAP'ToExcel.cs:line 68
   at MAP.Program.CreateExcelSheet() in c:'Users'tmitchell'Source'Workspaces'import'MAP'MAP'Program.cs:line 140
   at MAP.Program.Main(String[] args) in c:'Users'tmitchell'Source'Workspaces'import'MAP'MAP'Program.cs:line 31
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()</ExceptionString></Exception></TraceRecord>

Microsoft Excel 无法访问文件“C:�BBAC500”

尝试替换以下行:

excelWorkBook.SaveAs(filename);
excelWorkBook.Close();

excelWorkBook.Saved = true;
excelWorkBook.SaveCopyAs(filename);
excelWorkBook.Close(true, filename, Type.Missing);

确保以管理员权限运行应用程序。事实是系统(C:)云端硬盘需要管理员权限才能写入。

当您选择另一个驱动器来保存文件时,您会得到相同的结果吗?