简体   繁体   English

Microsoft Office Access 数据库引擎无法打开或写入文件 ''

[英]The Microsoft Office Access database engine cannot open or write to the file ''

Data needs to be read into .net gridview from excel worksheet.数据需要从 excel 工作表读入 .net gridview。 Here are the aspx and aspx.cs codes.下面是 aspx 和 aspx.cs 代码。

ASPX: ASPX:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Read and Display Data From an Excel File (.xsl or .xlsx) in ASP.NET</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<b>Please Select Excel File: </b>
<asp:FileUpload ID="fileuploadExcel" runat="server" />&nbsp;&nbsp;
<asp:Button ID="btnImport" runat="server" Text="Import Data" OnClick="btnImport_Click" />
<br />
<asp:Label ID="lblMessage" runat="server" Visible="False" Font-Bold="True" ForeColor="#009933"></asp:Label><br />
<asp:GridView ID="grvExcelData" runat="server">
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
</asp:GridView>
</div>
</form>
</body>
</html>

ASPX.CS ASPX文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnImport_Click(object sender, EventArgs e)
{
    string connString = "";
    string strFileType = Path.GetExtension(fileuploadExcel.FileName).ToLower();
    string path = fileuploadExcel.PostedFile.FileName;
    //Connection String to Excel Workbook
    if (strFileType.Trim() == ".xls")
    {
        connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
    }
    else if (strFileType.Trim() == ".xlsx")
    {
        connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path +";Extended Properties=\"Excel 12.0 Xml;HDR=Yes;IMEX=2;\"";
    }

    string query = "SELECT [UserName],[Education],[Location] FROM [Sheet1$]";
    OleDbConnection conn = new OleDbConnection(connString);
        conn.Open();
    OleDbCommand cmd = new OleDbCommand(query, conn);
    OleDbDataAdapter da = new OleDbDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);
    grvExcelData.DataSource = ds.Tables[0];
    grvExcelData.DataBind();
    da.Dispose();
    conn.Close();
    conn.Dispose();
}

conn.Open(); conn.Open(); gives the following error:给出以下错误:

The Microsoft Office Access database engine cannot open or write to the file ''. Microsoft Office Access 数据库引擎无法打开或写入文件 ''。 It is already opened exclusively by another user, or you need permission to view and write its data.它已被其他用户以独占方式打开,或者您需要查看和写入其数据的权限。

Network service has been provided all the permissions for the folder in which the excel sheet is stored.已为网络服务提供了存储 Excel 工作表的文件夹的所有权限。

Save the uploaded file to a disk location with FileUpload.SaveAs before trying to process it.在尝试处理它之前,使用FileUpload.SaveAs将上传的文件保存到磁盘位置。 As the docs warn,正如文档警告的那样,

The FileUpload control does not automatically save a file to the server ... FileUpload 控件不会自动将文件保存到服务器...

Files are cached in memory or a temporary folder on disk until you save them.文件缓存在内存或磁盘上的临时文件夹中,直到您保存它们。

You should probably consider using a different method of processing Excel files, like EPPlus (for xlsx), NPOI (xls and xlsx) or just the Open XML SDK (xlsx).您可能应该考虑使用不同的方法来处理 Excel 文件,例如EPPlus (用于 xlsx)、 NPOI (xls 和 xlsx)或仅使用 Open XML SDK (xlsx)。 They don't need the Jet driver, have less quirks and some of the can read a stream directly.它们不需要 Jet 驱动程序,具有较少的怪癖,并且其中一些可以直接读取流。

This would allow you to read the uploaded contents directly from the uploaded file's InputStream property.这将允许您直接从上传文件的InputStream属性读取上传的内容。 Using Open XML SDK for example, you can write:例如,使用Open XML SDK ,您可以编写:

using (SpreadsheetDocument spreadsheetDocument =
SpreadsheetDocument.Open(fileuploadExcel.PostedFile.InputStream, false))
{
    ...
}

In my case, the file I was trying to access was still open.就我而言,我试图访问的文件仍处于打开状态。 I simply closed it and this resolved it.我只是关闭它,这解决了它。

暂无
暂无

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

相关问题 Microsoft Office Access 数据库引擎无法打开或写入文件 - The Microsoft Office Access database engine cannot open or write to the file mdb文件oledbconnection引发“ Microsoft Access数据库引擎无法打开或写入文件…” - mdb file oledbconnection throws “The Microsoft Access database engine cannot open or write to the file …” Microsoft Access 数据库引擎无法打开或写入 windows 服务 c# 中的文件 - The Microsoft Access database engine cannot open or write to the file in windows service c# 打开与MS Access 2007文件的连接时出错:无法打开MS Office Access数据库引擎工作组信息文件 - Error while opening a connection to MS Access 2007 file: Cannot open the MS Office Access database engine workgroup information file 尝试查询Excel文件时出现问题…“ Microsoft Office Access数据库引擎找不到对象&#39;Sheet1 $&#39;。” - Issue attempting to query an Excel file… “The Microsoft Office Access database engine could not find the object 'Sheet1$'..” Microsoft Office Excel 无法访问 IIS 上的文件 - Microsoft Office Excel cannot access the file on IIS Microsoft Office 12.0 Access 数据库引擎 OLE DB 提供程序 - Microsoft Office 12.0 Access database engine OLE DB Provider Microsoft jet 数据库引擎无法打开文件。''。 已被其他用户独占打开,或者您需要权限 - Microsoft jet database engine cannot open the file.''. It is already opened exclusively by another user, or you need permission Microsoft Office Access数据库引擎找不到对象“ PPR_Status_Detailed” - The Microsoft Office Access database engine could not find the object 'PPR_Status_Detailed' Microsoft Jet数据库引擎无法打开文件“。它已经由另一个用户独占打开,或者您需要获得查看其数据的权限” - The Microsoft Jet database engine cannot open the file ''.It is already opened exclusively by another user, or you need permission to view its data"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM