java导出数据到excel (可直接使用)

需要加的依赖

<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.0.1</version>
        </dependency>
        <dependency>
            <groupId>com.unboundid</groupId>
            <artifactId>unboundid-ldapsdk</artifactId>
        </dependency>

工具类具体实现

/**
 * @Title: service
 * @Package cn.wine.supplier.ms.business.util
 */
package cn.wine.supplier.ms.business.util;

import cn.wine.supplier.model.business.enums.OrderStatusEnum;
import cn.wine.supplier.model.business.enums.PurchaseCoinTypeEnum;
import cn.wine.supplier.model.business.vo.resp.BusinessResourceOrderRespVO;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;

import java.io.FileOutputStream;
import java.util.List;

/***********************************
 * @ClassName: ToExclUtils
 * @Description: TODO
 * @author: tangh
 * @createdAt: 2019年01月29日 10:51
 ***********************************/

public class ToExclUtils {
	public ToExclUtils() {
		
	}
	
	
	public static HSSFCell getCell(HSSFSheet sheet, int row, int col) {
		
		HSSFRow sheetRow = sheet.getRow(row);
		
		if (sheetRow == null){
			
			sheetRow = sheet.createRow(row);
			
		}
		
		HSSFCell cell = sheetRow.getCell(col);
		
		if (cell == null){
			
			cell = sheetRow.createCell(col);
			
		}
		
		return cell;
		
	}
	
	
	public static void setText(HSSFCell cell, String text) {
		
		cell.setCellType(CellType.STRING);
		
		cell.setCellValue(text);
		
	}
	
	
	public static void toExcel(List<String> titles, List<BusinessResourceOrderRespVO> list) throws Exception {
		
		HSSFWorkbook wb = new HSSFWorkbook(); // 定义一个新的工作簿
		
		HSSFSheet sheet = wb.createSheet("第一个Sheet页"); // 创建第一个Sheet页
		
		// 第四步,创建单元格,并设置值表头 设置表头居中
		
		HSSFCellStyle style = wb.createCellStyle();
		
		style.setAlignment(HorizontalAlignment.CENTER); // 创建一个居中格式
		
		HSSFRow row = sheet.createRow(0); // 创建一个行
		
		HSSFCell cell = row.createCell(0); // 创建一个单元格 第1列
		
		// cell.setCellValue(new Date()); // 给单元格设置值
		
		for (int i = 0; i < titles.size(); i++){ // 设置标题
			
			String title = titles.get(i);
			
			cell = getCell(sheet, 0, i);
			
			setText(cell, title);
			
			cell.setCellStyle(style);
			
		}
		
		for (int i = 0; i < list.size(); i++){
			
			BusinessResourceOrderRespVO vpd = list.get(i);
			
			for (int j = 0; j < titles.size(); j++){
				StringBuilder str = new StringBuilder();
				//订单编号
				if (j == 0){
					str.append(vpd.getOrderCode() != null ? vpd.getOrderCode() : "");
				}//下单时间
				else if (j == 1){
					str.append(BusinessLocalDateTimeUtils.timestampToLocalDateTime(vpd.getOrderPlaceTime()) != null ? BusinessLocalDateTimeUtils.timestampToLocalDateTime(vpd.getOrderPlaceTime()) : "");
				}//买家名字
				else if (j == 2){
					str.append(vpd.getSupplierName() != null ? vpd.getSupplierName() : "");
				}
				cell = getCell(sheet, i + 1, j);
				
				setText(cell, String.valueOf(str));
				
				cell.setCellStyle(style);
				
			}
			
		}
		
		FileOutputStream fileOut = new FileOutputStream("D://成员信息表.xls");
		
		wb.write(fileOut);
		
		fileOut.close();
		
		wb.close();
		
	}
	
}

方法调用

public void toExcel(BusinessResourceOrderManagementPageVo vo) {
		//查询所有数据
		SpringPageVO<BusinessResourceOrderRespVO> result=findResouceOrderByOwnerOrCode(vo);
		List<BusinessResourceOrderRespVO> orderRespVOS=result.getContent();
		List<String> title=Lists.newArrayList();
		title.add("订单编号");
		title.add("下单时间");
		
		try{
			ToExclUtils.toExcel(title,orderRespVOS);
		}
		catch (Exception e){
			e.printStackTrace();
			AssertUtils.isTrue(true, BusinessExceptionType.IS_NULL, "导出失败");
		}

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值