1

As the title describes, my program is throwing an UnauthorizedAccessException when attempting to create an Excel file with the ExcelLibrary Library, which is strange given my computer hasn't got any restrictions regarding that. My code:

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

Any ideas of what could the problem be? Thanks


EDIT: My exception log (it's in spanish, hope you don't mind):

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
OrangeWall
  • 79
  • 1
  • 8

1 Answers1

0

Found my error, I had specified the route to the file but forgot to type the filename and extension: Instead of

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

I should have put

ExcelLibrary.DataSetHelper.CreateWorkbook("C:\\Users\\spereyra\\Documents\\Visual Studio 2012\\Projects\\deleteme\\ExcelTest\\myExcel.xls", ds);
                    MessageBox.Show("creating excel");
OrangeWall
  • 79
  • 1
  • 8