Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. public JsonResult UploadExcel(HttpPostedFileBase FileUpload)
  2. {
  3.  
  4. List<string> data = new List<string>();
  5. if (FileUpload != null)
  6. {
  7. // tdata.ExecuteCommand("truncate table OtherCompanyAssets");
  8. if (FileUpload.ContentType == "application/vnd.ms-excel" || FileUpload.ContentType == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  9. {
  10. string filename = FileUpload.FileName;
  11. string targetpath = "C:/Users/70561/Documents";
  12. FileUpload.SaveAs(targetpath + filename);
  13. string pathToExcelFile = targetpath + filename;
  14. var connectionString = "";
  15. if (filename.EndsWith(".xls"))
  16. {
  17. connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; data source={0}; Extended Properties=Excel 8.0;", pathToExcelFile);
  18. }
  19. else if (filename.EndsWith(".xlsx"))
  20. {
  21. connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties="Excel 12.0 Xml;HDR=YES;IMEX=1";", pathToExcelFile);
  22. }
  23.  
  24. var adapter = new OleDbDataAdapter("SELECT * FROM [Planilha1$]", connectionString);
  25. var ds = new DataSet();
  26.  
  27. adapter.Fill(ds, "ExcelTable");
  28.  
  29. DataTable dtable = ds.Tables["ExcelTable"];
  30.  
  31. string sheetName = "Planilha1";
  32.  
  33. var excelFile = new ExcelQueryFactory(pathToExcelFile);
  34. var dados = from a in excelFile.Worksheet<RETORNO_CM>(sheetName) select a;
  35.  
  36. foreach (var a in dados)
  37. {
  38. try
  39. {
  40. if (a.CM_CODIGO != null && a.CM_QM_COMPONENTE_RMA != null && a.CM_NS != null && a.CM_DESCRICAO != null &&
  41. a.CM_DEFEITO != null && a.J_FALHA != null &&
  42. a.CM_TIPO_DEFEITO != null && a.J_PLACA_RETRABALHO != null &&
  43. a.J_PLACA_RESTESTADA != null && a.J_STATUS != null && a.CM_NOME_TESTE != null && a.CM_NOME_DEBUG != null)
  44. {
  45. RETORNO_CM CM = new RETORNO_CM();
  46. CM.CM_CODIGO = a.CM_CODIGO;
  47. CM.CM_QM_COMPONENTE_RMA = a.CM_QM_COMPONENTE_RMA;
  48. CM.CM_NS = a.CM_NS;
  49. CM.CM_DESCRICAO = a.CM_DESCRICAO;
  50. CM.CM_DATA_REPARO = a.CM_DATA_REPARO;
  51. CM.CM_DEFEITO = a.CM_DEFEITO;
  52. CM.J_FALHA = a.J_FALHA;
  53. CM.CM_TIPO_DEFEITO = a.CM_TIPO_DEFEITO;
  54. CM.CM_COMPONENTE = a.CM_COMPONENTE;
  55. CM.J_PLACA_RETRABALHO = a.J_PLACA_RETRABALHO;
  56. CM.J_PLACA_RESTESTADA = a.J_PLACA_RESTESTADA;
  57. CM.J_STATUS = a.J_STATUS;
  58. CM.CM_NOME_TESTE = a.CM_NOME_TESTE;
  59. CM.CM_NOME_DEBUG = a.CM_NOME_DEBUG;
  60. db.RETORNO_CM.Add(CM);
  61.  
  62. db.SaveChanges();
  63. }
  64. else
  65. {
  66. data.Add("<ul>");
  67.  
  68. data.Add("</ul>");
  69. data.ToArray();
  70. return Json(data, JsonRequestBehavior.AllowGet);
  71. }
  72. }
  73.  
  74. catch (DbEntityValidationException ex)
  75. {
  76. foreach (var entityValidationErrors in ex.EntityValidationErrors)
  77. {
  78.  
  79. foreach (var validationError in entityValidationErrors.ValidationErrors)
  80. {
  81. Response.Write("Property: " + validationError.PropertyName + " Error: " + validationError.ErrorMessage);
  82. }
  83. }
  84. }
  85. }
  86. //deleting excel file from folder
  87. if ((System.IO.File.Exists(pathToExcelFile)))
  88. {
  89. System.IO.File.Delete(pathToExcelFile);
  90. }
  91. data.Add("Enviado com sucesso");
  92. return Json(data, JsonRequestBehavior.AllowGet);
  93. }
  94. else
  95. {
  96. //alert message for invalid file format
  97. data.Add("Apenas arquivos excel sao suportados");
  98. return Json(data, JsonRequestBehavior.AllowGet);
  99. }
  100. }
  101. else
  102. {
  103. if (FileUpload == null) data.Add("Selecione um arquivo");
  104. return Json(data, JsonRequestBehavior.AllowGet);
  105. }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement