Demo.aspx
  1. <body>
  2. <form id="form1" runat="server">
  3. <div>
  4. <table>
  5. <tr>
  6. <td>
  7. <asp:FileUpload ID ="fu" runat ="server" />
  8. </td>
  9. <td>
  10. <asp:Button ID ="btnupload" runat ="server" Text="Upload File"
  11. onclick="btnupload_Click" />
  12. </td>
  13. </tr>
  14. </table>
  15. </div>
  16. </form>
  17. </body>

Demo.aspx .cs
  1. DataTable dt = new DataTable();
  2. protected void btnupload_Click(object sender, EventArgs e)
  3. {
  4. if (fu.HasFile)
  5. {
  6. if (fu.PostedFile.ContentType == "application/vnd.ms-excel" || fu.PostedFile.ContentType == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  7. {
  8. string fileName = Path.Combine(Server.MapPath("~/localization"), Guid.NewGuid().ToString() + Path.GetExtension(fu.PostedFile.FileName));
  9. fu.PostedFile.SaveAs(fileName);
  10. string conString = "";
  11. string ext = Path.GetExtension(fu.PostedFile.FileName);
  12. if (ext.ToLower() == ".xls")
  13. {
  14. conString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
  15. }
  16. else if (ext.ToLower() == ".xlsx")
  17. {
  18. conString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
  19. }
  20. string query = "Select * from [Sheet1$]";
  21. OleDbConnection con = new OleDbConnection(conString);
  22. OleDbDataAdapter data = new OleDbDataAdapter(query, con);
  23. data.Fill(dt);
  24. int i = 0;
  25. //File_Upload file = new File_Upload();
  26. for (i = 0; i < dt.Rows.Count; i++)
  27. {
  28. string name = dt.Rows[i]["name"].ToString();
  29. string email = dt.Rows[i]["email"].ToString();
  30. string pass = dt.Rows[i]["Password"].ToString();
  31. string mobile = dt.Rows[i]["mobile"].ToString();
  32. string state = dt.Rows[i]["state"].ToString();
  33. string city = dt.Rows[i]["city"].ToString();
  34. string photo = dt.Rows[i]["Photo"].ToString();
  35. string[] pathArr = photo.Split('\\');
  36. string[] fileArr = pathArr.Last().Split('.');
  37. string fName = fileArr[0].ToString();
  38. Random rnd = new Random();
  39. string fn = fName + "_" + rnd.Next(111, 999) + "_" + rnd.Next(111, 999) + ".jpg";
  40. string path = "~/photo/" + fn;
  41. string pat = Server.MapPath(path);
  42. Wattermark w = new Wattermark();
  43. Dal odal = new Dal();
  44. System.Drawing.Image image = System.Drawing.Image.FromFile(photo);
  45. Graphics graphics = Graphics.FromImage(image);
  46. Font font = new Font("Times New Roman", 42.0f);
  47. PointF point = new PointF(10, 10);
  48. graphics.DrawString("Tusharsangani", font, Brushes.Red, point);
  49. image.Save(pat);
  50. odal.fetch("insert into Registeruser (name,E_id,password,mobile,state,city,photo) values('" + name + "','" + email + "','" + pass + "','" + mobile + "','" + state + "','" + city + "','" + path + "')");
  51. }
  52. }
  53. }
  54. else
  55. {
  56. }
  57. }