Gravar arquivo Excel no Servidor
27/06/2013
0
Este tentando gravar um arquivo excel no servidor, em uma determinada pasta, mas não estou conseguindo concluir a tarefa, alguém tem alguma solução?
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" runat="server">
<asp:Literal ID="litErrorMessages" runat="server" EnableViewState="false"></asp:Literal>
<div class="panel rounded">
<ul class="form">
<li>
<label>
Upload de Arquivos</label>
</li>
<li>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" CssClass="bt rounded" Text="Upload Excel File"
OnClick="btnUpload_Click" />
</li>
</ul>
</div>
</asp:Content>
E em C#
protected void btnUpload_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
if (System.IO.Path.GetExtension(FileUpload1.FileName) == ".xls" || System.IO.Path.GetExtension(FileUpload1.FileName) == ".xlsx")
{
FileUpload1.SaveAs(Server.MapPath("~/upload/temp/Teste.xlsx"));
}
}
FileStream file = new FileStream("~/upload/temp/Teste.xlsx", FileMode.Open, FileAccess.Read);
HSSFWorkbook hssfworkbook = new HSSFWorkbook(file);
ISheet Contact = hssfworkbook.GetSheet("Contact");
for (int i = 1; i <= Contact.LastRowNum; i++)
{
string coluna1 = Contact.GetRow(i).GetCell(0).ToString();
string coluna2 = Contact.GetRow(i).GetCell(1).ToString();
string coluna3 = Contact.GetRow(i).GetCell(2).ToString();
}
}
O probema é que não consigo gravar, depois que utilizo o botão.
Marcos Pinto
Posts
27/06/2013
Fernanda Acacia
27/06/2013
Marcos Pinto
1º eu gravo o arquivo no servidor fisicamente:
if (FileUpload1.HasFile)
{
if (Path.GetExtension(FileUpload1.FileName) == ".xls" || Path.GetExtension(FileUpload1.FileName) == ".xlsx")
{
try
{
//Localmente
FileUpload1.SaveAs(@"C:\Users\Documents\Arquivos\" + "teste" + Path.GetExtension(FileUpload1.FileName));
}
catch
{
//Erro
}
}
}
2º faço um select no Excel e salvo as informações no banco SQL na tabela preparada para este excel.
using (OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["ExcelCon"].ConnectionString))
{
con.Open();
OleDbCommand com = new OleDbCommand("Select * from [EmployeeInfo$]", con);
OleDbDataReader dr = com.ExecuteReader();
using (SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Sql"].ConnectionString))
{
sqlcon.Open();
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlcon))
{
bulkCopy.ColumnMappings.Add("[Employee Name]", "EmpName");
bulkCopy.ColumnMappings.Add("Department", "Department");
bulkCopy.ColumnMappings.Add("Address", "Address");
bulkCopy.ColumnMappings.Add("Age", "Age");
bulkCopy.ColumnMappings.Add("Sex", "Sex");
bulkCopy.DestinationTableName = "Employees";
bulkCopy.WriteToServer(dr);
}
}
dr.Close();
dr.Dispose();
}
Response.Write("Upload Successfull!");
27/06/2013
Fernanda Acacia
if (FileUpload1.HasFile) { if (Path.GetExtension(FileUpload1.FileName) == ".xls" || Path.GetExtension(FileUpload1.FileName) == ".xlsx") { try { //Localmente FileUpload1.SaveAs(@"C:\Users\Documents\Arquivos\" + "teste" + Path.GetExtension(FileUpload1.FileName)); } catch { //Erro } } } 2º faço um select no Excel e salvo as informações no banco SQL na tabela preparada para este excel. using (OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["ExcelCon"].ConnectionString)) { con.Open(); OleDbCommand com = new OleDbCommand("Select * from [EmployeeInfo$]", con); OleDbDataReader dr = com.ExecuteReader(); using (SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Sql"].ConnectionString)) { sqlcon.Open(); using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlcon)) { bulkCopy.ColumnMappings.Add("[Employee Name]", "EmpName"); bulkCopy.ColumnMappings.Add("Department", "Department"); bulkCopy.ColumnMappings.Add("Address", "Address"); bulkCopy.ColumnMappings.Add("Age", "Age"); bulkCopy.ColumnMappings.Add("Sex", "Sex"); bulkCopy.DestinationTableName = "Employees"; bulkCopy.WriteToServer(dr); } } dr.Close(); dr.Dispose(); } Response.Write("Upload Successfull!");
27/06/2013
Marcos Pinto
Clique aqui para fazer login e interagir na Comunidade :)