none
Exportar para excel sem preview com sub-relatorio RRS feed

  • Pergunta

  • Pessoal eu fiz eu adaptei uma rotina que vi aqui no forum para exportar um relatorio feito no report viewer sem precisar exibi-lo na tela.
    O meu codigo é o seguinte:
     protected void ImageButton4_Click(object sender, ImageClickEventArgs e)
        {
            
      
       {
           string reportPath = Server.MapPath("~/App_Relatorios/Rel_Teste.rdlc");
          
           ReportViewer rView = new ReportViewer();
          
           string stringconn = Convert.ToString(ConfigurationManager.ConnectionStrings["StringConMySql"]);
           MySqlConnection cb = new MySqlConnection(stringconn);
           DataTable Dt = new DataTable();
           MySqlCommand cmd = new MySqlCommand();
           cb.Open();
    
           cmd.Connection = cb;
           cmd.CommandType = CommandType.Text;
           cmd.CommandText = " Meu select ";
           cmd.Parameters.Add("?sequencia", MySqlDbType.Int16).Value = Session["sequencia"];
           cmd.Parameters.Add("?semestre", MySqlDbType.Int16).Value = Session["semestre"];
           cmd.ExecuteNonQuery();
           MySqlDataReader dr = cmd.ExecuteReader();
           Dt.Load(dr);
           cb.Close();
           
    
           //**********************************************************************************************************
           DataTable _DtU = new DataTable();
           MySqlCommand _cmdU = new MySqlCommand();
           cb.Open();
           rView.LocalReport.ReportPath = Server.MapPath("~/App_Relatorios/Rel_Teste.rdlc");
           
           _cmdU.Connection = cb;
           _cmdU.CommandType = CommandType.Text;
           _cmdU.CommandText = "SELECT responsavel FROM usuarios where sequencia = ?sequencia ";
    
           _cmdU.Parameters.Add("?sequencia", MySqlDbType.Int16).Value = Session["sequencia"];
           _cmdU.ExecuteNonQuery();
           MySqlDataReader _drU = _cmdU.ExecuteReader();
    
           _DtU.Load(_drU);
           cb.Close();
           //**********************************************************************************************************
           DataTable _DtM = new DataTable();
           MySqlCommand _cmdM = new MySqlCommand();
           cb.Open();
           rView.LocalReport.ReportPath = Server.MapPath("~/App_Relatorios/Rel_Teste.rdlc");
    
           _cmdM.Connection = cb;
           _cmdM.CommandType = CommandType.Text;
           _cmdM.CommandText = "Meu select";
           _cmdM.Parameters.Add("?sequencia", MySqlDbType.Int16).Value = Session["sequencia"];
           _cmdM.Parameters.Add("?semestre", MySqlDbType.Int16).Value = Session["semestre"];
           _cmdM.ExecuteNonQuery();
           MySqlDataReader _drM = _cmdM.ExecuteReader();
    
           _DtM.Load(_drM);
          cb.Close();
           //******************************************************************************************************************
           
           cb.Close();
          
           cb.Dispose();
          
          
            rView.LocalReport.DataSources.Add(new ReportDataSource("AnexoIX_CAPD", Dt));
           rView.LocalReport.DataSources.Add(new ReportDataSource("ANDs", _DtM));
           rView.LocalReport.DataSources.Add(new ReportDataSource("usuarios", _DtU));
          
          
          
           rView.LocalReport.ReportPath = reportPath;
          
           try {
              
               Warning[] warn = null;
              
               string[] streamids = null;
    
               string mimeType = "application/excel";
              
               string encoding = string.Empty;
              
               string extension = string.Empty;
              
               byte[] byteViewer = null;
    
               byteViewer = rView.LocalReport.Render("Excel", null, out mimeType, out encoding, out extension, out streamids,out warn);
              
               Response.Buffer = true;
              
               Response.Clear();
    
               Response.ContentType = "application/excel";
    
               Response.AddHeader("content-disposition", "inline; filename=ReportName.xls");
              
               Response.BinaryWrite(byteViewer);
              
               Response.Flush();
              
                  
               Response.End();
           }
           catch (Exception ex) {
              
          }
          finally {
             
              Dt.Dispose();
             
              Dt = null;
             
              rView.Dispose();
             
              rView = null;
             
                 
              Response.Clear();
          }
    Ele abre a tela para salvar o arquivo em excel e salva , mais o arquivo esta em branco sem nenhum dado!

    Alguem pode me dar alguma dica de como resolvo este problema?

    Obrigado.
    Danilo Molina
    quinta-feira, 29 de outubro de 2009 11:10

Todas as Respostas

  • Pessoal ninguem tem nenhuma ideia para me ajudar a resolver este problema ? pode ser um outro exemplo com exportação para o excel usando reporting viewer com sub-relatorios.


    Agradeço a todos!
    Danilo Molina
    sexta-feira, 30 de outubro de 2009 12:02