Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public JsonResult UploadExcel(HttpPostedFileBase FileUpload)
- {
- List<string> data = new List<string>();
- if (FileUpload != null)
- {
- // tdata.ExecuteCommand("truncate table OtherCompanyAssets");
- if (FileUpload.ContentType == "application/vnd.ms-excel" || FileUpload.ContentType == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
- {
- string filename = FileUpload.FileName;
- string targetpath = "C:/Users/70561/Documents";
- FileUpload.SaveAs(targetpath + filename);
- string pathToExcelFile = targetpath + filename;
- var connectionString = "";
- if (filename.EndsWith(".xls"))
- {
- connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; data source={0}; Extended Properties=Excel 8.0;", pathToExcelFile);
- }
- else if (filename.EndsWith(".xlsx"))
- {
- connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties="Excel 12.0 Xml;HDR=YES;IMEX=1";", pathToExcelFile);
- }
- var adapter = new OleDbDataAdapter("SELECT * FROM [Planilha1$]", connectionString);
- var ds = new DataSet();
- adapter.Fill(ds, "ExcelTable");
- DataTable dtable = ds.Tables["ExcelTable"];
- string sheetName = "Planilha1";
- var excelFile = new ExcelQueryFactory(pathToExcelFile);
- var dados = from a in excelFile.Worksheet<RETORNO_CM>(sheetName) select a;
- foreach (var a in dados)
- {
- try
- {
- if (a.CM_CODIGO != null && a.CM_QM_COMPONENTE_RMA != null && a.CM_NS != null && a.CM_DESCRICAO != null &&
- a.CM_DEFEITO != null && a.J_FALHA != null &&
- a.CM_TIPO_DEFEITO != null && a.J_PLACA_RETRABALHO != null &&
- a.J_PLACA_RESTESTADA != null && a.J_STATUS != null && a.CM_NOME_TESTE != null && a.CM_NOME_DEBUG != null)
- {
- RETORNO_CM CM = new RETORNO_CM();
- CM.CM_CODIGO = a.CM_CODIGO;
- CM.CM_QM_COMPONENTE_RMA = a.CM_QM_COMPONENTE_RMA;
- CM.CM_NS = a.CM_NS;
- CM.CM_DESCRICAO = a.CM_DESCRICAO;
- CM.CM_DATA_REPARO = a.CM_DATA_REPARO;
- CM.CM_DEFEITO = a.CM_DEFEITO;
- CM.J_FALHA = a.J_FALHA;
- CM.CM_TIPO_DEFEITO = a.CM_TIPO_DEFEITO;
- CM.CM_COMPONENTE = a.CM_COMPONENTE;
- CM.J_PLACA_RETRABALHO = a.J_PLACA_RETRABALHO;
- CM.J_PLACA_RESTESTADA = a.J_PLACA_RESTESTADA;
- CM.J_STATUS = a.J_STATUS;
- CM.CM_NOME_TESTE = a.CM_NOME_TESTE;
- CM.CM_NOME_DEBUG = a.CM_NOME_DEBUG;
- db.RETORNO_CM.Add(CM);
- db.SaveChanges();
- }
- else
- {
- data.Add("<ul>");
- data.Add("</ul>");
- data.ToArray();
- return Json(data, JsonRequestBehavior.AllowGet);
- }
- }
- catch (DbEntityValidationException ex)
- {
- foreach (var entityValidationErrors in ex.EntityValidationErrors)
- {
- foreach (var validationError in entityValidationErrors.ValidationErrors)
- {
- Response.Write("Property: " + validationError.PropertyName + " Error: " + validationError.ErrorMessage);
- }
- }
- }
- }
- //deleting excel file from folder
- if ((System.IO.File.Exists(pathToExcelFile)))
- {
- System.IO.File.Delete(pathToExcelFile);
- }
- data.Add("Enviado com sucesso");
- return Json(data, JsonRequestBehavior.AllowGet);
- }
- else
- {
- //alert message for invalid file format
- data.Add("Apenas arquivos excel sao suportados");
- return Json(data, JsonRequestBehavior.AllowGet);
- }
- }
- else
- {
- if (FileUpload == null) data.Add("Selecione um arquivo");
- return Json(data, JsonRequestBehavior.AllowGet);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement