ExcelLibrary 在尝试创建时抛出未经授权的访问异常

问题描述 投票:0回答:1

正如标题所述,我的程序在尝试使用 ExcelLibrary 库创建 Excel 文件时抛出

UnauthorizedAccessException
,这很奇怪,因为我的计算机对此没有任何限制。我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Drawing;
using System.Linq;
using System.Data;
using System.Data.OleDb;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ExcelLibrary;
namespace ExcelTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private DataSet ds;
        private void Form1_Load(object sender, EventArgs e)
        {
            string cs = "Provider=Microsoft.jet.OLEDB.4.0;Data Source=test1.mdb;";
            ds = new DataSet("New_DataSet");
            DataTable dt = new DataTable("New_DataTable");
            string[] x = new string[20];
            for (int i = 1; i < x.Length; i++)
            {
                x[i] = "a";
            }
            ds.Locale = System.Threading.Thread.CurrentThread.CurrentCulture;
            dt.Locale = System.Threading.Thread.CurrentThread.CurrentCulture;
            OleDbConnection con = new OleDbConnection(cs);
             con.Open();
            string sql = "SELECT * FROM personas;";
            OleDbCommand cmd = new OleDbCommand(sql, con);
            OleDbDataAdapter adptr = new OleDbDataAdapter();
            adptr.SelectCommand = cmd;
            adptr.Fill(dt);
            con.Close();
            ds.Tables.Add(dt);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ExcelLibrary.DataSetHelper.CreateWorkbook("C:\\Users\\spereyra\\Documents\\Visual Studio 2012\\Projects\\deleteme\\ExcelTest", ds);
            MessageBox.Show("creating excel");
        }
    }
    }

您知道问题可能出在哪里吗?谢谢


编辑:我的异常日志(西班牙语,希望你不介意):

System.UnauthorizedAccessException: Acceso denegado a la ruta de acceso 'C:\Users\spereyra\Documents\Visual Studio 2012\Projects\deleteme\ExcelTest'.
   en System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   en System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
   en System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
   en System.IO.File.Open(String path, FileMode mode, FileAccess access, FileShare share)
   en ExcelLibrary.CompoundDocumentFormat.CompoundDocument.Create(String file)
   en ExcelLibrary.SpreadSheet.Workbook.Save(String file)
   en ExcelLibrary.DataSetHelper.CreateWorkbook(String filePath, DataSet dataset)
   en ExcelTest.Form1.button1_Click(Object sender, EventArgs e) en c:\Users\spereyra\Documents\Visual Studio 2012\Projects\deleteme\ExcelTest\Form1.cs:línea 51
exception oledb unauthorizedaccessexcepti excellibrary
1个回答
0
投票

发现我的错误,我已经指定了文件的路径,但忘记输入文件名和扩展名: 而不是

ExcelLibrary.DataSetHelper.CreateWorkbook("C:\\Users\\spereyra\\Documents\\Visual Studio 2012\\Projects\\deleteme\\ExcelTest", ds);
                MessageBox.Show("creating excel");

我应该放

ExcelLibrary.DataSetHelper.CreateWorkbook("C:\\Users\\spereyra\\Documents\\Visual Studio 2012\\Projects\\deleteme\\ExcelTest\\myExcel.xls", ds);
                    MessageBox.Show("creating excel");
© www.soinside.com 2019 - 2024. All rights reserved.