2015-02-03 4 views
1

엑셀 워크 시트에서 .net gridview로 데이터를 읽어야합니다. 다음은 aspx 및 aspx.cs 코드입니다.Microsoft Office Access 데이터베이스 엔진에서 파일을 열거 나 쓸 수 없습니다. '

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.

엑셀 시트가 저장된 폴더에 대한 모든 권한이 네트워크 서비스에 제공되었습니다.

+0

이것은 파일 업로드가 아직 완료되지 않았거나 적어도 서버가 아직 파일을 릴리스하지 않았 음을 의미합니다. 'Click' 이벤트와 업로드는 브라우저의 'POST'요청에 대한 응답으로 발생합니다. –

+0

많은 도움이되지 않습니다. 내가 달리기 위해서해야 할 일을 말해 줄 수 있니? – Krithi07

답변

1

처리하기 전에 업로드 된 파일을 FileUpload.SaveAs의 디스크 위치에 저장하십시오. 문서가 경고 할 때,

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

파일은 저장 될 때까지 메모리 또는 디스크의 임시 폴더에 캐시됩니다.

EPPlus (xlsx의 경우), NPOI (xls 및 xlsx의 경우) 또는 Open XML SDK (xlsx의 경우)와 같은 다른 Excel 파일 처리 방법을 사용하는 것이 좋습니다. 그들은 제트 드라이버를 필요로하지 않으며 덜 버크를 가지며 일부는 직접 스트림을 읽을 수 있습니다.

이렇게하면 업로드 된 파일의 InputStream 속성에서 직접 업로드 된 내용을 읽을 수 있습니다. 예를 들어 Open XML SDK을 사용하면 다음과 같이 작성할 수 있습니다.

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