我檢查了上面的註釋,但他們沒有幫助。我使用VS2008作爲數據庫的ASP.Net和MS Access 2010。 我需要通過ASP網頁將數據從excel上傳到數據庫。Microsoft Jet數據庫引擎無法打開該文件。它已經由另一個用戶專門打開,或者您需要查看其數據的權限。
但是我收到一個錯誤如下:
「Microsoft Jet數據庫引擎無法打開文件 'C:\用戶\ poonamj \文檔\ Visual Studio 2008的\項目\智能工具\智能工具\上傳\'它已經被另一個用戶專門打開,或者您需要查看其數據的權限。「
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Collections.Generic;
using System.Data.OleDb;
namespace SmartTool
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void UploadFile(object sender, EventArgs e)
{
string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Uploads/") + fileName);
Response.Redirect(Request.Url.AbsoluteUri);
}
protected void DownloadFile(object sender, EventArgs e)
{
string filePath = (sender as LinkButton).CommandArgument;
Response.ContentType = ContentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
Response.WriteFile(filePath);
Response.End();
}
protected void DeleteFile(object sender, EventArgs e)
{
string filePath = (sender as LinkButton).CommandArgument;
File.Delete(filePath);
Response.Redirect(Request.Url.AbsoluteUri);
}
protected void ViewFile(object sender, EventArgs e)
{
//string filePath = (sender as LinkButton).CommandArgument;
// File.ReadAllLines(filePath);
//GridView2.DataSource = File.ReadAllLines(filePath);
//GridView2.DataBind();
//string[] content = File.ReadAllLines(filePath);
//GridView2.DataSource = content.
// OleDbConnection conn = new OleDbConnection();
// conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:/Users/poonamj/Documents/Visual Studio 2008/Projects/SmartTool/SmartTool/fallout.accdb;User id=admin;Password=";
// conn.Open();
string Access = Server.MapPath("App_Data/fallout.accdb");
string Excel = Server.MapPath("~/Uploads/");
string connect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Excel + ";Extended Properties=Excel 8.0;Mode=12;";
using (OleDbConnection conn = new OleDbConnection(connect))
{
using (OleDbCommand cmd = new OleDbCommand())
{
cmd.Connection = conn;
cmd.CommandText = "SELECT * INTO [MS Access;Database=" + Access + "].[New Table] FROM [Sheet1$]";
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string[] filePaths = Directory.GetFiles(Server.MapPath("~/Uploads/"));
List<ListItem> files = new List<ListItem>();
foreach (string filePath in filePaths)
{
files.Add(new ListItem(Path.GetFileName(filePath), filePath));
}
GridView1.DataSource = files;
GridView1.DataBind();
}
}
}
}
文件名一直不一樣。它是動態的,所以我不能指定文件名:( – user1763769 2013-02-21 05:20:02
)定義一個包含當前文件名的字符串變量,保持最新並在'uploads \'後面追加它的值,例如:'Server.MapPath(「〜/上傳/「&strVariableContainingFileName)'。這是否有幫助? – user1750028 2013-02-21 05:24:49
嘿其工作...字符串filePath =(發件人爲LinkButton).CommandArgument;字符串Excel = Server.MapPath(Path.GetFileName(filePath)); – user1763769 2013-02-21 05:39:51