导出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
    评论
要在 ASP.NET导出 Excel,您可以使用如下代码: ``` using System; using System.Data; using System.IO; using System.Web.UI; using Microsoft.Office.Interop.Excel; namespace ExportExcel { public partial class Export : Page { protected void Page_Load(object sender, EventArgs e) { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)), new DataColumn("Name", typeof(string)), new DataColumn("Country",typeof(string)) }); dt.Rows.Add(1, "John Hammond", "United States"); dt.Rows.Add(2, "Mudassar Khan", "India"); dt.Rows.Add(3, "Suzanne Mathews", "France"); dt.Rows.Add(4, "Robert Schidner", "Russia"); Application excel = new Application(); Workbook workbook = excel.Workbooks.Add(Type.Missing); Worksheet sheet = workbook.ActiveSheet; sheet.Name = "ExportedFromDatatable"; for (int i = 1; i < dt.Columns.Count + 1; i++) { sheet.Cells[1, i] = dt.Columns[i - 1].ColumnName; } for (int i = 0; i < dt.Rows.Count; i++) { for (int j = 0; j < dt.Columns.Count; j++) { sheet.Cells[i + 2, j + 1] = dt.Rows[i][j]; } } workbook.SaveAs(Response.OutputStream); Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; Response.AddHeader("content-disposition", "attachment; filename=ExportedFile.xlsx"); Response.Flush(); Response.End(); } } } ``` 请注意,上面的代码使用了 Microsoft.Office.Interop.Excel 库,因此您需要先安装该库。 这段代码会创建一个包含三列的 DataTable,分别为 Id、Name 和 Country,然后通过 Microsoft.Office.Interop.Excel 库创建 Excel 工作簿并将数据写入该工作簿。最后,通过 HTTP 响应将工作簿以 .xlsx 格式导出到客户端。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值