Insert Data using Uploading Excel file and upload image and set water mark
Insert Data in data base with image and set the water mark in Image
Demo.aspx- <body>
- <form id="form1" runat="server">
- <div>
- <table>
- <tr>
- <td>
- <asp:FileUpload ID ="fu" runat ="server" />
- </td>
- <td>
- <asp:Button ID ="btnupload" runat ="server" Text="Upload File"
- onclick="btnupload_Click" />
- </td>
- </tr>
- </table>
- </div>
- </form>
- </body>
Demo.aspx .cs
- DataTable dt = new DataTable();
-
- protected void btnupload_Click(object sender, EventArgs e)
- {
- if (fu.HasFile)
- {
- if (fu.PostedFile.ContentType == "application/vnd.ms-excel" || fu.PostedFile.ContentType == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
- {
- string fileName = Path.Combine(Server.MapPath("~/localization"), Guid.NewGuid().ToString() + Path.GetExtension(fu.PostedFile.FileName));
- fu.PostedFile.SaveAs(fileName);
-
- string conString = "";
- string ext = Path.GetExtension(fu.PostedFile.FileName);
- if (ext.ToLower() == ".xls")
- {
- conString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
- }
- else if (ext.ToLower() == ".xlsx")
- {
- conString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
- }
- string query = "Select * from [Sheet1$]";
- OleDbConnection con = new OleDbConnection(conString);
- OleDbDataAdapter data = new OleDbDataAdapter(query, con);
- data.Fill(dt);
- int i = 0;
- //File_Upload file = new File_Upload();
- for (i = 0; i < dt.Rows.Count; i++)
- {
- string name = dt.Rows[i]["name"].ToString();
- string email = dt.Rows[i]["email"].ToString();
- string pass = dt.Rows[i]["Password"].ToString();
- string mobile = dt.Rows[i]["mobile"].ToString();
- string state = dt.Rows[i]["state"].ToString();
- string city = dt.Rows[i]["city"].ToString();
- string photo = dt.Rows[i]["Photo"].ToString();
-
- string[] pathArr = photo.Split('\\');
- string[] fileArr = pathArr.Last().Split('.');
- string fName = fileArr[0].ToString();
- Random rnd = new Random();
- string fn = fName + "_" + rnd.Next(111, 999) + "_" + rnd.Next(111, 999) + ".jpg";
- string path = "~/photo/" + fn;
- string pat = Server.MapPath(path);
- Wattermark w = new Wattermark();
- Dal odal = new Dal();
- System.Drawing.Image image = System.Drawing.Image.FromFile(photo);
- Graphics graphics = Graphics.FromImage(image);
- Font font = new Font("Times New Roman", 42.0f);
- PointF point = new PointF(10, 10);
- graphics.DrawString("Tusharsangani", font, Brushes.Red, point);
- image.Save(pat);
- odal.fetch("insert into Registeruser (name,E_id,password,mobile,state,city,photo) values('" + name + "','" + email + "','" + pass + "','" + mobile + "','" + state + "','" + city + "','" + path + "')");
- }
- }
- }
- else
- {
-
- }
- }