c sharp code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web.SessionState;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.OleDb;
using System.Text;
namespace excel_upload
{
public partial class onlyupload : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlInputFile File1;
protected System.Web.UI.HtmlControls.HtmlInputButton Submit1;
DataTable dt = new DataTable();
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
}
//protected void btnimport_Click(object sender, EventArgs e)
//{
// string filePath = string.Empty;
// if (fu_ImportCSV.HasFile && fu_ImportCSV.PostedFile.ContentType.Equals("application/vnd.ms-excel"))
// {
// gv_GridView.DataSource = (DataTable)ReadToEnd(fu_ImportCSV.PostedFile.FileName);
// gv_GridView.DataBind();
// lbl_ErrorMsg.Visible = false;
// }
// else
// {
// lbl_ErrorMsg.Text = "Please check the selected file type";
// lbl_ErrorMsg.Visible = true;
// }
//}
private object ReadToEnd(string filePath)
{
DataTable dtDataSource = new DataTable();
string[] fileContent = File.ReadAllLines(filePath);
if (fileContent.Count() > 0)
{
//Create data table columns
string[] columns = fileContent[0].Split(',');
for (int i = 0; i < columns.Count(); i++)
{
dtDataSource.Columns.Add(columns[i]);
}
//Add row data
for (int i = 1; i < fileContent.Count(); i++)
{
string[] rowData = fileContent[i].Split(',');
dtDataSource.Rows.Add(rowData);
}
}
return dtDataSource;
}
protected void btn_ImportCSV_Click(object sender, EventArgs e)
{
string filePath = string.Empty;
if (fu_ImportCSV.HasFile && fu_ImportCSV.PostedFile.ContentType.Equals("application/vnd.ms-excel"))
{
DataTable dtCSVTable = (DataTable)ReadToEnd(fu_ImportCSV.PostedFile.FileName);
gv_GridView.DataSource = (DataTable)ReadToEnd(fu_ImportCSV.PostedFile.FileName);
gv_GridView.DataBind();
lbl_ErrorMsg.Visible = false;
}
else
{
lbl_ErrorMsg.Text = "Please check the selected file type";
lbl_ErrorMsg.Visible = true;
}
}
}
}
aspx page:
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="fu_ImportCSV" runat="server" />
<asp:Button ID="btn_ImportCSV" runat="server" Text="Import CSV" OnClick="btn_ImportCSV_Click" />
<br />
<br />
<asp:Label ID="lbl_ErrorMsg" runat="server" Visible="false"></asp:Label>
<br />
<br />
<asp:GridView ID="gv_GridView" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</div>
</form>
</body>
reference :
http://forums.asp.net/t/1705264.aspx/1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web.SessionState;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.OleDb;
using System.Text;
namespace excel_upload
{
public partial class onlyupload : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlInputFile File1;
protected System.Web.UI.HtmlControls.HtmlInputButton Submit1;
DataTable dt = new DataTable();
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
}
//protected void btnimport_Click(object sender, EventArgs e)
//{
// string filePath = string.Empty;
// if (fu_ImportCSV.HasFile && fu_ImportCSV.PostedFile.ContentType.Equals("application/vnd.ms-excel"))
// {
// gv_GridView.DataSource = (DataTable)ReadToEnd(fu_ImportCSV.PostedFile.FileName);
// gv_GridView.DataBind();
// lbl_ErrorMsg.Visible = false;
// }
// else
// {
// lbl_ErrorMsg.Text = "Please check the selected file type";
// lbl_ErrorMsg.Visible = true;
// }
//}
private object ReadToEnd(string filePath)
{
DataTable dtDataSource = new DataTable();
string[] fileContent = File.ReadAllLines(filePath);
if (fileContent.Count() > 0)
{
//Create data table columns
string[] columns = fileContent[0].Split(',');
for (int i = 0; i < columns.Count(); i++)
{
dtDataSource.Columns.Add(columns[i]);
}
//Add row data
for (int i = 1; i < fileContent.Count(); i++)
{
string[] rowData = fileContent[i].Split(',');
dtDataSource.Rows.Add(rowData);
}
}
return dtDataSource;
}
protected void btn_ImportCSV_Click(object sender, EventArgs e)
{
string filePath = string.Empty;
if (fu_ImportCSV.HasFile && fu_ImportCSV.PostedFile.ContentType.Equals("application/vnd.ms-excel"))
{
DataTable dtCSVTable = (DataTable)ReadToEnd(fu_ImportCSV.PostedFile.FileName);
gv_GridView.DataSource = (DataTable)ReadToEnd(fu_ImportCSV.PostedFile.FileName);
gv_GridView.DataBind();
lbl_ErrorMsg.Visible = false;
}
else
{
lbl_ErrorMsg.Text = "Please check the selected file type";
lbl_ErrorMsg.Visible = true;
}
}
}
}
aspx page:
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="fu_ImportCSV" runat="server" />
<asp:Button ID="btn_ImportCSV" runat="server" Text="Import CSV" OnClick="btn_ImportCSV_Click" />
<br />
<br />
<asp:Label ID="lbl_ErrorMsg" runat="server" Visible="false"></asp:Label>
<br />
<br />
<asp:GridView ID="gv_GridView" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</div>
</form>
</body>
reference :
http://forums.asp.net/t/1705264.aspx/1
No comments:
Post a Comment