Advertisement
Guest User

Untitled

a guest
May 24th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7.  
  8. using System.IO;
  9. using System.Data;
  10. using System.Data.OleDb;
  11.  
  12. using System.Data.SqlClient;
  13. using System.Data;
  14.  
  15. namespace WebApplication1
  16. {
  17. public partial class EmployeeImport : System.Web.UI.Page
  18. {
  19. public string GetDateTimeStampedFolderName()
  20. {
  21. return string.Format("{0:yyyy-MM-dd_hh-mm-ss-tt}", DateTime.Now);
  22. }
  23.  
  24. public void CreateSchemIni(string targetFolder, string fileName)
  25. {
  26. using (FileStream filestr = new FileStream(targetFolder + "/schema.ini", FileMode.Create, FileAccess.Write))
  27. {
  28. using (StreamWriter writer = new StreamWriter(filestr))
  29. {
  30. writer.WriteLine("[" + FileUpload1.FileName + "]");
  31. writer.WriteLine("ColNameHeader=True");
  32. writer.WriteLine("Format=CSVDelimited");
  33. writer.WriteLine("DateTimeFormat=dd-MMM-yy");
  34. writer.WriteLine("Col1=FirstName Text");
  35. writer.WriteLine("Col2=LastName Text");
  36. writer.WriteLine("Col3="Hire Date" Date");
  37. writer.Close();
  38. writer.Dispose();
  39. }
  40. filestr.Close();
  41. filestr.Dispose();
  42. }
  43. }
  44.  
  45. private void UploadAndImport()
  46. {
  47. if (FileUpload1.HasFile)
  48. {
  49. string targetFolder = Server.MapPath("~/Uploads/Employees/" + GetDateTimeStampedFolderName());
  50.  
  51. if (System.IO.Directory.Exists(targetFolder) == false)
  52. {
  53. System.IO.Directory.CreateDirectory(targetFolder);
  54. }
  55.  
  56. FileUpload1.SaveAs(Path.Combine(targetFolder, FileUpload1.FileName));
  57.  
  58. CreateSchemIni(targetFolder, FileUpload1.FileName);
  59.  
  60. string strSql = "SELECT * FROM [" + FileUpload1.FileName + "]";
  61. string strCSVConnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + targetFolder + ";" + "Extended Properties='text;HDR=YES;'";
  62. OleDbDataAdapter oleda = new OleDbDataAdapter(strSql, strCSVConnString);
  63. DataTable importData = new DataTable();
  64. oleda.Fill(importData);
  65.  
  66. GridView1.DataSource = importData;
  67. GridView1.DataBind();
  68. }
  69. }
  70.  
  71. protected void UploadButton_Click(object sender, EventArgs e)
  72. {
  73. if (FileUpload1.HasFile)
  74. {
  75. UploadAndImport();
  76. }
  77. }
  78. }
  79. }
  80.  
  81. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="EmployeeImport.aspx.cs" Inherits="WebApplication1.EmployeeImport" %>
  82.  
  83. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  84.  
  85. <html xmlns="http://www.w3.org/1999/xhtml">
  86. <head runat="server">
  87. <title></title>
  88. </head>
  89. <body>
  90. <form id="form1" runat="server">
  91. <div>
  92.  
  93. <asp:FileUpload ID="FileUpload1" runat="server" />
  94.  
  95. <br />
  96. <asp:Button ID="UploadButton" runat="server" Text="Upload"
  97. onclick="UploadButton_Click" />
  98. <asp:GridView ID="GridView1" runat="server">
  99. </asp:GridView>
  100.  
  101. </div>
  102. </form>
  103. </body>
  104. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement