// --------------------------------------------------------- // Rama Krishna's Export class // Copyright (C) 2004 Rama Krishna. All rights reserved. // --------------------------------------------------------- # region Includes using System; using System.Data; using System.Web; using System.Web.SessionState; using System.IO; using System.Text; using System.Xml; using System.Xml.Xsl; using System.Threading;
# endregion // Includes namespace DMD.Web.Module { # region Summary
/**////<summary> /// Exports datatable to CSV or Excel format. /// This uses DataSet's XML features and XSLT for exporting. ///</summary> ///<example> /// 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.ExportFormat.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.ExportFormat.CSV, FileName) /// /// Catch Ex As Exception /// lblError.Text = Ex.Message /// End Try /// /// End Sub ///</example> # endregion // Summary publicclass Export { publicenum ExportFormat : int{CSV =1, Excel =2}; // Export format enumeration System.Web.HttpResponse response; privatestring appType;
public Export() { appType ="Web"; response = System.Web.HttpContext.Current.Response; }
public Export(string ApplicationType) { appType = ApplicationType; if(appType !="Web"&& appType !="Win") thrownew Exception("Provide valid application format (Web/Win)"); if (appType =="Web") response = System.Web.HttpContext.Current.Response; }
// 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 publicvoid ExportDetails(DataTable DetailsTable, ExportFormat FormatType, string FileName) { try { if(DetailsTable.Rows.Count ==0) thrownew Exception("There are no details to export.");
// 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 publicvoid ExportDetails(DataTable DetailsTable, int[] ColumnList, ExportFormat FormatType, string FileName) { try { if(DetailsTable.Rows.Count ==0) thrownew Exception("There are no details to export");
if(ColumnList.Length > dtExport.Columns.Count) thrownew Exception("ExportColumn List should not exceed Total Columns");
// Getting Field Names string[] sHeaders =newstring[ColumnList.Length]; string[] sFileds =newstring[ColumnList.Length];
for (int i=0; i < ColumnList.Length; i++) { if((ColumnList[i] <0) || (ColumnList[i] >= dtExport.Columns.Count)) thrownew Exception("ExportColumn Number should not exceed Total Columns Range");
// 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 publicvoid ExportDetails(DataTable DetailsTable, int[] ColumnList, string[] Headers, ExportFormat FormatType, string FileName) { try { if(DetailsTable.Rows.Count ==0) thrownew Exception("There are no details to export");
if(ColumnList.Length != Headers.Length) thrownew Exception("ExportColumn List and Headers List should be of same length"); elseif(ColumnList.Length > dtExport.Columns.Count || Headers.Length > dtExport.Columns.Count) thrownew Exception("ExportColumn List should not exceed Total Columns");
// Getting Field Names string[] sFileds =newstring[ColumnList.Length];
for (int i=0; i < ColumnList.Length; i++) { if((ColumnList[i] <0) || (ColumnList[i] >= dtExport.Columns.Count)) thrownew Exception("ExportColumn Number should not exceed Total Columns Range");
// XSLT to use for transforming this dataset. MemoryStream stream =new MemoryStream( ); XmlTextWriter writer =new XmlTextWriter(stream, Encoding.UTF8);