.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;
}
}
}
}
.ashx datatable转excel的更多相关文章
- ashx导出dataTable为Excel
一,datatable导出Excel,用户可以选择路径,方法如下: /// <summary> /// DataTable导出到Excel /// </summary> /// ...
- 使用Aspose将DataTable转Excel
0.准备工作 1.下载并引入Aspose.Cells 下载Aspose Cells并引入using Aspose.Cells 下面示例中用的是.net 3.0版本的Aspose Cells,编译环 ...
- asp.net DataTable导出Excel 自定义列名
1.添加引用NPOI.dll 2.cs文件头部添加 using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using System.IO; 3.代码如 ...
- DataTable to Excel(使用NPOI、EPPlus将数据表中的数据读取到excel格式内存中)
/// <summary> /// DataTable to Excel(将数据表中的数据读取到excel格式内存中) /// </summary> /// <param ...
- Datatable导出Excel
; IRow headerRow = sheet.CreateRow(); ; ; ; iRowIndex++; } ; i < icolIndex; i++) { sheet.AutoSize ...
- c# Datatable导出Excel
using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using System; using System.Collections.Generic; ...
- C# DataTable导出EXCEL后身份证、银行卡号等长数字信息显示乱码解决
在DataTable导出EXCEL后发现有些格式显示有问题,比如身份证.银行卡号等大于11位的数字显示为科学计数法.13681-1等 带中划线的两段数字显示为日期格式等. 处理方法如下: public ...
- datatable的excel导入,其中有关于datatables的很多参数设置
datatable的excel导入,其中有关于datatables的很多参数设置 http://www.cnblogs.com/liyuhuan/p/5633095.html
- 用NPOI从DataTable到Excel,向Excel模板填充数据
DataTable---->Excel,填充数据 private IWorkbook workbook = null; private ISheet sheet = null; private ...
随机推荐
- JAVA用途
在现今阶段,最有前途的开发语言当属Java,Java语言是跨平台的,Write Once,Run Anywhere是Java的一句口号,学Application编程,可以在计算机上写程序,学Apple ...
- 夺命雷公狗-----React_native---1---jdk的安装
目录结构如下 然后打开安装包 这样大致上都算是完成了,那么我们要在cmd窗口里面对她进行测试下... 比如用命令看下版本 java -version 然后开始配置环境变量: 然后创建一个"J ...
- Android 中如何计算 App 的启动时间?
(转载) 已知的两种方法貌似可以获取,但是感觉结果不准确:一种是,adb shell am start -w packagename/activity,这个可以得到两个值,ThisTime和Total ...
- Android中top命令字段含义
PID PR CPU% S #THR VSS RSS PCY UID Name 1 ...
- 响应式编程 Part.01 (Layout and MVVM)
内部技术交流会PPT Keywords Less Requirejs Mvvm Bootstrap kendo 下载完整PPT
- angularJS和jQuery的区别
问题: 假如我熟悉利用jQuery去开发客户端应用,那么我怎么上手angularjs,能否描述下所需要的模式转变,下面这些问题能够帮助你给出一个回答: 1.在设计客户端web应用的时候有什么区别,最大 ...
- C# 利用反射
.NET基础篇——反射的奥妙 C#获取实体类属性名和值 | 遍历类对象 c#通过反射获取类上的自定义特性 C#利用反射+特性实现简单的实体映射数据库操作类
- 非root用户 gcc安装
亲测 可以安装 过程并不复杂 但可能需要一些时间 认真一点 按照步骤 一定可以成功哒 其他版本可以将ftp.gnu.org/gnu/gcc/敲入浏览器,找到自己需要的文件:[安装过4.9.0:成功:用 ...
- github初体验
1.服务端 首先得有一个github账号 用户名和密码 2.客户端 github是服务端,要想在自己电脑上使用git我们还需要一个git客户端,我这里选用msysgit,这个只是提供了git的核心功能 ...
- JAVA join()方法
转自:http://www.open-open.com/lib/view/open1371741636171.html 一.为什么要用join()方法 在很多情况下,主线程生成并起动了子线程,如果子线 ...