gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:11:45 24 4
gpt4 key购买 nike

数据需要从 excel 工作表读入 .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" />&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

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表所在文件夹的所有权限。

最佳答案

使用FileUpload.SaveAs 将上传的文件保存到磁盘位置在尝试处理它之前。正如文档警告的那样,

The FileUpload control does not automatically save a file to the server ...

文件缓存在内存中或磁盘上的临时文件夹中,直到您保存它们。

您可能应该考虑使用不同的方法来处理 Excel 文件,例如 EPPlus (对于 xlsx),NPOI (xls 和 xlsx)或只是 Open XML SDK (xlsx)。他们不需要 Jet 驱动程序,怪癖更少,并且有些可以直接读取流。

这将允许您直接从上传文件的 InputStream 属性读取上传的内容。使用Open XML SDK例如,你可以这样写:

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

关于c# - Microsoft Office Access 数据库引擎无法打开或写入文件 '',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28293911/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com