.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;
}
}
}
}

时间: 2024-11-03 21:56:41

.ashx datatable转excel的相关文章

ztree + ashx +DataTable +Oracle

好久没有使用ztree了,刚才在使用ztree做导航时遇到了几个小问题: 1.返回数据源是undefined . 2.数据出现后树结构没有出现(pIdKey单词拼写错误). 在使用Oracle查询时,Oracle将所有列名转化为大写,我在JSON处理过程中手动将字段处理成小写. js代码: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

C# Datatable导出Excel方法

C# 导出Excel方法  先引用下System.IO;System.data; 具体函数如下: 1 public static bool ExportCSV(DataTable dt, string fileNmae) 2 { 3 bool Msg = false; 4 string con = ""; 5 foreach (DataColumn dc in dt.Columns) 6 { 7 con += dc.ColumnName + ","; 8 } 9 c

使用Aspose将DataTable转Excel

0.准备工作 1.下载并引入Aspose.Cells 下载Aspose Cells并引入using Aspose.Cells 下面示例中用的是.net 3.0版本的Aspose Cells,编译环境VS2013 具体下载和引入方法见:http://www.cnblogs.com/moonache/p/4991459.html 1.使用Aspose将DataTable转为Excel 1.代码 下面代码用于将DataTable dt 转为Excel文件并存在path目录下 /// <summary>

【转】C# DataTable 导出 Excel 进阶 多行表头、合并单元格、中文文件名乱码

本文原创地址:http://blog.csdn.net/ranbolwb/article/details/8083983 ,转载请保留本行. 本例子是上一篇 DataTable 导出 Excel 的进阶,除了上一篇提到的处理乱码问题,本例还添加了处理多行表头.合并单元格的功能及处理中文文件名乱码问题,应该可以满足日常开发的需要了. 废话不多说了,直接上代码: [C#] 可以写单独类 1 using System; 2 using System.Collections.Generic; 3 usi

asp.net DataTable导出 excel的方法记录(第三方)

官网:http://npoi.codeplex.com/ 简单应用,主要是可以实现我们想要的简单效果,呵呵 需要引入dll,可以在官网下载,也可在下面下载 C#代码   protected void getExcel(DataTable dt) { NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook(); NPOI.SS.UserModel.Sheet sheet = book.CreateSh

DataTable 导出 excel , 可以自定义列名,没有繁琐的引用

/// <summary> /// 由DataTable导出Excel /// </summary> /// <param name="p"></param> /// <param name="dt"></param> /// <param name="fileName"></param> /// <param name="b"

C#中datatable导出excel(三种方法)

方法一:(拷贝直接可以使用,适合大批量资料, 上万笔)Microsoft.Office.Interop.Excel.Application appexcel = new Microsoft.Office.Interop.Excel.Application();SaveFileDialog savefiledialog = new SaveFileDialog();System.Reflection.Missing miss = System.Reflection.Missing.Value;ap

Excel导入DataTable兼容2003-2012(请细心查看注释)以及 DataTable导出Excel(导出格式2003.xls)注释:需要引用NPOI

1.#region Excel导入DataTable兼容2003-2012(请细心查看注释)/// <summary> /// 读取Excel文件到DataSet中/// 注释1:2012导出如报错“ System.InvalidOperationException: 未在本地计算机上注册“microsoft.ACE.oledb.12.0”提供程序.”解决:下载2007 Office system 驱动程序:数据连接组件安装http://download.microsoft.com/downl

ASP.Net的导出Excel的快速方法,DataTable导出Excel(亲测,非原创)

//使用方法 ExcelHelper.dataTableToCsv(dt,@"D:\1212.xls");System.Diagnostics.Process.Start(@"c:\1.xls"); //打开excel文件 public static void dataTableToCsv(DataTable table, string file) { string title = ""; FileStream fs = new FileStre