欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

EXCELPOI的样式设置

程序员文章站 2022-06-03 12:23:58
...

EXCELPOI的样式设置

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class ExcelExecuse {
	@RequestMapping("/exportExcelModle")
	@ResponseBody
	public void exportExcelModle(HttpServletRequest request, HttpServletResponse response, String funcNames,
			String funcCodes) {
		StringBuffer sql = new StringBuffer("select * from EXCELMODELT_FIELD where FUNCODE = ?");
		response.setCharacterEncoding("utf-8");
		response.setContentType("multipart/form-data");
		response.setHeader("Content-Disposition", "attachment;fileName=" + funcCodes + ".xls");
		ServletOutputStream out = null;
		HSSFWorkbook workbook = null;
		try {
			out = response.getOutputStream();
			// 声明一个工作薄
			workbook = new HSSFWorkbook();
			List<IBean> dataList = null;
			HSSFCellStyle style = getStyle(workbook);
			for (String func : funcCodes.split(",")) {
				dataList = SqlUtil.getExecutor().queryForList(sql.toString(), func);
				setSheetByData(func, dataList, workbook, style);
			}
			workbook.write(out);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	@SuppressWarnings("deprecation")
	public void setSheetByData(String title, List<IBean> dataList, HSSFWorkbook workbook, HSSFCellStyle style) {
		HSSFSheet sheet = workbook.createSheet(title);
		sheet.setDefaultColumnWidth((short) 15);
		HSSFRow row = sheet.createRow(0);
		HSSFCell cell = null;
		String FIELD_NAME = null;
		int column = 0;
		for (IBean data : dataList) {
			FIELD_NAME = data.get("FIELD_NAME", "");
			if (!StringUtils.isEmpty(FIELD_NAME)) {
				cell = row.createCell(column);
				cell.setCellStyle(style);
				HSSFRichTextString text = new HSSFRichTextString(FIELD_NAME);
				cell.setCellValue(text);
				column++;
			}
		}
	}

	private HSSFCellStyle getStyle(HSSFWorkbook workbook) {
		HSSFCellStyle style = workbook.createCellStyle();
		style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
		style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		style.setBorderRight(HSSFCellStyle.BORDER_THIN);
		style.setBorderTop(HSSFCellStyle.BORDER_THIN);
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		HSSFFont font = workbook.createFont();
		font.setColor(HSSFColor.VIOLET.index);
		font.setFontHeightInPoints((short) 12);
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		style.setFont(font);
		return style;
	}
}