• .ashx datatable转excel


    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Text;
    using System.Web;
    using System.IO;
    using Newtonsoft.Json;
    using System.Reflection;
    using System.Runtime.InteropServices;
    using NPOI.HSSF.UserModel;
    using NPOI.SS.UserModel;
    using NPOI.SS.Util;
    namespace Web.Handler
    {
    /// <summary>
    /// DriveOverviewExcelnew 的摘要说明
    /// </summary>
    public class DriveOverviewExcelnew : IHttpHandler
    {

    public void ProcessRequest(HttpContext context)
    {
    string strPath = createExcelFileByNPOI(context);
    // string strJson = "[{"result":"1","data":" + strPath + "}]";
    context.Response.ContentType = "text/plain";
    context.Response.Write(strPath);
    }

    public string createExcelFileByNPOI(HttpContext context)
    {
    HSSFWorkbook wk = new HSSFWorkbook();
    ISheet tb = wk.CreateSheet("汇总表");

    Dictionary<string, string> columNames = getColumnName();//列明

    System.Data.DataTable dt = getData(context);

    var data = (from t in dt.AsEnumerable()
    select new
    {
    CrmClient_ID = t["CrmClient_ID"],
    CrmArea_Name = t["CrmArea_Name"],
    CrmClient_SearchName = t["CrmClient_SearchName"],
    CrmClient_CluesTime = t["CrmClient_CluesTime"],
    CrmClient_Name = t["CrmClient_Name"],
    CrmTrade_Name = t["CrmTrade_Name"],
    CrmClient_Trench = t["CrmClient_Trench"],
    ProvinceName = t["ProvinceName"],
    CityName = t["CityName"],
    CrmClient_Address = t["CrmClient_Address"],
    CrmClientConactPersonName = t["CrmClientConactPersonName"],
    CrmClient_ProductName = t["CrmClient_ProductName"],
    CrmProductType_Name = t["CrmProductType_Name"],
    CrmLogisticsModel_Name = t["CrmLogisticsModel_Name"],
    CrmClient_IsTradeTop=t["CrmClient_IsTradeTop"],
    CrmClient_IsMatched = t["CrmClient_IsMatched"],
    CrmClient_IsCompanyTop = t["CrmClient_IsCompanyTop"],
    CrmClient_IsAreaTop = t["CrmClient_IsAreaTop"],
    CrmClient_IsProvinceTop = t["CrmClient_IsProvinceTop"],
    CrmOppportunity_Code = t["CrmOppportunity_Code"],
    CrmOppportunity_Name = t["CrmOppportunity_Name"],
    oppoChengquhao = t["oppoChengquhao"],
    CrmOppportunity_CreatedDateTime = t["CrmOppportunity_CreatedDateTime"],
    CrmOppportunity_Analysis = t["CrmOppportunity_Analysis"],
    CrmOppportunity_PurchaseDp = t["CrmOppportunity_PurchaseDp"],
    CrmOppportunityPersonName = t["CrmOppportunityPersonName"],
    CrmClient_LogisticsFees = t["CrmClient_LogisticsFees"],
    CrmOppportunity_Amount = t["CrmOppportunity_Amount"],
    CrmOppportunity_SignDate = t["CrmOppportunity_SignDate"],
    CrmOppportunity_ProblemsAndDeve = t["CrmOppportunity_ProblemsAndDeve"],
    CrmProject_Code = t["CrmProject_Code"],
    CrmProject_CreatorName = t["CrmProject_CreatorName"],
    CrmProjectClass_Name = t["CrmProjectClass_Name"],
    CrmProject_IsCrossArea = t["CrmProject_IsCrossArea"],
    CrmProject_EstimatedCost = t["CrmProject_EstimatedCost"],
    CrmProject_WillGetAmount = t["CrmProject_WillGetAmount"],
    CrmProject_EstimateStart = t["CrmProject_EstimateStart"],
    CrmProject_EstimateEnd = t["CrmProject_EstimateEnd"],
    CrmProject_TenderTime = t["CrmProject_TenderTime"],
    CrmProject_DescriptionOfWay = t["CrmProject_DescriptionOfWay"],
    CrmProject_ManagerName = t["CrmProject_ManagerName"],
    CrmProjectPersonName = t["CrmProjectPersonName"],
    lixiangfenxi = t["lixiangfenxi"],
    xiangmuqidong = t["xiangmuqidong"],
    xiangmufenxi = t["xiangmufenxi"],
    chanpinyufangan = t["chanpinyufangan"],
    toubiaoshangwu = t["toubiaoshangwu"],
    xiaoshouxianmu = t["xiaoshouxianmu"],
    xiangmujiaojie = t["xiangmujiaojie"],
    hetongjiaofu = t["hetongjiaofu"],
    gongsikaocha = t["gongsikaocha"],
    jiaoliuhuibao = t["jiaoliuhuibao"],
    gaocengbaifang = t["gaocengbaifang"],
    yangbandian = t["yangbandian"],
    CrmBid_Result = t["CrmBid_Result"],
    CrmBid_Amount = t["CrmBid_Amount"],
    bidhetongdate = t["bidhetongdate"]
    });
    int indexRowNumber = 0;
    int maxRow = 0;

    IRow row0 = tb.CreateRow(0);
    if (row0.Cells.Count <= 0) createCells(row0);
    tb.AddMergedRegion(new CellRangeAddress(0, 0, 0, 74));


    ICell cell = row0.GetCell(0);
    cell.SetCellValue("片区业务信息汇总表");

    IRow row1 = tb.CreateRow(1);
    if (row1.Cells.Count <= 0) createCells(row1);
    for (int xuhao = 0; xuhao < columNames.Keys.Count(); xuhao++)
    {
    //ICell cell = row1.CreateCell(xuhao);

    row1.Cells[xuhao].SetCellValue(columNames[columNames.Keys.ToArray()[xuhao]]);
    }

    int indexRow = 0;

    foreach (var indexClient in data)
    {
    indexRow++;
    if (indexRowNumber == 0)
    indexRowNumber++;
    else
    {
    indexRowNumber = indexRowNumber + maxRow + 1;
    maxRow = 0;
    }
    IRow rowN = tb.CreateRow(indexRowNumber + 1);
    if (rowN.Cells.Count <= 0) createCells(rowN);

    rowN.Cells[0].SetCellValue(indexRow.ToString());
    rowN.Cells[1].SetCellValue(indexClient.CrmArea_Name.ToString());
    rowN.Cells[2].SetCellValue(indexClient.CrmClient_SearchName.ToString());
    rowN.Cells[3].SetCellValue(indexClient.CrmClient_CluesTime.ToString());
    rowN.Cells[4].SetCellValue(indexClient.CrmClient_Name.ToString());
    rowN.Cells[5].SetCellValue(indexClient.CrmTrade_Name.ToString());
    rowN.Cells[6].SetCellValue(indexClient.CrmClient_Trench.ToString());
    rowN.Cells[7].SetCellValue(indexClient.ProvinceName.ToString());
    rowN.Cells[8].SetCellValue(indexClient.CityName.ToString());
    rowN.Cells[9].SetCellValue(indexClient.CrmClient_Address.ToString());
    rowN.Cells[10].SetCellValue(indexClient.CrmClientConactPersonName.ToString());
    rowN.Cells[11].SetCellValue(indexClient.CrmClient_ProductName.ToString());
    rowN.Cells[12].SetCellValue(indexClient.CrmProductType_Name.ToString());
    rowN.Cells[13].SetCellValue(indexClient.CrmLogisticsModel_Name.ToString());
    rowN.Cells[14].SetCellValue(indexClient.CrmClient_IsMatched.ToString());
    rowN.Cells[15].SetCellValue(indexClient.CrmClient_IsTradeTop.ToString());
    rowN.Cells[16].SetCellValue(indexClient.CrmClient_IsCompanyTop.ToString());
    rowN.Cells[17].SetCellValue(indexClient.CrmClient_IsAreaTop.ToString());
    rowN.Cells[18].SetCellValue(indexClient.CrmClient_IsProvinceTop.ToString());
    rowN.Cells[19].SetCellValue(indexClient.CrmOppportunity_Code.ToString());
    rowN.Cells[20].SetCellValue(indexClient.CrmOppportunity_Name.ToString());
    rowN.Cells[21].SetCellValue(indexClient.oppoChengquhao.ToString());
    rowN.Cells[22].SetCellValue(indexClient.CrmOppportunity_CreatedDateTime.ToString());
    rowN.Cells[23].SetCellValue(indexClient.CrmOppportunity_Analysis.ToString());
    rowN.Cells[24].SetCellValue(indexClient.CrmOppportunity_PurchaseDp.ToString());
    rowN.Cells[25].SetCellValue(indexClient.CrmOppportunityPersonName.ToString());
    rowN.Cells[26].SetCellValue(indexClient.CrmClient_LogisticsFees.ToString());
    rowN.Cells[27].SetCellValue(indexClient.CrmOppportunity_Amount.ToString());
    rowN.Cells[28].SetCellValue(indexClient.CrmOppportunity_SignDate.ToString());
    rowN.Cells[29].SetCellValue(indexClient.CrmOppportunity_ProblemsAndDeve.ToString());
    rowN.Cells[30].SetCellValue(indexClient.CrmProject_Code.ToString());
    rowN.Cells[31].SetCellValue(indexClient.CrmProject_CreatorName.ToString());
    rowN.Cells[32].SetCellValue(indexClient.CrmProjectClass_Name.ToString());
    rowN.Cells[33].SetCellValue(indexClient.CrmProject_IsCrossArea.ToString());
    rowN.Cells[34].SetCellValue(indexClient.CrmProject_EstimatedCost.ToString());
    rowN.Cells[35].SetCellValue(indexClient.CrmProject_WillGetAmount.ToString());
    rowN.Cells[36].SetCellValue(indexClient.CrmProject_EstimateStart.ToString());
    rowN.Cells[37].SetCellValue(indexClient.CrmProject_EstimateEnd.ToString());
    rowN.Cells[38].SetCellValue(indexClient.CrmProject_TenderTime.ToString());
    rowN.Cells[39].SetCellValue(indexClient.CrmProject_DescriptionOfWay.ToString());
    rowN.Cells[40].SetCellValue(indexClient.CrmProject_ManagerName.ToString());
    rowN.Cells[41].SetCellValue(indexClient.CrmProjectPersonName.ToString());
    rowN.Cells[42].SetCellValue(indexClient.xiangmuqidong.ToString());
    rowN.Cells[43].SetCellValue(indexClient.xiangmufenxi.ToString());
    rowN.Cells[44].SetCellValue(indexClient.chanpinyufangan.ToString());
    rowN.Cells[45].SetCellValue(indexClient.toubiaoshangwu.ToString());
    rowN.Cells[46].SetCellValue(indexClient.xiaoshouxianmu.ToString());
    rowN.Cells[47].SetCellValue(indexClient.xiangmujiaojie.ToString());
    rowN.Cells[48].SetCellValue(indexClient.hetongjiaofu.ToString());
    rowN.Cells[49].SetCellValue(indexClient.gongsikaocha.ToString());
    rowN.Cells[50].SetCellValue(indexClient.jiaoliuhuibao.ToString());
    rowN.Cells[51].SetCellValue(indexClient.gaocengbaifang.ToString());
    rowN.Cells[52].SetCellValue(indexClient.yangbandian.ToString());
    rowN.Cells[53].SetCellValue(indexClient.yangbandian.ToString());
    rowN.Cells[54].SetCellValue(indexClient.CrmBid_Result.ToString());
    rowN.Cells[55].SetCellValue(indexClient.CrmBid_Amount.ToString());
    rowN.Cells[56].SetCellValue(indexClient.bidhetongdate.ToString());
    }
    if (tb != null)
    {
    string ExcelFolder = "~/OutPutError/Opportunity/" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
    //string ExcelFolder = "~/OutPutError/Opportunity/bb.xls";
    string FilePath = context.Server.MapPath(ExcelFolder);
    if (!File.Exists(FilePath))
    {
    FileStream fs = File.Create(FilePath);
    wk.Write(fs);
    wk.Close();
    fs.Close();
    fs.Dispose();

    }
    return ExcelFolder;
    }

    return "";
    }
    /// <summary>
    /// 列名
    /// </summary>
    /// <returns></returns>
    private Dictionary<string, string> getColumnName()
    {

    Dictionary<string, string> nameList = new Dictionary<string, string>();
    nameList.Add("xuhao", "序号");
    nameList.Add("CrmArea_Name", "片区");
    nameList.Add("CrmClient_SearchName", "线索搜集人");
    nameList.Add("CrmClient_CluesTime", "线索获取时间");
    nameList.Add("CrmClient_Name", "客户全称");
    nameList.Add("CrmTrade_Name", "行业");
    nameList.Add("CrmClient_Trench", "渠道来源");
    nameList.Add("ProvinceName", "省");
    nameList.Add("CityName", "市");
    nameList.Add("CrmClient_Address", "客户地址");
    nameList.Add("CrmClientConactPersonName", "客户联系人");
    nameList.Add("CrmClient_ProductName", "产品名称");
    nameList.Add("CrmProductType_Name", "产品属性");
    nameList.Add("CrmLogisticsModel_Name", "物流模式");
    nameList.Add("CrmClient_LogisticsFees", "年物流费用(万)");
    nameList.Add("CrmClient_IsMatched", "与我司业务是否匹配");
    nameList.Add("CrmClient_IsTradeTop", "行业TOP");
    nameList.Add("CrmClient_IsCompanyTop", "公司TOP");
    nameList.Add("CrmClient_IsAreaTop", "片区TOP");
    nameList.Add("CrmClient_IsProvinceTop", "省级TOP");
    nameList.Add("CrmOppportunity_Code", "机会点编号");
    nameList.Add("CrmOppportunity_Name", "机会点名称");
    nameList.Add("oppoChengquhao", "机会点所属城市区号");
    nameList.Add("CrmOppportunity_CreatedDateTime", "机会点获取时间");
    nameList.Add("CrmOppportunity_Analysis", "机会点分析");
    nameList.Add("CrmOppportunity_PurchaseDp", "物流供应商采购部");
    nameList.Add("CrmOppportunityPersonName", "业务对接人及职位");
    nameList.Add("CrmOppportunity_Amount", "预估签单金额(万)");
    nameList.Add("CrmOppportunity_SignDate", "预计签约时间");
    nameList.Add("CrmOppportunity_ProblemsAndDeve", "进展与问题");
    nameList.Add("CrmProject_Code", "项目编号");
    nameList.Add("CrmProject_CreatorName", "项目申请人");
    nameList.Add("CrmProjectClass_Name", "项目级别");
    nameList.Add("CrmProject_IsCrossArea", "是否跨区域项目");
    nameList.Add("CrmProject_EstimatedCost", "客户物流费用预算(总)(万)");
    nameList.Add("CrmProject_WillGetAmount", "我司参与的物流费用预算(万)");
    nameList.Add("CrmProject_EstimateStart", "客户预算开始执行时间");
    nameList.Add("CrmProject_EstimateEnd", "客户预算结束执行时间");
    nameList.Add("CrmProject_TenderTime", "招标时间");
    nameList.Add("CrmProject_DescriptionOfWay", "线路划分描述");
    nameList.Add("CrmProject_ManagerName", "项目经理");
    nameList.Add("CrmProjectPersonName", "项目组成员");
    nameList.Add("lixiangfenxi", "立项分析会");//以下时间是同一个字段
    nameList.Add("xiangmuqidong", "项目启动会");
    nameList.Add("xiangmufenxi", "项目分析会");
    nameList.Add("chanpinyufangan", "产品与解决方案决策会");
    nameList.Add("toubiaoshangwu", "投标与商务决策会");
    nameList.Add("xiaoshouxianmu", "销售项目总结会");
    nameList.Add("xiangmujiaojie", "项目交接会");
    nameList.Add("hetongjiaofu", "合同交付总结会");
    nameList.Add("gongsikaocha", "公司考察");
    nameList.Add("jiaoliuhuibao", "交流汇报");
    nameList.Add("gaocengbaifang", "高层拜访");
    nameList.Add("zhanhui", "展会/论坛/活动邀请");
    nameList.Add("yangbandian", "样板点参观");
    nameList.Add("CrmBid_Result", "投标结果");
    nameList.Add("CrmBid_Amount", "合同金额(万)");
    nameList.Add("bidhetongdate", "合同期限");
    return nameList;
    }

    private System.Data.DataTable getData(HttpContext context)
    {

    BLL.CrmClient bll = new BLL.CrmClient();
    Hashtable hashtable_clientid = new Hashtable();
    for (int i = 0; i < context.Request.Form.Count; i++)
    {
    hashtable_clientid.Add(i, context.Request.Form[i].ToString());
    }

    StringBuilder builder = new StringBuilder();
    string fengefu = "";
    foreach (DictionaryEntry de in hashtable_clientid)
    {
    builder.Append(fengefu);
    builder.Append("'");
    builder.Append(de.Value.ToString());
    builder.Append("'");
    fengefu = ",";
    }

    return bll.GetOverviewDriveAllInformation(builder.ToString());
    }

    public IRow createCells(IRow ir)
    {
    for (int i = 0; i < 75; i++)
    {
    ir.CreateCell(i);
    }
    return ir;
    }
    public bool IsReusable
    {
    get
    {
    return false;
    }
    }
    }
    }

  • 相关阅读:
    libevent(十)bufferevent 2
    libevent(九)bufferevent
    maven本地库更新失败
    IDEA常用快捷键
    ELASTIC SEARCH 安装
    Hbase建模选择
    ElasticSearch关键概念
    Nginx+tomcat 负载均衡
    MapReduce (MRV1)设计理念与基本架构
    Kafka安装验证及其注意
  • 原文地址:https://www.cnblogs.com/zhang-wenbin/p/6027435.html
Copyright © 2020-2023  润新知