• 大小: 5.82KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2020-12-26
  • 语言: C#
  • 标签: Excel  操作  c  

资源简介

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using Microsoft.Office.Interop.Excel;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Collections;


/// <summary>
/// ExportToExcel 的摘要说明
/// </summary>
public class ExportToExcel
{
    public ExportToExcel()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }

    private void KillExcelProcess()
    {
        System.Diagnostics.Process[] myProcesses;
        myProcesses = System.Diagnostics.Process.GetProcessesByName("EXCEL");

        foreach (System.Diagnostics.Process myProcess in myProcesses)
        {
            myProcess.Kill();

        }
    }



    /// <summary>
    /// 导出Excel
    /// </summary>
    /// <param name="dt">数据源</param>
    /// <param name="tblName">表名(</param>
    public void ToExcel(System.Data.DataTable dt,string tblName)
    {
        try
        {
            if (dt == null) return;

            System.Data.DataTable dtExcel = new System.Data.DataTable();
            dtExcel = dt.Copy();
            string Path = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath @"\UpLoad\" "tblName" ".xls");
            ExportToExcelFile.CreateExcelFile.CreateExcelDocument(dtExcel, Path);

            System.IO.FileInfo file = new System.IO.FileInfo(Path);
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Charset = "GB2312";
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
            // 添加头信息,为"文件下载/另存为"对话框指定默认文件名
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" HttpContext.Current.Server.UrlEncode(file.Name));
            // 添加头信息,指定文件大小,让浏览器能够显示下载进度
            HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
            // 指定返回的是一个不能被客户端读取的流,必须被下载
            HttpContext.Current.Response.ContentType = "application/ms-excel";
            // 把文件流发送到客户端
            HttpContext.Current.Response.WriteFile(file.FullName);

        }
        catch
        {
           // KillExcelProcess();
        }
    }


    /// <summary>
    /// 导出Excel
    /// </summary>
    /// <param name="dt">数据源</param>
    /// <param name="tblName">表名(</param>
    public void ToExcel(System.Data.DataSet ds, string tblName)
    {
        try
        {
            if (ds == null) return;

            System.Data.DataSet dsExcel = new System.Data.DataSet();
            dsExcel = ds.Copy();
            string Path = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath @"\UpLoad\" "tblName" ".xls");
            ExportToExcelFile.CreateExcelFile.CreateExcelDocument(dsExcel, Path);

            System.IO.FileInfo file = new System.IO.FileInfo(Path);
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Charset = "GB2312";
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
            // 添加头信息,为"文件下载/另存为"对话框指定默认文件名
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" HttpContext.Current.Server.UrlEncode(file.Name));
            // 添加头信息,指定文件大小,让浏览器能够显示下载进度
            HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
            // 指定返回的是一个不能被客户端读取的流,必须被下载
            HttpContext.Current.Response.ContentType = "application/ms-excel";
            // 把文件流发送到客户端
            HttpContext.Current.Response.WriteFile(file.FullName);



        }
        catch
        {
            // KillExcelProcess();
        }
    }


    public bool DataTableToExcel(System.Data.DataTable dt)
    {
        try
        {
            string FileName = HttpContext.Current.Server.MapPath("../Excel/") "催收案件记录报表_" dt.Rows[0][2].ToString() ".xls";
            System.Data.DataTable dtExcel = new System.Data.DataTable();
            dtExcel = dt.Copy();
            string Path = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath @"\UpLoad\" "催收案件记录报表"  ".xls");
            ExportToExcelFile.CreateExcelFile.CreateExcelDocument(dtExcel, Path);

            System.IO.FileInfo file = new System.IO.FileInfo(Path);
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Charset = "GB2312";
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
            // 添加头信息,为"文件下载/另存为"对话框指定默认文件名
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" HttpContext.Current.Server.UrlEncode(file.Name));
            // 添加头信息,指定文件大小,让浏览器能够显示下载进度
            HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
            // 指定返回的是一个不能被客户端读取的流,必须被下载
            HttpContext.Current.Response.ContentType = "application/ms-excel";
            // 把文件流发送到客户端
            HttpContext.Current.Response.WriteFile(file.FullName);


            return true;
        }
        catch
        {
            return false;
        }
    }


 
}

资源截图

代码片段和文件信息

// #define INCLUDE_WEB_FUNCTIONS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Data;
using System.Reflection;
using DocumentFormat.Openxml.Packaging;
using DocumentFormat.Openxml.Spreadsheet;
using DocumentFormat.Openxml;

namespace ExportToExcelFile
{
    //
    //  March 2014
    //  http://www.mikesknowledgebase.com
    //
    //  Note: if you plan to use this in an ASP.Net application remember to add a reference to “System.Web“ and to uncomment
    //  the “INCLUDE_WEB_FUNCTIONS“ definition at the top of this file.
    //
    //  Release history
    //   - Mar 2014: 
    //        Now writes the Excel data using the OpenxmlWriter classes which are much more memory efficient.
    

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件      17487  2015-06-29 13:00  CreateExcelFile.cs

     文件       6034  2017-10-16 13:54  ExportToExcel.cs

----------- ---------  ---------- -----  ----

                23521                    2


评论

共有 条评论