类对象导出excle

using System;
using System.Collections;
using System.Data;
using System.Data.OleDb;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Web;
using System.Text;
using System.Data.SqlClient;
using System.Drawing;
using System.Data.OleDb;
using Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;

 

 

 

 

 

 

            /// <summary>
            /// 类导出excel 调用微软自带Microsoft.Office.Interop.Excel接口
            /// </summary>
            /// <typeparam name="T">类对象</typeparam>
            /// <param name="list">类集合</param>
            /// <param name="notecolumn">类属性</param>
            /// <param name="titlecolumn">表头</param>
            /// <note>备注:表头与相对应属性必须一一对应,类属性必须完全与类中属性相同</note>

 

public void OutExcel<T>(List<T> list, string[] notecolumn, string[] titlecolumn)
            {
                //-----------初始化-----------------------
                string filename = "";
                Application app = null;
                Workbook wb = null;
                Worksheet sheet = null;
                //-----------------------------------
                try
                {
                    //---------------------------------------------------数据填充-------------------------------------------------------------
                    app = new Application();
                    app.Visible = false;
                    wb = (Workbook)app.Workbooks.Add(Missing.Value);
                    sheet = (Worksheet)wb.ActiveSheet;
                    for (int i = 0; i < titlecolumn.Length; i++)
                    {
                        sheet.Cells[1, 1 + i] = titlecolumn[i];
                        for (int j = 0; j < list.Count; j++)
                        {
                            if (notecolumn[i] != string.Empty)
                            {
                                sheet.Cells[j + 2, i+1] ="'" +list[j].GetType().GetProperty(notecolumn[i]).GetValue(list[j],null);
                            }
                        }
                    }
                    //-------------------------------------------------------------------------------------------------------------------
                    //--------------------------------------------------保存到服务器-----------------------------------------------------------------------
                    wb.Saved = true;
                    string filepath = HttpContext.Current.Server.MapPath("~") + "/Utility/files/TempExcel";
                    if (!Directory.Exists(filepath))
                    {
                        Directory.CreateDirectory(filepath);
                    }
                    filename = String.Format("{0}{1}.xls",filepath+"//", DateTime.Now.ToString("yyyyMMddHHmmssffff"));
                    app.ActiveWorkbook.SaveCopyAs(filename);
                    //------------------------------------------------------------------------------------------------------------------------------------
                }
                finally
                {
                    wb.Close(null, null, null);
                    app.Workbooks.Close();
                    app.Quit();
                    Marshal.ReleaseComObject((object)app);
                    Marshal.ReleaseComObject((object)wb);
                    Marshal.ReleaseComObject((object)sheet);
                    GC.Collect();
                }
                //-----------------------------------输出excel------------------------------------------------------------------------------------------------
                FileInfo fileInfo = new FileInfo(filename);
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.ClearContent();
                HttpContext.Current.Response.ClearHeaders();
                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename="+filename.Substring(filename.LastIndexOf("//")+1));
                HttpContext.Current.Response.AddHeader("Content-Length", fileInfo.Length.ToString());
                HttpContext.Current.Response.AddHeader("Content-Transfer-Encoding", "binary");
                HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
                HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
                HttpContext.Current.Response.WriteFile(fileInfo.FullName);
                HttpContext.Current.Response.Flush();
                HttpContext.Current.Response.End();
                //---------------------------------------------------------------------------------------------------------------------------------------------
            }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值