导出Excel(.NET 代码)

4 篇文章 0 订阅
3 篇文章 0 订阅

需要引用的DLL文件:Aspose.Cells.dll,Aspose.Pdf.dll,Aspose.Words.dll,O2S.Components.PDFRender4NET.dll,Util.dll


管理系统,做导出功能的,可以用到


代码如下:

using System;

using System.Collections.Generic;

using System.Data;

using System.IO;

using System.Linq;

using System.Web;

 

/// <summary>

/// ImportExcel 的摘要说明

/// </summary>

public class ImportExcel

{

public ImportExcel()

{

//

// TODO: 在此处添加构造函数逻辑

//

}

    public static string Import(string StrWhere)

    {

      try

        {

            DataSet ds2 = huahaocms.com.DbHelper.DbHelperSQL.Query(StrWhere);

            if (ds2.Tables[0].Rows.Count == 0)

            {

                return "0";

            }

            Aspose.Cells.Workbook wkBook = new Aspose.Cells.Workbook();

            Aspose.Cells.Worksheet sheet = wkBook.Worksheets["Sheet1"];

            sheet.Cells.ImportDataTable(ds2.Tables[0], true, 0, 0, ds2.Tables[0].Rows.Count, ds2.Tables[0].Columns.Count, false, "yyyy-MM-dd", false);

            string _fileName = DateTime.Now.ToString("yyyyMMddHHmmssff") + "." + "xls"; //随机文件名

            //按日期归类保存

            string _datePath = DateTime.Now.ToString("yyyyMMdd") + "/";

            string filePath = "/upload/file/" + _datePath;

            //获得要保存的文件路径

            string serverFileName = filePath + _fileName;

            //物理完整路径                    

            string toFileFullPath = HttpContext.Current.Server.MapPath(filePath);

            //检查是否有该路径没有就创建

            if (!Directory.Exists(toFileFullPath))

            {

                Directory.CreateDirectory(toFileFullPath);

            }

            //将要保存的完整文件名                

            string toFile = toFileFullPath + _fileName;

            //导出保存

            wkBook.Save(toFile, Aspose.Cells.FileFormatType.Excel97To2003);

            //释放对象

            sheet = null;

            wkBook = null;

            return serverFileName;

        }

        catch (Exception ex)

        {

            return "1";

        }

}

}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值