导出Excel (application方法)

来源:互联网 发布:手机淘宝优惠券怎么用? 编辑:程序博客网 时间:2024/05/17 22:03

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,"软件安全性报表");
    }
}

原创粉丝点击