IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

ASP.NET Discussion :

[c#1.1]Commente exporter une dataView dans un fichier Excel?


Sujet :

ASP.NET

  1. #1
    Membre à l'essai
    Inscrit en
    Octobre 2004
    Messages
    28
    Détails du profil
    Informations forums :
    Inscription : Octobre 2004
    Messages : 28
    Points : 19
    Points
    19
    Par défaut [c#1.1]Commente exporter une dataView dans un fichier Excel?
    Slt,

    J'aimerais exporter une dataView dans un fichier excel. J'ai trouvé un exemple de code qui permet d'exporter une dataTable vers un fichier Excel. Je l'ai un peu modifié pour que ca marche avec une dataView mais malheureusement je n'arrive pas au resultat voulu.

    Voici le code
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
     
    // Export the details of specified columns
    try
    {
    	// Get the datatable to export
    	DataView myDv = (DataView)Session["MetaDataView"];
    	DataTable myDt = myDv.Table;
     
    	// Specify the column list to export
    	int[] iColumns = {0, 1, 2, 3, 4, 5, 6};
    	string[] sHeaders = { "Name", "Author", "Content-type", "Created Date", 
    					"Last Modified Date", "Containing Folder", "Containing Project" };
     
    	// Export the details of specified columns to Excel
    	RKLib.ExportData.Export objExport = new RKLib.ExportData.Export("Web");
    	objExport.ExportDetails(myDt, iColumns, sHeaders, RKLib.ExportData.Export.ExportFormat.Excel, excelFilename);
    }
    (exemple pris de http://www.codeproject.com/aspnet/Ex...assLibrary.asp)
    En fait a la base j'ai une dataTable qui est affiché dans une page web. Par l'intermedaire de cette page je peux appliquer un filtre, sur l'extension du fichier par exemple. Cette dataTable est affiché avec une datagrid dans ma page aspx, et je souhaiterai donc que ma fonction d'export vers un fichier Excel prenne en compte les resultats filtrés, et non systematiquement toute ma datable.

    Comment pourrais je faire cela?

    Merci d'avance
    a+

  2. #2
    Membre à l'essai
    Inscrit en
    Octobre 2004
    Messages
    28
    Détails du profil
    Informations forums :
    Inscription : Octobre 2004
    Messages : 28
    Points : 19
    Points
    19
    Par défaut
    J'ai trouve une autre methode pour faire l'export vers excel

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
     
    private void ExportToWord_Click(object sender, System.EventArgs e)
    {
     
    	// Get the filename selected by the user 
    	string wordFilename = Request.QueryString["filename"].ToString();
     
    	// Add the doc extension
    	wordFilename = wordFilename.Replace("xml", "doc");
     
    	Response.Clear();
    	Response.AddHeader("content-disposition", "attachment;filename=" + wordFilename);
    	Response.Charset = "";
    	Response.Cache.SetCacheability(HttpCacheability.NoCache);
    	Response.ContentType = "application/vnd.word";
     
    	System.IO.StringWriter stringWrite = new System.IO.StringWriter();
    	System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
     
     
    	this.MetadataDataGrid.RenderControl(htmlWrite);
     
    	Response.Write(stringWrite.ToString());
    	Response.End();
    }
    Cette fonction marche bcp mieux mais il y a un ptit probleme. Ma datagrid a l'attribut AllowSorting="true" d'activer, et lorsque j'execute cette fonction, j'ai ce messsage d'Erreur:

    Control 'MetadataDataGrid__ctl1__ctl0' of type 'DataGridLinkButton' must be placed inside a form tag with runat=server.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.Web.HttpException: Control 'MetadataDataGrid__ctl1__ctl0' of type 'DataGridLinkButton' must be placed inside a form tag with runat=server.
    En passant l'option AllowSorting a false, ca marche tres bien. Comment alors faire fonctionner cette finction avec le allowsorting a true dans la datagrid?

    Merci d'avance
    a+

  3. #3
    Membre à l'essai
    Inscrit en
    Octobre 2004
    Messages
    28
    Détails du profil
    Informations forums :
    Inscription : Octobre 2004
    Messages : 28
    Points : 19
    Points
    19
    Par défaut
    J'ai trouvé une solution pour regler mon probleme. Juste avant d'exporter ma datagrid, je desactive le allowsorting et je rebind la source de ma datagrid et ensuite ca marche.

    bon ce n'est pas tres propre mai j'arrive au resultat voulu. Si quelqu'un voit une meilleur idée, ca m'interesse.

    bye

  4. #4
    Membre éclairé
    Profil pro
    Inscrit en
    Juillet 2005
    Messages
    700
    Détails du profil
    Informations personnelles :
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Juillet 2005
    Messages : 700
    Points : 780
    Points
    780
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Control 'MetadataDataGrid__ctl1__ctl0' of type 'DataGridLinkButton' must be placed inside a form tag with runat=server
    Tu as ajouté le tag runat="server" dans la page aspx?

    (jai pas regardé tout ton post, c'est juste que ce messge m'interpelle)

  5. #5
    Membre à l'essai
    Inscrit en
    Octobre 2004
    Messages
    28
    Détails du profil
    Informations forums :
    Inscription : Octobre 2004
    Messages : 28
    Points : 19
    Points
    19
    Par défaut
    Ben ecoute jen ai mis un peu partout pour essayer mais ca change rien
    , et puis je nai pas d'element DataGridLinkButton dans ma page aspx

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
     
    <asp:DataGrid Id="MetadataDataGrid" AllowSorting="true" AutoGenerateColumns="False"
    OnSortCommand="SortDataGrid" AllowPaging="False" BorderColor="White" OnItemDataBound="DataGrid_ItemDataBound"
    CssClass="DetailPage_MetadataDataGrid_td" Runat="Server">
    	<AlternatingItemStyle CssClass="DetailPage_AlternatingItemStyle_item" />
    	<ItemStyle CssClass="DetailPage_ItemStyle_item" />
    	<HeaderStyle CssClass="DetailPage_HeaderStyle_item" />
    	<Columns>
    		<asp:TemplateColumn HeaderText="Name" SortExpression="Name">
    			<ItemTemplate>
    				<asp:HyperLink NavigateUrl='<%#DataBinder.Eval(Container.DataItem, "DetailURL")%>' Target="_blank" Text='<%#DataBinder.Eval(Container.DataItem, "Name")%>' Runat="server" />
    			</ItemTemplate>
    		</asp:TemplateColumn>
    		<asp:BoundColumn DataField="ContainingFolder" HeaderText="Containing Folder" SortExpression="ContainingFolder" />
    		<asp:BoundColumn DataField="ContainingProject" HeaderText="Containing Project" SortExpression="ContainingProject" />
    		<asp:BoundColumn DataField="Author" HeaderText="Author" SortExpression="Author" />
    		<asp:BoundColumn DataField="Content-type" HeaderText="Content-type" SortExpression="Content-type" />
    		<asp:BoundColumn DataField="CreatedDate" HeaderText="Created Date" SortExpression="CreatedDate" />
    		<asp:BoundColumn DataField="LastModifiedDate" HeaderText="Last Modified Date" SortExpression="LastModifiedDate" />
    	</Columns>
    </asp:DataGrid>

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [AC-2000] Comment exporter une table vers un fichier Excel ?
    Par kitou71 dans le forum Modélisation
    Réponses: 35
    Dernier message: 16/10/2009, 01h36
  2. Exporter une DataTable dans un fichier Excel déjà creer
    Par pools dans le forum Windows Forms
    Réponses: 13
    Dernier message: 09/06/2009, 09h12
  3. Réponses: 6
    Dernier message: 01/04/2009, 14h28
  4. comment exporter une table vers un fichier excel
    Par 21247692 dans le forum Développement
    Réponses: 3
    Dernier message: 27/02/2009, 14h44
  5. Exporter une table dans un fichier excel
    Par david71 dans le forum MS SQL Server
    Réponses: 5
    Dernier message: 07/09/2005, 17h09

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo