java poi XSSF导出excel代码分享

import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.PrintSetup;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFHyperlink;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class TranOrderExport {

	String sheetTitle;
	List<TranOrder> tranOrderList;
	Workbook workbook;
	Sheet sheet;
	Map<String, CellStyle> styles;

	public TranOrderExport(String sheetTitle, List<TranOrder> tranOrderList) {
		this.sheetTitle = sheetTitle;
		this.tranOrderList = tranOrderList;
		// 初始化表
		initSheet();
		// 初始化表头
		creatSheetHead();
		// 填充数据
		fillSheetData();
	}

	/**
	 * 初始化表
	 */
	private void initSheet() {
		workbook = new XSSFWorkbook();
		styles = createStyles(workbook);

		sheet = workbook.createSheet(sheetTitle);

		sheet.setDisplayGridlines(false);
		sheet.setPrintGridlines(false);
		sheet.setFitToPage(true);
		sheet.setHorizontallyCenter(true);
		sheet.createFreezePane(0, 2, 0, 2);
		PrintSetup printSetup = sheet.getPrintSetup();
		printSetup.setLandscape(true);
		// 初始化列宽
		sheet.setColumnWidth(0, 24 * 256);
		sheet.setColumnWidth(1, 12 * 256);
		sheet.setColumnWidth(2, 12 * 256);
		sheet.setColumnWidth(3, 12 * 256);
		sheet.setColumnWidth(4, 36 * 256);
		sheet.setColumnWidth(5, 12 * 256);
		sheet.setColumnWidth(6, 12 * 256);
		sheet.setColumnWidth(7, 12 * 256);
		sheet.setColumnWidth(8, 12 * 256);
		sheet.setColumnWidth(9, 24 * 256);
		sheet.setColumnWidth(10, 12 * 256);
		sheet.setColumnWidth(11, 12 * 256);
		sheet.setColumnWidth(12, 12 * 256);
		sheet.setColumnWidth(13, 12 * 256);
		sheet.setColumnWidth(14, 12 * 256);
		sheet.setColumnWidth(15, 12 * 256);
	}

	/**
	 * 初始表头
	 */
	private void creatSheetHead() {
		Row headRow_0 = sheet.createRow(0);
		headRow_0.setHeightInPoints(20);
		CellStyle headCellStyle = styles.get("head");
		Cell titleCell = headRow_0.createCell(0);
		titleCell.setCellValue(sheetTitle);
		CellStyle headTitleStyle = styles.get("headTitle");
		titleCell.setCellStyle(headTitleStyle);
		//sheet.addMergedRegion(CellRangeAddress.valueOf("$A$1:$AO$1"));
		// 表头一
		Row headRow_1 = sheet.createRow(1);
		int headCellIndex = 0;
		Cell headCell = headRow_1.createCell(headCellIndex++);
		headCell.setCellValue("下单时间");
		headCell.setCellStyle(headCellStyle);
		headCell = headRow_1.createCell(headCellIndex++);
		headCell.setCellValue("订单编号");
		headCell.setCellStyle(headCellStyle);
		headCell = headRow_1.createCell(headCellIndex++);
		headCell.setCellValue("订单状态");
		headCell.setCellStyle(headCellStyle);
		headCell = headRow_1.createCell(headCellIndex++);
		headCell.setCellValue("合同金额(元)");
		headCell.setCellStyle(headCellStyle);
		headCell = headRow_1.createCell(headCellIndex++);
		headCell.setCellValue("服务名称");
		headCell.setCellStyle(headCellStyle);
		headCell = headRow_1.createCell(headCellIndex++);
		headCell.setCellValue("服务售价");
		headCell.setCellStyle(headCellStyle);
		headCell = headRow_1.createCell(headCellIndex++);
		headCell.setCellValue("服务类目(一)");
		headCell.setCellStyle(headCellStyle);
		headCell = headRow_1.createCell(headCellIndex++);
		headCell.setCellValue("服务类目(二)");
		headCell.setCellStyle(headCellStyle);
		headCell = headRow_1.createCell(headCellIndex++);
		headCell.setCellValue("服务类目(三)");
		headCell.setCellStyle(headCellStyle);
		headCell = headRow_1.createCell(headCellIndex++);
		headCell.setCellValue("店铺名称");
		headCell.setCellStyle(headCellStyle);
		headCell = headRow_1.createCell(headCellIndex++);
		headCell.setCellValue("产品线");
		headCell.setCellStyle(headCellStyle);		
		headCell = headRow_1.createCell(headCellIndex++);
		headCell.setCellValue("会员等级");
		headCell.setCellStyle(headCellStyle);
		headCell = headRow_1.createCell(headCellIndex++);
		headCell.setCellValue("归属基地");
		headCell.setCellStyle(headCellStyle);
		headCell = headRow_1.createCell(headCellIndex++);
		headCell.setCellValue("雇主名称");
		headCell.setCellStyle(headCellStyle);
		headCell = headRow_1.createCell(headCellIndex++);
		headCell.setCellValue("雇主类型");
		headCell.setCellStyle(headCellStyle);
		headCell = headRow_1.createCell(headCellIndex++);
		headCell.setCellValue("订单标记");
		headCell.setCellStyle(headCellStyle);
	}

	/**
	 * 填充数据
	 */
	private void fillSheetData() {
		int rowNum = 2;
		CellStyle cellStyle = styles.get("cell");
		CellStyle urlStyle = styles.get("urlStyle");
		for (int i = 0, length = tranOrderList.size(); i < length; i++) {
		    TranOrder record = tranOrderList.get(i);
			Row row = sheet.createRow(rowNum++);
			row.setHeightInPoints(2 * sheet.getDefaultRowHeightInPoints());
			
			int cellIndex = 0;
			Cell itemCell = row.createCell(cellIndex++);
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			String orderTime = df.format(new Date(record.getOrderTime().getTime()));
			itemCell.setCellValue(fmtString(orderTime));
			itemCell.setCellStyle(cellStyle);
			itemCell = row.createCell(cellIndex++);
			itemCell.setCellValue(fmtString(record.getOrderNO()));
			itemCell.setCellStyle(cellStyle);
			itemCell = row.createCell(cellIndex++);
			itemCell.setCellValue(fmtString(record.getOrderStatus()));
			itemCell.setCellStyle(cellStyle);
			itemCell = row.createCell(cellIndex++);
			itemCell.setCellValue(record.getPrice());
			itemCell.setCellStyle(cellStyle);
			
			itemCell = row.createCell(cellIndex++);
			CreationHelper createHelper = workbook.getCreationHelper();
		    XSSFHyperlink  servieLink= (XSSFHyperlink) createHelper.createHyperlink(Hyperlink.LINK_URL);
		    servieLink.setAddress(record.getServiceUrl());//服务链接
		    itemCell.setHyperlink(servieLink);// 设置超链接
			itemCell.setCellValue(fmtString(record.getProName()));
			itemCell.setCellStyle(urlStyle);
			
			itemCell = row.createCell(cellIndex++);
			itemCell.setCellValue(record.getServicePrice());
			itemCell.setCellStyle(cellStyle);
			//服务类目
			String serviceType = record.getServiceType();
			String[] serviceTypes = serviceType.split("-");
			for (int jj = 0; jj < serviceTypes.length; jj++) {
			    if (jj>=3 ) {
			        break;
			    }		    
	            itemCell = row.createCell(cellIndex++);
	            itemCell.setCellValue(fmtString(serviceTypes[jj]));
	            itemCell.setCellStyle(cellStyle);
			}
			if (serviceTypes.length < 3) {
			    for (int jjj = 0; jjj < (3 - serviceTypes.length); jjj++) {	        
	                itemCell = row.createCell(cellIndex++);
	                itemCell.setCellValue("");
	                itemCell.setCellStyle(cellStyle);
			    }
			}
			//店铺
			itemCell = row.createCell(cellIndex++);
            XSSFHyperlink  shopLink= (XSSFHyperlink) createHelper.createHyperlink(Hyperlink.LINK_URL);
            shopLink.setAddress(record.getShopUrl());//店铺链接
            itemCell.setHyperlink(shopLink);// 设置超链接
			itemCell.setCellValue(fmtString(record.getSupName()));
			itemCell.setCellStyle(urlStyle);	
			
			itemCell = row.createCell(cellIndex++);
            itemCell.setCellValue(fmtString(record.getProLineName()));
            itemCell.setCellStyle(cellStyle);
            itemCell = row.createCell(cellIndex++);
            itemCell.setCellValue(fmtString(record.getVipName()));
            itemCell.setCellStyle(cellStyle);
            itemCell = row.createCell(cellIndex++);
            itemCell.setCellValue(fmtString(record.getBaseName()));
            itemCell.setCellStyle(cellStyle);
            itemCell = row.createCell(cellIndex++);
            itemCell.setCellValue(fmtString(record.getFullName()));
            itemCell.setCellStyle(cellStyle);
            itemCell = row.createCell(cellIndex++);
            itemCell.setCellValue(fmtString(record.getPersonOrder()));
            itemCell.setCellStyle(cellStyle);
            itemCell = row.createCell(cellIndex++);
            itemCell.setCellValue(fmtString(record.getOrderMark()));
            itemCell.setCellStyle(cellStyle);
		}
	}
	
	private String fmtString(String str) {
	    if (StringUtils.isBlank(str)) {
	        return "";
	    }
	    return str;
	}

	/**
	 * 单元格样式
	 * @param wb
	 * @return
	 */
	private Map<String, CellStyle> createStyles(Workbook wb) {
		Map<String, CellStyle> styles = new HashMap<String, CellStyle>();

		Font headFont = wb.createFont();
		headFont.setFontHeightInPoints((short) 12);
		headFont.setColor(IndexedColors.BLACK.getIndex());

		Font cellFont = wb.createFont();
		cellFont.setFontHeightInPoints((short) 10);
		cellFont.setColor(IndexedColors.BLACK.getIndex());

		CellStyle headCellStyle = createBorderedStyle(wb);
		headCellStyle.setWrapText(true);
		headCellStyle.setAlignment(CellStyle.ALIGN_CENTER);
		headCellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
		headCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT
				.getIndex());
		headCellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
		headCellStyle.setFont(cellFont);
		styles.put("head", headCellStyle);
		
		CellStyle headTitleStyle = createBorderedStyle(wb);
		headTitleStyle.setWrapText(true);
		headTitleStyle.setAlignment(CellStyle.ALIGN_LEFT);
		headTitleStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
		headTitleStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT
				.getIndex());
		headTitleStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
		headTitleStyle.setFont(headFont);
		styles.put("headTitle", headTitleStyle);

		CellStyle cellStyle = createBorderedStyle(wb);
		cellStyle.setWrapText(true);
		cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
		cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
		cellStyle.setFont(cellFont);
		styles.put("cell", cellStyle);
		
		CellStyle urlStyle = createBorderedStyle(wb);
		Font urlFont = wb.createFont();
		urlFont.setColor(IndexedColors.BLUE.getIndex());
		urlFont.setUnderline(HSSFFont.U_SINGLE);
		urlStyle.setFont(urlFont);
        //cellStyle.setWrapText(true);
        cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
        cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
        styles.put("urlStyle", urlStyle);

		return styles;
	}

	private CellStyle createBorderedStyle(Workbook wb) {
		CellStyle style = wb.createCellStyle();
		style.setBorderRight(CellStyle.BORDER_THIN);
		style.setRightBorderColor(IndexedColors.BLACK.getIndex());
		style.setBorderBottom(CellStyle.BORDER_THIN);
		style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
		style.setBorderLeft(CellStyle.BORDER_THIN);
		style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
		style.setBorderTop(CellStyle.BORDER_THIN);
		style.setTopBorderColor(IndexedColors.BLACK.getIndex());
		return style;
	}

	public void export(HttpServletResponse response) throws IOException {
		response.setContentType("application/vnd.ms-excel");
		String excelFileName = sheetTitle + ".xlsx";
		response.addHeader("Content-Disposition", "attachment;filename="
				+ new String(excelFileName.getBytes("GB2312"), "iso8859-1"));
		OutputStream ouputStream = response.getOutputStream();
		workbook.write(ouputStream);
		ouputStream.flush();
		ouputStream.close();
	}
}

 

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值