Exports datatable to CSV or Excel format

简介:

// ---------------------------------------------------------
// 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

    
public class Export
    
{        
        
public enum ExportFormat : int {CSV = 1, Excel = 2}// Export format enumeration
    
        System.Web.HttpResponse response;
        
private string appType;    
            
        
public Export()
        
{
            appType 
= "Web";
            response 
= System.Web.HttpContext.Current.Response;
        }


        
public Export(string ApplicationType)
        
{
            appType 
= ApplicationType;
            
if(appType != "Web" && appType != "Win"throw new Exception("Provide valid application format (Web/Win)");
            
if (appType == "Web") response = System.Web.HttpContext.Current.Response;
        }

        
        
ExportDetails OverLoad : Type#1 // ExportDetails OverLoad : Type#1

        
ExportDetails OverLoad : Type#2 // ExportDetails OverLoad : Type#2

        
ExportDetails OverLoad : Type#3 // ExportDetails OverLoad : Type#3

        
Export_with_XSLT_Web // Export_with_XSLT 

        
Export_with_XSLT_Windows // Export_with_XSLT 

        
CreateStylesheet // WriteStylesheet

    }


}

 

 

本文转自RubyPdf 的中文博客博客园博客,原文链接:http://www.cnblogs.com/hardrock/archive/2005/12/27/305970.html,如需转载请自行联系原作http://www.cnblogs.com/hardrock/archive/2006/05/17/402654.html

相关文章
csv和excel
【10月更文挑战第18天】csv和excel
179 5
C# | 导出DataGridView中的数据到Excel、CSV、TXT
从DataGridView中导出数据到Excel、CSV、TXT是开发中非常常见的一种需求。本文将讲解如何高效的完成对这三种格式的单向导出。 倘若直接写三种格式的导出必定会产生大量的重复代码,而从表中获取结构化数据的思路是基本一致的,因此有一个思路是先将DataGridView中的数据转换为DataTable对象,再进一步导出成我们的目标格式。 本文将介绍如何将DataGridView中的数据转换为DataTable格式,并提供将DataTable转换为Excel、CSV、TXT三种格式的例子。
717 0
C# | 导出DataGridView中的数据到Excel、CSV、TXT
phpexcel(二)导入excel文件
有导出。自然就有导入。功能多样化嘛。 我这里使用的是laravel5.8框架(框架老点,但是这玩意完全就是一个自定义方法,跟框架版本什么的没有关系)
78 1
把DataTable用Excel形式导出
把DataTable用Excel形式导出
125 0
EasyExcel读取文件的问题:Convert excel format exception.You can try specifying the ‘excelType‘ yourself
EasyExcel读取文件的问题:Convert excel format exception.You can try specifying the ‘excelType‘ yourself
pandas读excel类型文件报错: xlrd.biffh.XLRDError: Excel xlsx file; not supported
pandas读excel类型文件报错: xlrd.biffh.XLRDError: Excel xlsx file; not supported
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等