Export.cs
Description du code
Export.cs est un fichier du projet BrolDev.Ce fichier est situé dans /var/www/bin/sniplets/bibliobrol/broldev/src/.
Projet BrolDev : Librairie de composants réutilisables pour les applications BrolDev en CSharp.
Code source ou contenu du fichier
Code c# (Export.cs) (402 lignes)
using System; using System.Data; using System.IO; using System.Text; using System.Threading; using System.Xml; using System.Xml.Xsl; using be.gaudry.model.enums; namespace be.gaudry.model { # region Summary /// <summary> /// Exports datatable to CSV or Excel format. /// This uses DataSet's XML features and XSLT for exporting. /// /// C#.Net Example to be used in WebForms /// ------------------------------------- /// using MyLib.ExportData; /// /// private void btnExport_Click(object sender, System.EventArgs e) /// { /// try /// { /// // Declarations /// DataSet dsUsers = ((DataSet) Session["dsUsers"]).Copy( ); /// MyLib.ExportData.Export oExport = new MyLib.ExportData.Export("Web"); /// string FileName = "UserList.csv"; /// int[] ColList = {2, 3, 4, 5, 6}; /// oExport.ExportDetails(dsUsers.Tables[0], ColList, Export.EXPORT_FORMAT.CSV, FileName); /// } /// catch(Exception Ex) /// { /// lblError.Text = Ex.Message; /// } /// } /// /// VB.Net Example to be used in WindowsForms /// ----------------------------------------- /// Imports MyLib.ExportData /// /// Private Sub btnExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) /// /// Try /// /// 'Declarations /// Dim dsUsers As DataSet = (CType(Session("dsUsers"), DataSet)).Copy() /// Dim oExport As New MyLib.ExportData.Export("Win") /// Dim FileName As String = "C:\\UserList.xls" /// Dim ColList() As Integer = New Integer() {2, 3, 4, 5, 6} /// oExport.ExportDetails(dsUsers.Tables(0), ColList, Export.EXPORT_FORMAT.CSV, FileName) /// /// Catch Ex As Exception /// lblError.Text = Ex.Message /// End Try /// /// End Sub /// /// </summary> # endregion // Summary public class Export { public enum APPLICATION_TYPE { Web, Win } System.Web.HttpResponse response; private APPLICATION_TYPE appType; public Export() { appType = APPLICATION_TYPE.Win; //response = System.Web.HttpContext.Current.Response; } public Export(APPLICATION_TYPE ApplicationType) { appType = ApplicationType; //if(appType != "Web" && appType != "Win") throw new Exception("Provide valid application format (Web/Win)"); if (appType == APPLICATION_TYPE.Web) response = System.Web.HttpContext.Current.Response; } #region ExportDetails OverLoad : Type#1 // Function : ExportDetails // Arguments : DetailsTable, FormatType, FileName // Purpose : To get all the column headers in the datatable and // exorts in CSV / Excel format with all columns public void ExportDetails(DataTable DetailsTable, BROL_EXPORT_FORMAT FormatType, string FileName) { try { if(DetailsTable.Rows.Count == 0) // Create Dataset DataTable dtExport = DetailsTable.Copy(); dtExport.TableName = "Values"; dsExport.Tables.Add(dtExport); // Getting Field Names for (int i=0; i < dtExport.Columns.Count; i++) { sHeaders[i] = dtExport.Columns[i].ColumnName; sFileds[i] = dtExport.Columns[i].ColumnName; } if (appType == APPLICATION_TYPE.Web) Export_with_XSLT_Web(dsExport, sHeaders, sFileds, FormatType, FileName); else if (appType == APPLICATION_TYPE.Win) Export_with_XSLT_Windows(dsExport, sHeaders, sFileds, FormatType, FileName); } catch(Exception Ex) { throw Ex; } } #endregion // ExportDetails OverLoad : Type#1 #region ExportDetails OverLoad : Type#2 // Function : ExportDetails // Arguments : DetailsTable, ColumnList, FormatType, FileName // Purpose : To get the specified column headers in the datatable and // exorts in CSV / Excel format with specified columns public void ExportDetails(DataTable DetailsTable, int[] ColumnList, BROL_EXPORT_FORMAT FormatType, string FileName) { try { if(DetailsTable.Rows.Count == 0) // Create Dataset DataTable dtExport = DetailsTable.Copy(); dtExport.TableName = "Values"; dsExport.Tables.Add(dtExport); if(ColumnList.Length > dtExport.Columns.Count) // Getting Field Names for (int i=0; i < ColumnList.Length; i++) { if((ColumnList[i] < 0) || (ColumnList[i] >= dtExport.Columns.Count)) sHeaders[i] = dtExport.Columns[ColumnList[i]].ColumnName; sFileds[i] = dtExport.Columns[ColumnList[i]].ColumnName; } if (appType == APPLICATION_TYPE.Web) Export_with_XSLT_Web(dsExport, sHeaders, sFileds, FormatType, FileName); else if (appType == APPLICATION_TYPE.Win) Export_with_XSLT_Windows(dsExport, sHeaders, sFileds, FormatType, FileName); } catch(Exception Ex) { throw Ex; } } #endregion // ExportDetails OverLoad : Type#2 #region ExportDetails OverLoad : Type#3 // Function : ExportDetails // Arguments : DetailsTable, ColumnList, Headers, FormatType, FileName // Purpose : To get the specified column headers in the datatable and // exorts in CSV / Excel format with specified columns and // with specified headers public void ExportDetails(DataTable DetailsTable, int[] ColumnList, string[] Headers, BROL_EXPORT_FORMAT FormatType, string FileName) { try { if(DetailsTable.Rows.Count == 0) // Create Dataset DataTable dtExport = DetailsTable.Copy(); dtExport.TableName = "Values"; dsExport.Tables.Add(dtExport); if(ColumnList.Length != Headers.Length) else if(ColumnList.Length > dtExport.Columns.Count || Headers.Length > dtExport.Columns.Count) // Getting Field Names for (int i=0; i < ColumnList.Length; i++) { if((ColumnList[i] < 0) || (ColumnList[i] >= dtExport.Columns.Count)) sFileds[i] = dtExport.Columns[ColumnList[i]].ColumnName; } if (appType == APPLICATION_TYPE.Web) Export_with_XSLT_Web(dsExport, Headers, sFileds, FormatType, FileName); else if (appType == APPLICATION_TYPE.Win) Export_with_XSLT_Windows(dsExport, Headers, sFileds, FormatType, FileName); } catch(Exception Ex) { throw Ex; } } #endregion // ExportDetails OverLoad : Type#3 #region Export_with_XSLT_Web // Function : Export_with_XSLT_Web // Arguments : dsExport, sHeaders, sFileds, FormatType, FileName // Purpose : Exports dataset into CSV / Excel format private void Export_with_XSLT_Web(DataSet dsExport, string[] sHeaders, string[] sFileds, BROL_EXPORT_FORMAT FormatType, string FileName) { try { // Appending Headers response.Clear(); response.Buffer= true; switch (FormatType) { case BROL_EXPORT_FORMAT.CSV: response.ContentType = "text/csv"; response.AppendHeader("content-disposition", "attachment; filename=" + FileName); break; case BROL_EXPORT_FORMAT.Excel: response.ContentType = "application/vnd.ms-excel"; response.AppendHeader("content-disposition", "attachment; filename=" + FileName); break; default: } // XSLT to use for transforming this dataset. CreateStylesheet(writer, sHeaders, sFileds, FormatType); writer.Flush( ); stream.Seek( 0, SeekOrigin.Begin); //Writeout the Content response.Write(sw.ToString()); sw.Close(); writer.Close(); stream.Close(); response.End(); } catch(ThreadAbortException Ex) { string ErrMsg = Ex.Message; } catch(Exception Ex) { throw Ex; } } #endregion // Export_with_XSLT #region Export_with_XSLT_Windows // Function : Export_with_XSLT_Windows // Arguments : dsExport, sHeaders, sFileds, FormatType, FileName // Purpose : Exports dataset into CSV / Excel format private void Export_with_XSLT_Windows(DataSet dsExport, string[] sHeaders, string[] sFileds, BROL_EXPORT_FORMAT FormatType, string FileName) { try { // XSLT to use for transforming this dataset. if(FormatType!=BROL_EXPORT_FORMAT.CSV && FormatType!=BROL_EXPORT_FORMAT.Excel) { } CreateStylesheet(writer, sHeaders, sFileds, FormatType); writer.Flush( ); stream.Seek( 0, SeekOrigin.Begin); xslTran.Transform(xmlDoc, null, sw, null); //Writeout the Content strwriter.WriteLine(sw.ToString()); strwriter.Close(); sw.Close(); writer.Close(); stream.Close(); } catch(Exception Ex) { throw Ex; } } #endregion // Export_with_XSLT #region CreateStylesheet // Function : WriteStylesheet // Arguments : writer, sHeaders, sFileds, FormatType // Purpose : Creates XSLT file to apply on dataset's XML file private void CreateStylesheet(XmlTextWriter writer, string[] sHeaders, string[] sFileds, BROL_EXPORT_FORMAT FormatType) { try { // xsl:stylesheet string ns = "http://www.w3.org/1999/XSL/Transform"; writer.Formatting = Formatting.Indented; writer.WriteStartDocument( ); writer.WriteStartElement("xsl","stylesheet",ns); writer.WriteAttributeString("version","1.0"); writer.WriteStartElement("xsl:output"); writer.WriteAttributeString("method","text"); writer.WriteAttributeString("version","4.0"); writer.WriteEndElement( ); // xsl-template writer.WriteStartElement("xsl:template"); writer.WriteAttributeString("match","/"); // xsl:value-of for headers for(int i=0; i< sHeaders.Length; i++) { writer.WriteString("\""); writer.WriteStartElement("xsl:value-of"); writer.WriteAttributeString("select", "'" + sHeaders[i].Replace("'"," ") + "'"); writer.WriteEndElement( ); // xsl:value-of writer.WriteString("\""); if (i != sFileds.Length - 1) writer.WriteString( (FormatType == BROL_EXPORT_FORMAT.CSV ) ? "," : " " ); } // xsl:for-each writer.WriteStartElement("xsl:for-each"); writer.WriteAttributeString("select","Export/Values"); writer.WriteString("\r\n"); // xsl:value-of for data fields for(int i=0; i< sFileds.Length; i++) { writer.WriteString("\""); writer.WriteStartElement("xsl:value-of"); writer.WriteAttributeString("select", sFileds[i]); writer.WriteEndElement( ); // xsl:value-of writer.WriteString("\""); if (i != sFileds.Length - 1) writer.WriteString( (FormatType == BROL_EXPORT_FORMAT.CSV ) ? "," : " " ); } writer.WriteEndElement( ); // xsl:for-each writer.WriteEndElement( ); // xsl-template writer.WriteEndElement( ); // xsl:stylesheet writer.WriteEndDocument( ); } catch(Exception Ex) { throw Ex; } } #endregion // WriteStylesheet } }
Structure et Fichiers du projet
Afficher/masquer...Icône | Nom | Taille | Modification |
Icône | Nom | Taille | Modification |
| _ | Répertoire parent | 0 octets | 1734903686 22/12/2024 22:41:26 |
| _ | config | 0 octets | 1541007188 31/10/2018 18:33:08 |
| _ | enums | 0 octets | 1541007189 31/10/2018 18:33:09 |
| _ | exceptions | 0 octets | 1541007189 31/10/2018 18:33:09 |
| _ | drawing | 0 octets | 1541007188 31/10/2018 18:33:08 |
| _ | win32 | 0 octets | 1541007190 31/10/2018 18:33:10 |
| _ | file | 0 octets | 1541007190 31/10/2018 18:33:10 |
| _ | comparators | 0 octets | 1541007188 31/10/2018 18:33:08 |
Utilisation de l'explorateur de code
- Navigation :
- Un clic sur une icône de répertoire ouvre ce répertoire pour en afficher les fichiers.
- Lorsque le répertoire en cours ne contient pas de sous-répertoires il est possible de remonter vers le répertoire parent.
- La structure de répertoires en treetable (tableau en forme d'arborescence) n'est plus possibledans cette version.
- Un clic sur une icône de fichier ouvre ce fichier pour en afficher le code avec la coloration syntaxique adaptée en fonction du langage principal utilisé dans le fichier.
- Affichage :
- Il est possible de trier les répertoires ou les fichiers selon certains critères (nom, taille, date).
- Actions :
- Les actions possible sur les fichiers dépendent de vos droits d'utilisateur sur le site. Veuillez activer le mode utilisateur pour activer les actions.
Nederlandse vertaling
U hebt gevraagd om deze site in het Nederlands te bezoeken. Voor nu wordt alleen de interface vertaald, maar nog niet alle inhoud.Als je me wilt helpen met vertalingen, is je bijdrage welkom. Het enige dat u hoeft te doen, is u op de site registreren en mij een bericht sturen waarin u wordt gevraagd om u toe te voegen aan de groep vertalers, zodat u de gewenste pagina's kunt vertalen. Een link onderaan elke vertaalde pagina geeft aan dat u de vertaler bent en heeft een link naar uw profiel.
Bij voorbaat dank.
Document heeft de 16/10/2009 gemaakt, de laatste keer de 26/10/2018 gewijzigd
Bron van het afgedrukte document:https://www.gaudry.be/nl/cs-broldev-source-rf-model//Export.cs.html
De infobrol is een persoonlijke site waarvan de inhoud uitsluitend mijn verantwoordelijkheid is. De tekst is beschikbaar onder CreativeCommons-licentie (BY-NC-SA). Meer info op de gebruiksvoorwaarden en de auteur.