下载并导出数据到execl中

下载poi-3.6-20091214.jar,下载地址如下:

http://download.csdn.net/detail/evangel_z/3895051

1.jsp

<button type="button" class="btn btn-mini" onClick="location.href=‘<%=basePath%>/bankcard/exportEffectThirdData?begintime=${begintime}&endtime=${endtime}&page=1‘">导出有效订单</button>

2.后台代码处理:

controller 处理:

@Get("exportReturnThirdData")

public  void  exportReturnThirdListData(Invocation inv,@Param("begintime") String startTime,

@Param("endtime") String endTime){

HttpServletResponse response = inv.getResponse();

// response.setContentType("application/xls");

response.setContentType("application/x-download");

response.reset();

response.setContentType("bin");

String header = "attachment;filename=returnThirdData.xls";

response.addHeader("Content-Disposition", header);

String[] heads = { "订单日期", "订单号", "商品名称", "商品属性", "渠道名称", "支付银行卡","支付账号", "成本价", "卖出价", "卖出收入", "发货日期", "外订单审核日期", "退货日期", "取消日期" };

List<FenqiGoodsOrder> returnOrders = fenqiGoodsOrderListService

.exportReturnThirdList(startTime, endTime);

String path = "";

OrderDetailExportExeclUtil.exeportListData(heads, returnOrders,response);

}

导出到execl处理逻辑:

public static void  exeportListData(String[] heads,List<FenqiGoodsOrder> returnOrders,HttpServletResponse response){

// 第一步,创建一个webbook,对应一个Excel文件

HSSFWorkbook wb = new HSSFWorkbook();

// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet

HSSFSheet sheet = wb.createSheet("第三方退货订单明细");

// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short

HSSFRow row = sheet.createRow((int) 0);

// 第四步,创建单元格,并设置值表头 设置表头居中

HSSFCellStyle style = wb.createCellStyle();

style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

HSSFCell cell=null;

for(int h=0;h<heads.length;h++){

cell = row.createCell((short) h);

cell.setCellValue(heads[h]);

cell.setCellStyle(style);

}

// 第五步,写入实体数据 实际应用中这些数据从数据库得到,

for (int i = 0; i < returnOrders.size(); i++)

{

row = sheet.createRow((int) i + 1);

FenqiGoodsOrder goodsOrder = (FenqiGoodsOrder) returnOrders.get(i);

// 第四步,创建单元格,并设置值

row.createCell((short) 0).setCellValue(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(goodsOrder.getCreateTime()));

row.createCell((short) 1).setCellValue(goodsOrder.getOrderNo());

row.createCell((short) 2).setCellValue(goodsOrder.getGoodName());

row.createCell((short) 3).setCellValue(goodsOrder.getGoodType());

row.createCell((short) 4).setCellValue(goodsOrder.getChannelName());

row.createCell((short) 5).setCellValue(goodsOrder.getCardNo());

row.createCell((short) 6).setCellValue(goodsOrder.getAccountNo());

row.createCell((short) 7).setCellValue(goodsOrder.getPurchasePrice());//成本价

row.createCell((short) 8).setCellValue(goodsOrder.getSellPrice());//卖出价

row.createCell((short) 9).setCellValue(goodsOrder.getSellEarning());//卖出收入

if(goodsOrder.getDeliveryTime() !=null && !goodsOrder.getDeliveryTime().equals("")){

row.createCell((short) 10).setCellValue(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(goodsOrder.getDeliveryTime()));

}

if(goodsOrder.getAuditTime() !=null && !goodsOrder.getAuditTime().equals("")){

row.createCell((short) 11).setCellValue(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(goodsOrder.getAuditTime()));

}

if(goodsOrder.getReturnTime() !=null && !goodsOrder.getReturnTime().equals("")){

row.createCell((short) 12).setCellValue(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(goodsOrder.getReturnTime()));

}

if(goodsOrder.getCancelDate() !=null && !goodsOrder.getCancelDate().equals("")){

row.createCell((short) 13).setCellValue(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(goodsOrder.getCancelDate()));

}

}

// 第六步,将文件存到指定位置

try

{

wb.write(response.getOutputStream());

}

catch (Exception e)

{

e.printStackTrace();

}

}

http://www.cnblogs.com/bmbm/archive/2011/12/08/2342261.html

时间: 2024-08-29 11:28:40

下载并导出数据到execl中的相关文章

利用NPOI导出数据到Execl

相信很多童鞋都开发过Execl的导入导出功能,最近产品中无论是后台数据分析的需要,还是前端满足用户管理的方便,都有Execl导入导出的维护需求产生. 以前做这个功能,如果是web,利用HttpContext.Current.Response.ContentType ="application/ms-excel";就可以导出html数据表格到execl中,这种方法的问题就是编码格式的兼容性太差,用Mac OS之类的 office打开直接乱码给你看.或者是调用office的COM组件,或宏

struts2导出数据到excel中方法(模板方法)

现在的项目需要把用户反馈的信息表中的数据导出到excel的需求.之前做过类似的事情,但是时间已经久远,只能网上搜了一下,对于struts2,基本使用poi的方法,但是感觉网上的方法比较乱及不靠谱(没有开vpn 去google),就把之前的老项目用到的方法拿出来,整理如下,希望对大家有用.确实很简单(对于使用,里面的原理没有深入了解,可能我这边使用的是基本功能,如果需要细致研究,去apache下载jar包的时候,顺便把源码下载下来,这样就可以一目了然了.) 不多说,先从架构(使用步骤,从前端到后端

c# winform DataGridView导出数据到Excel中,可以导出当前页和全部数据

准备工作就是可以分页的DataGridView,和两个按钮,一个用来导出当前页数据到Excel,一个用来导出全部数据到Excel 没有使用SaveFileDialog,但却可以弹出保存对话框来 先做导出当前页数据到Excel的 DataGridView命名为dataGridView1 1 //按下导出按钮 2 private void button7_Click(object sender, EventArgs e) 3 { 4 print(dataGridView1); 5 } 6 publi

python中导出数据到execl

# 1.写一个函数,随便输入一个表名,把这个表里面所有的数据,导出到excel里面 # 1.'select * from %s' ,查出这个表所有的数据# 2.再把所有的数据写到excel xlwt import pymysql, hashlib, xlwt def op_mysql(sql: str): mysql_info = { 'host': '***.24.3.40', 'port': 3306, 'password': '****', 'user': 'jxz', 'db': 'jx

使用Linq导出数据到execl

/// <summary>    /// 绑定数据    /// </summary>    private void BindData()    { IQueryable<TableName> query = SelectData();        this.anpList.RecordCount = query.Count();        int curr_page_index = this.anpList.CurrentPageIndex;        i

C#不用COM组件导出数据到Excel中

<?xml version='1.0'?><?mso-application progid='Excel.Sheet'?><Workbook xmlns='urn:schemas-microsoft-com:office:spreadsheet' xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel' xmlns:ss='urn:sch

Scrapy基础(十二)————异步导出Item数据到Mysql中

异步导出数据到Mysql中 上次说过从Item中同步写入数据库,因为网络的下载速度和数据库的I/O速度是不一样的所以有可能会发生下载快,但是写入数据库速度慢,造成线程的堵塞:关于堵塞和非堵塞,同步和异步的关系,因为没接触过,所以也不是很好的理解,这里查看了些资料,已做记录 链接1:https://www.zhihu.com/question/19732473;http://wiki.jikexueyuan.com/project/twisted-intro/ 实现异步写入mysql数据库的思路:

db2导出数据并导入hive临时表中

操作: 1.从db2 中导出数据到txt中 2.修改文件中的分隔符为":" 3.在hive中新建表(建表时需要制定分隔符) 4.导入数据 -------- 1.从db2 中导出数据到txt中 db2 -x "select col1,col2,col3  from tbl_name where xxx with ur">filename.txt 2.修改文件中的分隔符为":" cat filename.txt | awk '{print $1

MySQL 导出数据

MySQL 导出数据 MySQL中你可以使用SELECT...INTO OUTFILE语句来简单的导出数据到文本文件上. 使用 SELECT ... INTO OUTFILE 语句导出数据 以下实例中我们将数据表 tutorials_tbl 数据导出到 /tmp/tutorials.txt 文件中: mysql> SELECT * FROM tutorials_tbl -> INTO OUTFILE '/tmp/tutorials.txt'; 你可以通过命令选项来设置数据输出的指定格式,以下实