导出Excel (application方法)

using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.Excel;

 

public partial class _Default : System.Web.UI.Page
{
    bll bl = new bll();

    protected void Page_Load(object sender, EventArgs e)
    {
        Bind();
    }

 

    private void Bind()
    {
        Repeater1.DataSource = bl.GetView();
        Repeater1.DataBind();
    }


    public void OutputExcel(DataView dv, string str)
    {
        Excel.Range range;

        GC.Collect();
        Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
        int rowIndex = 2;
        int colIndex = 0;
        Excel._Workbook book;
        Excel._Worksheet sheet;

        book = app.Workbooks.Add(true);  //新建一张excel工作簿
        sheet = (Excel._Worksheet)book.ActiveSheet;

        //取得标题

        foreach (DataColumn col in dv.Table.Columns)
        {
            colIndex++;
            app.Cells[2, colIndex] = col.ColumnName;
            sheet.get_Range(app.Cells[2, colIndex], app.Cells[2, colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//标题居中显示
        }

        //取得数据
        foreach (DataRowView row in dv)
        {
            rowIndex++;
            colIndex = 0;
            foreach (DataColumn col in dv.Table.Columns)
            {
                colIndex++;
                range = sheet.get_Range(app.Cells[rowIndex, colIndex], app.Cells[rowIndex, colIndex]);

                if (col.DataType == System.Type.GetType("System.DateTime"))
                {
                    app.Cells[rowIndex, colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");
                }
                else
                {
                    app.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString();
                }
                range.HorizontalAlignment = XlVAlign.xlVAlignCenter;
                range.RowHeight = 80;
            }

        }
        int rowSum = rowIndex + 1;

        //取得整个报表的标题
        app.Cells[1, 1] = str;

        sheet.get_Range(app.Cells[1, 1], app.Cells[1, dv.Table.Columns.Count]).Merge(0);   //标题合并
        sheet.get_Range(app.Cells[1, 1], app.Cells[1, dv.Table.Columns.Count]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//标题居中


        app.Visible = true;
        book.SaveCopyAs(Server.MapPath("a.xlsx"));
        dv = null;
        book.Close(false,null,null);
        app.Quit();


        System.Runtime.InteropServices.Marshal.ReleaseComObject(book );
        System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
        book = null;
        app = null;
        sheet = null;
        GC.Collect();
        string path = Server.MapPath("a.xlsx");
        System.IO.FileInfo file = new System.IO.FileInfo(path);
        Response.Clear();
        Response.Charset = "GB2312";
        Response.ContentEncoding = System.Text.Encoding.UTF8;

        Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
        // 添加头信息,指定文件大小,让浏览器能够显示下载进度
        Response.AddHeader("Content-Length", file.Length.ToString());
        // 指定返回的是一个不能被客户端读取的流,必须被下载
        Response.ContentType = "application/ms-excel";
        // 把文件流发送到客户端
        Response.WriteFile(file.FullName);
        // 停止页面的执行
        Response.End();


    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        DataView dv = bl.GetView();
        OutputExcel(dv,"软件安全性报表");
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值