<form id="form1" runat="server">
<div class="content">
<div>
<h3>엑셀 파일 업로드 : <asp:Label ID="lblUploadText" runat="server"></asp:Label></h3><br />
</div>
<div>
XLS 파일 : <asp:FileUpload ID="FileUpload1" runat="server" CssClass="txtCommon" Width="300" />
<asp:Button ID="ButtonUpload1" runat="server" Text="업로드" Width="100" CssClass="txtCommon" OnClick="ButtonUpload1_Click" /><br />
XLSX 파일 : <asp:FileUpload ID="FileUpload2" runat="server" CssClass="txtCommon" Width="300" />
<asp:Button ID="ButtonUpload2" runat="server" Text="업로드" Width="100" CssClass="txtCommon" OnClick="ButtonUpload2_Click" /><br /><br />
</div>
<div><hr /></div>
<div>
<asp:GridView ID="GridView1" runat="server" BackColor="White"
BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3"
ForeColor="Black" GridLines="Vertical" Width="719px">
<FooterStyle BackColor="#CCCCCC" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="#CCCCCC" />
</asp:GridView>
</div>
</div>
</form>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
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;
public partial class UseExcel : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ButtonUpload1_Click(object sender, EventArgs e)
{
string strFilePath = string.Empty;
string strFileType = string.Empty;
string strFileName = string.Empty;
string strNewPath = string.Empty;
if (FileUpload1.PostedFile != null)
{
strFilePath = FileUpload1.PostedFile.FileName;
strFileType = FileUpload1.PostedFile.ContentType.ToString();
System.IO.FileInfo fi = new System.IO.FileInfo(strFilePath);
strFileName = fi.Name;
strNewPath = @"D:\TempUploadFolder\" + strFileName;
FileUpload1.SaveAs(strNewPath);
lblUploadText.Text = strFileName + " 파일이 업로드되었습니다.";
ReadContentInExcelFile(strNewPath, false);
}
}
protected void ButtonUpload2_Click(object sender, EventArgs e)
{
string strFilePath = string.Empty;
string strFileType = string.Empty;
string strFileName = string.Empty;
string strNewPath = string.Empty;
if (FileUpload2.PostedFile != null)
{
strFilePath = FileUpload2.PostedFile.FileName;
strFileType = FileUpload2.PostedFile.ContentType.ToString();
System.IO.FileInfo fi = new System.IO.FileInfo(strFilePath);
strFileName = fi.Name;
strNewPath = @"D:\TempUploadFolder\" + strFileName;
FileUpload2.SaveAs(strNewPath);
lblUploadText.Text = strFileName + " 파일이 업로드되었습니다.";
ReadContentInExcelFile(strNewPath, true);
}
}
private void ReadContentInExcelFile(string strFilePath, bool bExcelVersion2007)
{
string strProvider = string.Empty;
if (bExcelVersion2007)
{
strProvider = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + strFilePath + "; Extended Properties=Excel 12.0";
}
else
{
strProvider = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + strFilePath + "; Extended Properties=Excel 8.0";
}
string strQuery = "SELECT * FROM [Sheet1$]";
OleDbConnection oleDBCon = null;
OleDbCommand oleDBCom = null;
OleDbDataReader oleDBReader = null;
try
{
oleDBCon = new OleDbConnection(strProvider);
oleDBCom = new OleDbCommand(strQuery, oleDBCon);
oleDBCon.Open();
oleDBReader = oleDBCom.ExecuteReader(CommandBehavior.CloseConnection);
DataTable dtData = new DataTable();
dtData.Load(oleDBReader);
GridView1.DataSource = dtData.DefaultView;
GridView1.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.Source + "::" + ex.InnerException + "::" + ex.StackTrace);
}
finally
{
oleDBReader.Close();
oleDBReader.Dispose();
oleDBCon.Close();
}
}
}