java分页数据导出excel

/**
	 * 订单导出(用于统计利润)
	 * @return
	 */
	public String orderExport() throws IOException{
		if (queryOrderList_currentPage == null || queryOrderList_currentPage <= 0) {
			queryOrderList_currentPage = 1;
		}
		OrderInfo order = new OrderInfo();
		if (!StringUtil.isNull(queryOrderList_orderStatus)) {
			order.setOrderStatus(queryOrderList_orderStatus);
		}
		if (!StringUtil.isNull(queryOrderList_orderCard)) {
			order.setOrderCard(queryOrderList_orderCard);
		}
		if (!StringUtil.isNull(queryOrderList_memberPhone)) {
			order.setMemberPhone(queryOrderList_memberPhone);
		}
		if (!StringUtil.isNull(queryOrderList_memberContactsPhone)) {
			order.setMemberContactsPhone(queryOrderList_memberContactsPhone);
		}
		if (queryOrderList_businessId != null && queryOrderList_businessId > 0) {
			order.setBusinessId(queryOrderList_businessId);
		}
		if (!StringUtil.isNull(queryOrderList_addressDesc)) {
			order.setAddressDesc(queryOrderList_addressDesc);
		}
		
		if (!StringUtil.isNull(queryOrderList_beginTime) && !StringUtil.isNull(queryOrderList_endTime)) {
			order.setBeginTime(queryOrderList_beginTime);
			order.setEndTime(queryOrderList_endTime);
		}
		
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			queryOrderList_pageBean = statisticsService.queryOrderListByCompanyConditions(order,queryOrderList_currentPage, queryOrderList_pageSize);
			int totalPage = queryOrderList_pageBean.getTotalPage();
			response.reset(); 
			SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy-MM-dd");
			String fname= "订单导出"+sdFormat.format(new Date());
			fname=new String(fname.getBytes("GBK"),"ISO_8859_1");//文件名称{避免出现乱码} 
			response.setHeader("Content-disposition", "attachment; filename="+fname+".csv");// 设定输出文件头
			response.setContentType("text/csv"); 
			response.setCharacterEncoding("UTF-8"); 
			OutputStream out = response.getOutputStream();
			String sep = ","; 
			//列名
			for(int column=0;column<CompanyConfig.ORDER_EXPORT_COLUMN.split(",").length;column++){
				out.write(CompanyConfig.ORDER_EXPORT_COLUMN.split(",")[column].getBytes()); 
				out.write(sep.getBytes()); 
			}
			//换行符
			out.write(System.getProperty("line.separator").getBytes());
			//数据
			OrderInfo newOrderInfo =new OrderInfo();
			if(queryOrderList_pageBean.getList()!=null && queryOrderList_pageBean.getList().size()>0){
				for (int i = 0; i < queryOrderList_pageBean.getList().size(); i++) {
					newOrderInfo=(OrderInfo)queryOrderList_pageBean.getList().get(i);
					getOut(out,newOrderInfo,sep);
				}
			}
			if(totalPage>1){
				for (int m = 2; m <= totalPage; m++) {
					queryOrderList_pageBean = statisticsService.queryOrderListByCompanyConditions(order, 
							queryOrderList_currentPage, queryOrderList_pageSize);
					List list =queryOrderList_pageBean.getList();
					if(list!=null && list.size()>0){
						for (int j = 0; j < list.size(); j++){
							OrderInfo nOrderInfo=(OrderInfo)list.get(j);
							getOut(out, nOrderInfo, sep);
						} 
					}
				}
			}
			out.flush(); 
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
        return null;
	}
	
	public void getOut(OutputStream out,OrderInfo newOrderInfo,String sep) throws IOException{
		if(!StringUtil.isNull(newOrderInfo.getOrderCard())){
			out.write(String.valueOf(newOrderInfo.getOrderCard()).getBytes()); 
		}else{
			out.write(String.valueOf("订单编号为空").getBytes()); 
		}
		out.write((sep).getBytes());
		if(!StringUtil.isNull(newOrderInfo.getMemberContactsPhone())){
			out.write(newOrderInfo.getMemberContactsPhone().getBytes()); 
		}else{
			out.write(String.valueOf("用户联系电话为空").getBytes());
		}
		out.write((sep).getBytes()); 
		if(newOrderInfo.getAddressDesc()!=null){
			out.write(newOrderInfo.getAddressDesc().getBytes());
		}else{
			out.write(String.valueOf("联系地址为空").getBytes());
		}
		out.write((sep).getBytes()); 
		if(newOrderInfo.getGenerateTime()!=null){
			out.write(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(newOrderInfo.getGenerateTime()).getBytes());
		}else{
			out.write(String.valueOf("下单时间为空").getBytes());
		}
		out.write(System.getProperty("line.separator").getBytes());
	}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,实现分页导出Excel通常涉及到两个主要步骤:数据分页和将数据写入Excel文件。Apache POI是一个常用库,用于处理Microsoft Office格式的文档,包括Excel。以下是一个简单的示例,展示了如何使用Apache POI进行分页导出Excel: 1. **数据分页**: - 首先,你需要有一个数据源,如列表或数据库查询结果。例如,如果你的数据在`List<YourDataModel>`中,可以计算每页的大小(比如10条记录)并使用`Collections`或数据库API来获取相应页的数据。 ```java List<YourDataModel> dataList = // 获取数据源 int pageSize = 10; int currentPage = 1; // 假设用户选择当前页 int startIndex = (currentPage - 1) * pageSize; List<YourDataModel> pageData = dataList.subList(startIndex, startIndex + pageSize); ``` 2. **创建Excel工作簿和工作表**: - 使用`HSSFWorkbook`创建一个新的Excel工作簿,`HSSFSheet`创建工作表。 ```java HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet("Sheet1"); ``` 3. **写入数据Excel**: - 使用`Row`对象表示行,`Cell`对象表示单元格,将分页数据写入Excel。 ```java Row headerRow = sheet.createRow(0); // 创建表头行 // 填充表头 for (YourDataModel.Field field : YourDataModel.Fields.values()) { Cell cell = headerRow.createCell(field.getIndex()); cell.setCellValue(field.getName()); } for (YourDataModel data : pageData) { Row row = sheet.createRow(sheet.getLastRowNum() + 1); // 创建新行 for (YourDataModel.Field field : data.getFieldValues()) { Cell cell = row.createCell(field.getIndex()); cell.setCellValue(field.getValue()); } } ``` 4. **保存Excel文件**: - 最后,将工作簿写入磁盘文件。 ```java try (FileOutputStream fileOut = new FileOutputStream("output.xlsx")) { workbook.write(fileOut); } catch (IOException e) { e.printStackTrace(); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值