Microsoft Office Access 数据库引擎无法打开或写入文件“”
The Microsoft Office Access database engine cannot open or write to the file ''
数据需要从excel工作sheet读入.net gridview。
这是 aspx 和 aspx.cs 代码。
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" />
<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
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();出现以下错误:
The Microsoft Office Access database engine cannot open or write to the file ''. It is already opened exclusively by another user, or you
need permission to view and write its data.
网络服务已获得 excel sheet 所在文件夹的所有权限。
在尝试处理之前,使用 FileUpload.SaveAs 将上传的文件保存到磁盘位置。正如文档警告的那样,
The FileUpload control does not automatically save a file to the server ...
文件缓存在内存或磁盘上的临时文件夹中,直到您保存它们。
您可能应该考虑使用不同的方法来处理 Excel 文件,例如 EPPlus (for xlsx), NPOI(xls 和 xlsx)或仅使用 Open XML SDK (xlsx)。他们不需要 Jet 驱动程序,怪癖更少,并且有些可以直接读取流。
这将允许您直接从上传文件的 InputStream
属性 中读取上传的内容。以Open XML SDK为例,你可以这样写:
using (SpreadsheetDocument spreadsheetDocument =
SpreadsheetDocument.Open(fileuploadExcel.PostedFile.InputStream, false))
{
...
}
数据需要从excel工作sheet读入.net gridview。 这是 aspx 和 aspx.cs 代码。
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" />
<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
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();出现以下错误:
The Microsoft Office Access database engine cannot open or write to the file ''. It is already opened exclusively by another user, or you need permission to view and write its data.
网络服务已获得 excel sheet 所在文件夹的所有权限。
在尝试处理之前,使用 FileUpload.SaveAs 将上传的文件保存到磁盘位置。正如文档警告的那样,
The FileUpload control does not automatically save a file to the server ...
文件缓存在内存或磁盘上的临时文件夹中,直到您保存它们。
您可能应该考虑使用不同的方法来处理 Excel 文件,例如 EPPlus (for xlsx), NPOI(xls 和 xlsx)或仅使用 Open XML SDK (xlsx)。他们不需要 Jet 驱动程序,怪癖更少,并且有些可以直接读取流。
这将允许您直接从上传文件的 InputStream
属性 中读取上传的内容。以Open XML SDK为例,你可以这样写:
using (SpreadsheetDocument spreadsheetDocument =
SpreadsheetDocument.Open(fileuploadExcel.PostedFile.InputStream, false))
{
...
}