简体   繁体   English

Microsoft Excel无法访问文件'C:\\ 0BBAC500'

[英]Microsoft Excel cannot access the file 'C:\0BBAC500'

I have a method to export a dataset to an excel file. 我有一种将数据集导出到Excel文件的方法。 Nothing is wrong with the dataset. 数据集没有任何问题。 The problem I am having though is I am getting a weird error when saving the file. 我遇到的问题是,在保存文件时出现了一个奇怪的错误。 It's trying to access the right directory, however it looks like it is adding a string to the directory and ignoring my filename altogether. 它正在尝试访问正确的目录,但是似乎它在目录中添加了一个字符串,而完全忽略了我的文件名。

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();
    }

Things I have tried: 我尝试过的事情:

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")

Thanks for your help. 谢谢你的帮助。

Full Exception Message: 完整的异常消息:

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>

Try to replace the lines: 尝试替换以下行:

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

with

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

Make sure you run the application with admin privileges. 确保以管理员权限运行该应用程序。 The fact is that the system (C:) drive requires admin privileges for writing. 事实是系统(C :)驱动器需要管理员权限才能进行写入。

Do you get the same results when you choose another drive for saving files? 选择另一个驱动器保存文件时,是否得到相同的结果?

暂无
暂无

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

相关问题 Microsoft Office Excel 无法访问 IIS 上的文件 - Microsoft Office Excel cannot access the file on IIS Microsoft Office Excel 无法访问文件'c:\inetpub\wwwroot\Timesheet\App_Data\Template.xlsx' - Microsoft Office Excel cannot access the file 'c:\inetpub\wwwroot\Timesheet\App_Data\Template.xlsx' C# 错误:Microsoft Excel 无法访问文件“...”。 有几个可能的原因 - C# Error: Microsoft Excel cannot access the file '…'. There are several possible reasons C# Win 应用程序,任务计划程序,用户帐户:系统。 Microsoft Excel 无法访问该文件 - C# Win App, Task Scheduler, User Account: System. Microsoft Excel cannot access the file Microsoft Excel无法访问Windows Server 2012上的文件 - Microsoft Excel cannot access the file on Windows Server 2012 Microsoft Excel无法访问ASP.net Project中的文件 - Microsoft Excel cannot access the file in ASP.net Project Microsoft Excel 无法访问文件“...”。 使用 Microsoft Office 2010 的 Windows Server 2008 R2 有多种可能的原因 - Microsoft Excel cannot access the file “…”. There are several possible reasons Windows Server 2008 R2 with Microsoft Office 2010 无法使用SQL(OLEDB)C#写入Microsoft Access文件 - Cannot write to a Microsoft Access file using SQL (OLEDB) C# 无法访问excel文件 - Cannot access excel file Microsoft Access 数据库引擎无法打开或写入 windows 服务 c# 中的文件 - The Microsoft Access database engine cannot open or write to the file in windows service c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM