public void Insert(ArquivoExcel arquivoExcel) { string con = "Data Source=LAB-FANTASMA;Initial Catalog=ControleExcel;User ID=sa;Password=123"; SqlConnection connection = new SqlConnection(con); try { connection.Open(); SqlCommand command = new SqlCommand(string.Format("INSERT INTO Cad_arquivo_excel (Desc_arquivo,Extensao,Caminho,Data_Arquivo,StatusArquivo) VALUES ('{0}','{1}','{2}','{3}',{4})", arquivoExcel.desc_arquivo, arquivoExcel.extensao, arquivoExcel.caminho, arquivoExcel.data_arquivo.ToString("s"), arquivoExcel.statusArquivo.status_arquivo_id), connection); command.ExecuteNonQuery(); } catch (Exception ex) { throw ex; } finally { connection.Close(); } }
protected void bntUpload_Click(object sender, EventArgs e) { ArquivoExcelData arquivoExcelData = new ArquivoExcelData(); StatusArquivoData statusData = new StatusArquivoData(); if (FileUpload1.HasFile) { if (FileUpload1.FileContent.Length > 0) { Random random = new Random(); string FolderTemp; string Foldername; string Extension = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName); string filename = Path.GetFileName(FileUpload1.PostedFile.FileName.ToString()); if (Extension == ".XLS" || Extension == ".XLSX" || Extension == ".xls" || Extension == ".xlsx") { StatusArquivo arquivo = new StatusArquivo(); arquivo.status_arquivo_id = 1; var ArquivoStatus = statusData.Select(arquivo); FolderTemp = Server.MapPath("~/Temporario/"); Foldername = Server.MapPath("~/Arquivos/"); String filenameRandom = random.Next().ToString() + filename.ToString(); String caminho = FolderTemp + filenameRandom.ToString(); FileUpload1.PostedFile.SaveAs(caminho); String conStr = ""; switch (Extension) { case ".xls": //Excel 97-03 conStr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + FolderTemp + "//" + filenameRandom + ";" + "Extended Properties=Excel 8.0;"; break; case ".xlsx": //Excel 07 conStr = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + FolderTemp + "//" + filenameRandom + ";" + "Extended Properties=Excel 8.0;"; break; } OleDbConnection oconn = new OleDbConnection(conStr); //------ OleDbCommand ocmd = new OleDbCommand("select * from [Produtos$]", oconn); oconn.Open(); OleDbDataReader odr = ocmd.ExecuteReader(); List<Produto> listaProduto = new List<Produto>(); while (odr.Read()) { listaProduto.Add(new Produto { Produto_id = Convert.ToInt16(odr["Produto_id"].ToString()), Descricao_Produto = odr["Descricao_Produto"].ToString() }); } ArquivoExcel arquivoExcel = new ArquivoExcel(); arquivoExcel.desc_arquivo = filenameRandom; arquivoExcel.extensao = Extension; arquivoExcel.caminho = Foldername + filenameRandom; arquivoExcel.data_arquivo = DateTime.Now; arquivoExcel.statusArquivo = ArquivoStatus; System.IO.File.Copy(caminho, Foldername + "//" + filenameRandom); arquivoExcelData.Insert(arquivoExcel); lblmsg.Text = "Upload Excel File ......"; } else { lblmsg.Text = "Select only Excel File ....!!"; } } } }