Excel导出cs文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Text;

public partial class ckgl_ck_ckqd : System.Web.UI.Page
{
    private static BLLV_Order bllv_order = new BLLV_Order();
    private static BLLOutbound bll_outbound = new BLLOutbound();
    private static BLLOrderDetial bll_orderdetial = new BLLOrderDetial();
    private static BLLV_OutboundDetial bllv_outbounddetial = new BLLV_OutboundDetial();
    private static BLLV_OutboundDetial_Count_Ext bll_outbounddetial_ext = new BLLV_OutboundDetial_Count_Ext();
    protected static BLLStorage bllstorage = new BLLStorage();
    protected BLLSpace bllspace = new BLLSpace();
    List<StorageModel> storagemodellist = bllstorage.SelectAll();
    private static BLLOrder bllorder = new BLLOrder();
    private static int PageSize = 10000;
    private static string Order_IDs = "";
    private static string Outbound_ID = "";
    private static V_OutboundDetialModel v_obdm = new V_OutboundDetialModel();
    private static BLLOperLog blloperlog = new BLLOperLog();

    //导出Excel文件
    protected void btn_Export_Click(object sender, EventArgs e)
    {
        Dictionary<string, string> dicspace = new Dictionary<string, string>();
        foreach (var item in storagemodellist)
        {
            SpaceModel sm = new SpaceModel();
            sm.Storage_ID = item.Storage_ID;
            List<SpaceModel> spacelist = bllspace.Select(sm, "1");
            foreach (var space in spacelist)
            {
                dicspace.Add(item.Storage_ID + space.Space_ID, space.Name);
            }
        }
        try
        {
            V_OutboundDetial_Count_ExtModel outbounddetial_ext_model = new V_OutboundDetial_Count_ExtModel();
            outbounddetial_ext_model.Outbound_ID = Outbound_ID;
            outbounddetial_ext_model.OutType = "0";
            List<V_OutboundDetial_Count_ExtModel> count_ext_models = bll_outbounddetial_ext.Select(outbounddetial_ext_model, "1");
            DataTable dt = new DataTable();
            DataColumn[] dc ={
                           new DataColumn("学习资料编号"),
                           new DataColumn("教学资料名称"),
                           new DataColumn("订购类型"),
                           new DataColumn("数量"),
                           new DataColumn("单价(元)"),
                           new DataColumn("库位"),
                           new DataColumn("仓位")
                       };
            dt.Columns.AddRange(dc);
            DataRow drHead = dt.NewRow();
            drHead["学习资料编号"] = "学习资料编号";
            drHead["教学资料名称"] = "教学资料名称";
            drHead["订购类型"] = "订购类型";
            drHead["数量"] = "数量";
            drHead["单价(元)"] = "单价(元)";
            drHead["库位"] = "库位";
            drHead["仓位"] = "仓位";
            dt.Rows.Add(drHead);
            //导入内容
            foreach (var model in count_ext_models)
            {
                DataRow dr = dt.NewRow();
                dr["学习资料编号"] = model.TMID;
                dr["教学资料名称"] = model.TMName;
                dr["订购类型"] = model.OrderName;
                dr["数量"] = model.SumCount;
                dr["单价(元)"] = model.Price;
                dr["库位"] = model.StorName;
                dr["仓位"] = dicspace[model.Storage_ID + model.Space_ID];
                dt.Rows.Add(dr);
            }
            ExportByWeb(dt, "出库清单.xls");
            //ExportExcel1.FileName = "出库清单.xls";
            //ExportExcel1.ExportToExcel(dt);
        }
        catch (Exception ex)
        {
            string strmessage = @"alert('" + ex.Message + "');";
            ClientScript.RegisterStartupScript(this.GetType(), "", strmessage, true);
        }
    }
 
    public static void ExportByWeb(DataTable dtSource, string strFileName)
    {
        try
        {
            HttpContext curContext = HttpContext.Current;

            // 设置编码和附件格式   
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = Encoding.UTF8;
            curContext.Response.Charset = "";
            curContext.Response.AppendHeader("Content-Disposition",
                "attachment;filename=" + HttpUtility.UrlEncode(strFileName, Encoding.UTF8));

            curContext.Response.BinaryWrite(Cau_TeachOrderLibrary.Common.RenderToExcel(dtSource).GetBuffer());
            curContext.Response.End();
        }
        catch (Exception ee)
        {
            throw new ApplicationException("导出过程出错!" + ee.Message);

        }

    }
}

posted @ 2014-02-18 17:25  明济安  阅读(547)  评论(0编辑  收藏  举报