0
ASP.NET보기에서 DataGrid를 선언했으며 Excel 스프레드 시트 (.xlsx)로 DataGrid를 채우는 C# 코드를 생성하고 싶습니다. 나는 웹 사이트를 실행하면Excel Reader ASP.NET
<asp:DataGrid id="DataGrid1" runat="server"/>
<script language="C#" runat="server">
protected void Page_Load(object sender, EventArgs e)
{
string connString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\FileName.xlsx;Extended Properties=""Excel 12.0;HDR=YES;""";
// Create the connection object
OleDbConnection oledbConn = new OleDbConnection(connString);
try
{
// Open connection
oledbConn.Open();
// Create OleDbCommand object and select data from worksheet Sheet1
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [sheetname$]", oledbConn);
// Create new OleDbDataAdapter
OleDbDataAdapter oleda = new OleDbDataAdapter();
oleda.SelectCommand = cmd;
// Create a DataSet which will hold the data extracted from the worksheet.
DataSet ds = new DataSet();
// Fill the DataSet from the data extracted from the worksheet.
oleda.Fill(ds, "Something");
// Bind the data to the GridView
DataGrid1.DataSource = ds.Tables[0].DefaultView;
DataGrid1.DataBind();
}
catch
{
}
finally
{
// Close connection
oledbConn.Close();
}
}
</script>
는, 아무것도 정말 변화가 없습니다 : 여기 내가 가지고있는 코드입니다. 뭐라 구요?
당신은 오류가 발생하지 않았습니까? - catch 블록 내에 코드가 없습니다. – CResults
try/catch/finally 블록에서 finally를 사용하려면 catch 블록을 지정하지 않아도됩니다. 마침내/finally로 충분하므로 오류가 발생하면 버블됩니다. 쪽으로... – ozczecho