Step 1: File Upload control
- <label class="control-label">
- Select an Excel File</label>
- <div class="controls">
- <asp:UpdatePanel ID="UpdatePanel2" runat="server">
- <ContentTemplate>
- <asp:FileUpload ID="FileUpload1" class="default" runat="server" accept=".xlsx,.xls,.csv" />
- <asp:Label ID="lbl" runat="server"></asp:Label>
- </ContentTemplate>
- </asp:UpdatePanel>
Step 2: Add GridView with excel sheet datafields
- <asp:Panel ID="Panel1" runat="server" ScrollBars="Both" Height="400px">
- <asp:UpdatePanel ID="UpdatePanel1" runat="server">
- <ContentTemplate>
- <div id="GHead">
- </div>
- <asp:GridView ID="gvExcelFile" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" class="table table-striped table-bordered table-hover" AutoGenerateColumns="False" AllowSorting="True" ShowHeader="true">
- <Columns>
-
- <asp:BoundField DataField="Date" DataFormatString="{0:dd/MM/yyyy}" HeaderText="Meeting Date" />
- <asp:BoundField DataFormatString="{0:HH:mm tt}" DataField="Meeting Time" HeaderText="Meeting Time" />
- <asp:BoundField DataField="Location" HeaderText="Location" />
- <asp:BoundField DataField="BDM Name" HeaderText="BDM Name" />
- <asp:BoundField DataField="Company Name" HeaderText="Company Name" />
- <asp:BoundField DataField="Customer Name" HeaderText="Customer Name" />
- <asp:BoundField DataField="Full Address" HeaderText="Full Address" Visible="" />
- <asp:BoundField DataField="Contact Number" HeaderText="Contact Number" Visible="" />
- <asp:BoundField DataField="Email ID" HeaderText="Email ID" Visible="" />
-
- </Columns>
- </asp:GridView>
- </ContentTemplate>
- </asp:UpdatePanel>
- </asp:Panel>
- <asp:Button ID="btnSubmit" class="btn blue" runat="server" ValidationGroup="save" Text="Save" OnClick="btnSubmit_Click" />
-->Grid for duplicate records
- <asp:GridView ID="GridView1" CellPadding="4" ForeColor="#333333" class="table table-striped table-bordered table-hover" runat="server" AutoGenerateColumns="False" >
- <Columns>
- <asp:TemplateField HeaderText="Designation_id" Visible="False">
- <EditItemTemplate>
- <asp:Label ID="lblEditDesignationid" runat="server" Text='<%# Bind("Dmr_id") %>'></asp:Label>
- </EditItemTemplate>
- <ItemTemplate>
- <asp:Label ID="lblDesignationid" runat="server" Text='<%# Bind("Dmr_id") %>'></asp:Label>
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="Meeting Date">
- <EditItemTemplate>
- <asp:TextBox ID="txtEditDesignationName" runat="server" Text='<%# Bind("Meeting_date") %>'></asp:TextBox>
- </EditItemTemplate>
- <ItemTemplate>
- <asp:Label ID="lblDesignationName" runat="server" Text='<%# Bind("Meeting_date") %>'></asp:Label>
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="Meeting Time">
- <EditItemTemplate>
- <asp:TextBox ID="txtEditDesignationName" runat="server" Text='<%# Bind("Meeting_time") %>'></asp:TextBox>
- </EditItemTemplate>
- <ItemTemplate>
- <asp:Label ID="lblDesignationName" runat="server" Text='<%# Bind("Meeting_time") %>'></asp:Label>
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="Location">
- <EditItemTemplate>
- <asp:TextBox ID="txtEditDesignationName" runat="server" Text='<%# Bind("Location") %>'></asp:TextBox>
- </EditItemTemplate>
- <ItemTemplate>
- <asp:Label ID="lblDesignationName" runat="server" Text='<%# Bind("Location") %>'></asp:Label>
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="BDM Name">
- <EditItemTemplate>
- <asp:TextBox ID="txtEditDesignationName" runat="server" Text='<%# Bind("User_id") %>'></asp:TextBox>
- </EditItemTemplate>
- <ItemTemplate>
- <asp:Label ID="lblDesignationName" runat="server" Text='<%# Bind("User_id") %>'></asp:Label>
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="Company Name">
- <EditItemTemplate>
- <asp:TextBox ID="txtEditDesignationName" runat="server" Text='<%# Bind("Company_name") %>'></asp:TextBox>
- </EditItemTemplate>
- <ItemTemplate>
- <asp:Label ID="lblDesignationName" runat="server" Text='<%# Bind("Company_name") %>'></asp:Label>
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="Customer Name">
- <EditItemTemplate>
- <asp:TextBox ID="txtEditDesignationName" runat="server" Text='<%# Bind("Cutomer_name") %>'></asp:TextBox>
- </EditItemTemplate>
- <ItemTemplate>
- <asp:Label ID="lblDesignationName" runat="server" Text='<%# Bind("Cutomer_name") %>'></asp:Label>
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="Address">
- <EditItemTemplate>
- <asp:TextBox ID="txtEditDesignationName" runat="server" Text='<%# Bind("Address") %>'></asp:TextBox>
- </EditItemTemplate>
- <ItemTemplate>
- <asp:Label ID="lblDesignationName" runat="server" Text='<%# Bind("Address") %>'></asp:Label>
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="Contact Number">
- <EditItemTemplate>
- <asp:TextBox ID="txtEditDesignationName" runat="server" Text='<%# Bind("Contact") %>'></asp:TextBox>
- </EditItemTemplate>
- <ItemTemplate>
- <asp:Label ID="lblDesignationName" runat="server" Text='<%# Bind("Contact") %>'></asp:Label>
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="Email ID">
- <EditItemTemplate>
- <asp:TextBox ID="txtEditDesignationName" runat="server" Text='<%# Bind("Email_id") %>'></asp:TextBox>
- </EditItemTemplate>
- <ItemTemplate>
- <asp:Label ID="lblDesignationName" runat="server" Text='<%# Bind("Email_id") %>'></asp:Label>
- </ItemTemplate>
- </asp:TemplateField>
-
- </Columns>
- </asp:GridView>
Step 3: Add Java Script to load excel sheet data into GridView
- <script type="text/javascript">
- function UploadFile(fileUpload)
- {
- if (fileUpload.value != '')
- {
- document.getElementById("<%=LinkButton1.ClientID %>").click();
- }
- }
- </script>
Step 4: Add LinkButton
- <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click"></asp:LinkButton>
C# Coding
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.IO;
- using System.Data.OleDb;
- using System.Drawing;
- using System.Globalization;
-
-
- public partial class Dmr_Excel: System.Web.UI.Page
- {
-
- clsConnection c1 = new clsConnection();
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
-
- FileUpload1.Attributes["onchange"] = "UploadFile(this)";
-
- }
- }
-
- DataTable dt_duplicate_row = new DataTable();
-
- private void FillDates()
- {
- DateTime CurrentDate = DateTime.Today;
- var lastDayOfMonth = DateTime.DaysInMonth(CurrentDate.Year, CurrentDate.Month);
- }
- private void Upload_data()
- {
-
-
- if (dt_duplicate_row.Columns.Count == 0)
- {
- dt_duplicate_row.Columns.Add("Date");
- dt_duplicate_row.Columns.Add("Meeting Time");
- dt_duplicate_row.Columns.Add("Location");
- dt_duplicate_row.Columns.Add("BDM Name");
- dt_duplicate_row.Columns.Add("Company Name");
- dt_duplicate_row.Columns.Add("Customer Name");
- dt_duplicate_row.Columns.Add("Full Address");
- dt_duplicate_row.Columns.Add("Contact Number");
- dt_duplicate_row.Columns.Add("Email Id");
-
- }
-
- foreach(GridViewRow grd in gvExcelFile.Rows)
- {
- string date = grd.Cells[1].Text;
- string Meet_date = grd.Cells[1].Text;
- string meeting_time = grd.Cells[2].Text;
- string location = grd.Cells[3].Text;
- string bdm_name = grd.Cells[4].Text;
- string Company_name = grd.Cells[5].Text;
- string Customer_name = grd.Cells[6].Text;
- string address = grd.Cells[7].Text;
- string contact_no = grd.Cells[8].Text;
- string email_id = grd.Cells[9].Text;
-
- int City_id = 1;
- string f_date = "";
-
-
- location = location.Replace("'", "~");
- bdm_name = bdm_name.Replace("'", "~");
- Customer_name = Customer_name.Replace("'", "~");
- Company_name = Company_name.Replace("'", "~");
- address = address.Replace("'", "~");
- email_id = email_id.Replace("'", "~");
- package = package.Replace("'", "~");
- remark = remark.Replace("'", "~");
- call_type = call_type.Replace("'", "~");
- visting_card = visting_card.Replace("'", "~");
-
- if (date == " " && Meet_date == " " && meeting_time == " " && location == " " && bdm_name == " " && Company_name == " " &&
- Customer_name == " " && address == " " && contact_no == " " && email_id == " " )
- {
- continue;
- }
-
-
- if (date == "NA" || date == "" || date == " " || date == null)
- {
- dt_duplicate_row.Rows.Add(Meet_date, meeting_time, location, bdm_name, Company_name, Customer_name, address, contact_no,
- email_id);
- continue;
- } else
- {
- Meet_date = Convert.ToDateTime(date).ToString("yyyy-MM-dd");
- }
- if (amount == "" || amount == " " || amount == null)
- {
- amount = "0";
- }
-
- string str = "Select * from dmr_login where Meeting_date='" + Meet_date + "' and Contact_no='" + contact_no + "' ";
- DataTable dt_check = c1.filldt(str);
- if (dt_check.Rows.Count > 0)
- {
- dt_duplicate_row.Rows.Add(Meet_date, meeting_time, location, bdm_name, Company_name, Customer_name, address, contact_no,
- email_id);
-
- } else
- {
- DateTime dts = Convert.ToDateTime(Meet_date);
- FillDays(dts);
- CultureInfo ci = new CultureInfo("en-US");
- string Month_name = dts.ToString("MMMM", ci);
- string Year_name = dts.ToString("yyyy");
- string dmr_insert = "Insert into dmr_login (User_id,Meeting_date,Meeting_time,Location,City_id,Company_name,Customer_name,Address,";
- dmr_insert += " Contact_no,Email_id,Record_month,Record_year) values (";
- dmr_insert += " " + ddluser.SelectedValue + ",'" + Meet_date + "','" + meeting_time + "','" + location + "'," + City_id + ",'" + Company_name + "','" + Customer_name + "',";
- dmr_insert += " '" + address + "','" + contact_no + "','" + email_id + "','" + DateTime.Now.ToString("yyyy-MM-dd") + "','" + Month_name + "','" + Year_name + "')";
- c1.IUD(dmr_insert);
- if (clsConnection.z == 1)
- {
- clsConnection.z = 0;
- }
-
-
- }
- if (dt_duplicate_row.Rows.Count > 0)
- {
-
- lblMsg.Text = "Record Saved Successfully";
- PanelMsg.Visible = true;
- lblErrorMsg.Text = "Duplicate Record Found";
- PanelErrorMsg.Visible = true;
- btnExportExcel.Visible = true;
- btnSubmit.Visible = false;
-
- }
- else
- {
- lblErrorMsg.Text = "Duplicate Record Found";
- PanelErrorMsg.Visible = false;
- }
- gvExcelFile.DataSource = dt_duplicate_row;
- clsVariable.dt_duplicate = dt_duplicate_row.Clone();
- clsVariable.dt_duplicate = dt_duplicate_row.Copy();
- gvExcelFile.DataBind();
- lblMsg.Text = "Record Saved Successfully";
- PanelMsg.Visible = true;
- }
-
- protected void SaveExcelData()
- {
- for (int i = 0; i < gvExcelFile.Rows.Count; i++)
- {
-
- GridViewRow row = gvExcelFile.Rows[i];
- CheckBox chkbox = (CheckBox) row.FindControl("chbView");
-
-
- string Date = row.Cells[1].Text;
- string Meet_date = "";
-
-
- if (Date == "NA" || Date == " " || Date == "")
- {
- continue;
- } else
- {
- Meet_date = Convert.ToDateTime(Date).ToString("dd-MM-yyyy");
- }
- string Meeting_time = row.Cells[2].Text;
- string Location = row.Cells[3].Text;
- if (Location == " ")
- {
- return;
- }
-
- string usrname = "select * from tbl_user where User_name='" + ddluser.SelectedValue + "'";
- DataTable user_name = c1.filldt(usrname);
- int b = 1;
- if (user_name.Rows.Count > 0)
- {
- b = Convert.ToInt32(user_name.Rows[0]["User_id"].ToString());
- }
-
- string Company_name = row.Cells[5].Text;
- string Customer = row.Cells[6].Text;
-
-
-
-
- string Address = row.Cells[7].Text;
- string Contact_no = row.Cells[8].Text;
- string Email_id = row.Cells[9].Text;
-
-
-
- string Package = row.Cells[10].Text;
- string Product = "select * from tbl_package where Package_name='" + Package + "'";
- DataTable package_name = c1.filldt(Product);
- int c = 1;
- if (package_name.Rows.Count > 0)
- {
- c = Convert.ToInt32(package_name.Rows[0]["Package_id"].ToString());
- }
-
-
- string Amount = row.Cells[11].Text;
- if (Amount == "NA" || Amount == " " || Amount == "")
- {
- continue;
- }
-
-
-
- string check = "select * from dmr_loginn where Meeting_date='" + Meet_date + "' and Contact='" + row.Cells[8].Text + "'";
- DataTable dt_check = c1.filldt(check);
-
- if (dt_check.Rows.Count > 0)
- {
-
-
- dt_duplicate_row.Columns.Add("Meeting Date");
- dt_duplicate_row.Columns.Add("Meeting Time");
- dt_duplicate_row.Columns.Add("Location");
- dt_duplicate_row.Columns.Add("BDM Name");
- dt_duplicate_row.Columns.Add("Company Name");
- dt_duplicate_row.Columns.Add("Customer Name");
- dt_duplicate_row.Columns.Add("Full Address");
- dt_duplicate_row.Columns.Add("Contact Number");
- dt_duplicate_row.Columns.Add("Email Id");
-
-
- dt_duplicate_row.Rows.Add(Meet_date, Meeting_time, Location, b, Company_name, Customer, Address, Contact_no, Email_id);
- gvExcelFile.DataSource = dt_duplicate_row;
-
- gvExcelFile.DataBind();
- btnExportExcel.Visible = true;
-
- }
-
-
- string dtr = "Insert into dmr_loginn(User_id,Meeting_date,Meeting_time,Location,User_id,Company_name,Cutomer_name,Address,Contact,Email_id,";
- dtr += " Package,Amount,Remark,Call_type,Follow_up_date,Telly_self,Visiting_card,branch_name) values";
- dtr += "('" + ddluser.SelectedValue + "', '" + Meet_date + "','" + Meeting_time + "','" + Location + "','" + b + "','" + Company_name + "','" + Customer + "','" + Address + "',";
- dtr += " '" + Contact_no + "','" + Email_id + "')";
- c1.IUD(dtr);
-
-
- if (clsConnection.z == 1)
- {
- clsConnection.z = 0;
- lblMsg.Text = "Record Saved Successfully";
- PanelMsg.Visible = true;
- PanelErrorMsg.Visible = false;
- }
-
- }
- }
- }
-
-
- protected void btnSubmit_Click(object sender, EventArgs e)
- {
- Upload_data();
-
- }
-
- private void Export_excel(DataTable dt)
- {
- Response.ClearContent();
- Response.Buffer = true;
- Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "DMR.xls"));
- Response.ContentType = "application/ms-excel";
-
- string str = string.Empty;
- foreach(DataColumn dtcol in dt.Columns)
- {
- Response.Write(str + dtcol.ColumnName);
- str = "\t";
- }
- Response.Write("\n");
- foreach(DataRow dr in dt.Rows)
- {
- str = "";
- for (int j = 0; j < dt.Columns.Count; j++)
- {
- Response.Write(str + Convert.ToString(dr[j]));
- str = "\t";
- }
- Response.Write("\n");
- }
- Response.End();
- }
- protected void btnExportExcel_Click(object sender, EventArgs e)
- {
- Export_excel(clsVariable.dt_duplicate);
-
- }
- protected void Button2_Click(object sender, EventArgs e)
- {
- Upload_data();
- }
- protected void LinkButton1_Click(object sender, EventArgs e)
- {
- if (FileUpload1.HasFile)
- {
- clsVariable.dt_duplicate.Rows.Clear();
- clsVariable.dt_duplicate.Columns.Clear();
-
- string ConStr = "";
-
-
- string ext = Path.GetExtension(FileUpload1.FileName).ToString();
-
- string path = Server.MapPath("~/TempExcel/DMR/" + FileUpload1.FileName);
-
- FileUpload1.SaveAs(path);
-
-
- if (ext.Trim() == ".xls") {
-
- ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
- } else if (ext.Trim() == ".xlsx") {
-
- ConStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
- }
-
- string query = "SELECT * FROM [Sheet1$]";
-
- OleDbConnection conn = new OleDbConnection(ConStr);
-
-
- if (conn.State == ConnectionState.Closed)
- {
- conn.Open();
- }
-
-
- OleDbCommand cmd = new OleDbCommand(query, conn);
-
- OleDbDataAdapter da = new OleDbDataAdapter(cmd);
- DataSet ds = new DataSet();
-
- da.Fill(ds);
-
- gvExcelFile.DataSource = ds.Tables[0];
-
- gvExcelFile.DataBind();
-
- conn.Close();
-
- gvExcelFile.Visible = true;
- lbl.Text = "";
- PanelMsg.Visible = false;
- PanelErrorMsg.Visible = false;
- btnExportExcel.Visible = false;
- btnSubmit.Visible = true;
- }
- }
- }