正如标题所描述的那样,我的程序在尝试使用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
答案 0 :(得分: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");