package com.mx.travel.mto.util;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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.HSSFPalette;
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.CellRangeAddress;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.hssf.util.Region;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
public class ExcelUtil {
/**
*
* @param excelFile excel文档对象
* @param titleList 标题list
* @param steetNum 所见sheet页
* @param startRowNum 从第几行开始 第一行为0
* @param lastNoCount 从后向前数 丢弃的行数
* @return
*/
public static List<Map<String,String>> getExcelInfoListByHssf(InputStream is,String[] titleList,int steetNum,int startRowNum,int lastNoCount){
List<Map<String,String>> resultList = new ArrayList<Map<String,String>>();
try{
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
HSSFSheet hSSFSheet = hssfWorkbook.getSheetAt(steetNum);
if(hSSFSheet == null){
return resultList;
}
for(int i = startRowNum;i <= hSSFSheet.getLastRowNum() - lastNoCount;i++){
HSSFRow hssfRow = hSSFSheet.getRow(i);
if(hssfRow == null){
continue;
}
if(titleList == null){
continue;
}
Map<String,String> recordMap = new HashMap<String,String>();
for(int j = 0;j < titleList.length;j++){
String title = titleList[j];
HSSFCell hssfCell = hssfRow.getCell((short)j);
if (hssfCell == null) {
continue;
}
String value = getValue(hssfCell);
recordMap.put(title, value);
}
resultList.add(recordMap);
}
return resultList;
}catch(Exception e){
e.printStackTrace();
return resultList;
}
}
//sssssssssssssssssss
public static void getExcelStreamByHssf(String[] chineseTitleList,String[] englishTitleList,Integer[] widths,List<HashMap<String,String>> recordList,String sheetTitle,OutputStream out) throws Exception{
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
HSSFSheet sheet = hssfWorkbook.createSheet(sheetTitle);
sheet.setDefaultRowHeight((short)6000);
sheet.setDefaultRowHeightInPoints((short)6000);
for(int i = 0;i < widths.length;i++){
int width = widths[i].intValue();
sheet.setColumnWidth((short)i,(short)width);
}
HSSFCellStyle titleStyle = ExcelUtil.setStyleA(hssfWorkbook);
HSSFCellStyle recordStyle = ExcelUtil.setStyleB(hssfWorkbook);
//生成标题行
HSSFRow titleRow1 = sheet.createRow(0);
for(int i = 8;i < 9;i++){
HSSFCell cell = titleRow1.createCell((short)8);
//设置单元格 合并 //参数 1:行号 参数 2:起始列号 参数 3:行号 参数 4:终止列号
sheet.addMergedRegion(new Region((short)0,(short)0,(short)0,(short)7));
cell.setCellStyle(recordStyle);
HSSFRichTextString text = new HSSFRichTextString("美元汇率");
cell.setCellValue(text);
}
HSSFRow titleRow2 = sheet.createRow(1);
for(int i = 8;i < 9;i++){
HSSFCell cell = titleRow2.createCell((short)8);
sheet.addMergedRegion(new Region((short)1,(short)0,(short)1,(short)7));
cell.setCellStyle(recordStyle);
HSSFRichTextString text = new HSSFRichTextString("美元汇率");
cell.setCellValue(text);
}
HSSFRow titleRow = sheet.createRow(2);
for(int i = 0;i < chineseTitleList.length;i++){
HSSFCell cell = titleRow.createCell((short)i);
cell.setCellStyle(titleStyle);
HSSFRichTextString text = new HSSFRichTextString(chineseTitleList[i]);
cell.setCellValue(text);
}
//生成记录
for(int i = 0;i < recordList.size();i++){
HashMap<String,String> valueMap = recordList.get(i);
HSSFRow recordRow = sheet.createRow(i+3);
for(int j = 0;j < englishTitleList.length;j++){
HSSFCell cell = recordRow.createCell((short)j);
cell.setCellStyle(recordStyle);
String key = englishTitleList[j];
String value =valueMap.get(key);
HSSFRichTextString text = new HSSFRichTextString(value);
//System.out.println(key + ":"+ value);
cell.setCellValue(text);
}
}
hssfWorkbook.write(out);
}
private static String getValue(HSSFCell hssfCell) {
int cellType = hssfCell.getCellType();
if (cellType == HSSFCell.CELL_TYPE_BOOLEAN) {
// 返回布尔类型的值
return String.valueOf(hssfCell.getBooleanCellValue());
}else if (cellType == HSSFCell.CELL_TYPE_NUMERIC) {
// 返回数值类型的值
double value = hssfCell.getNumericCellValue();
BigDecimal bigDecimal = new BigDecimal(value);
return String.valueOf(bigDecimal.toString());
}else if (cellType == HSSFCell.CELL_TYPE_FORMULA) {
// 返回数值类型的值
return String.valueOf(hssfCell.getCellFormula());
}else {
// 返回字符串类型的值
return String.valueOf(hssfCell.getStringCellValue());
}
}
private static HSSFCellStyle setStyle(HSSFWorkbook hssfWorkbook){//底色蓝,全居中,加粗
HSSFCellStyle style = hssfWorkbook.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平对齐方式
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直方向对齐方式
style.setBorderBottom(CellStyle.BORDER_THIN);//底边框
style.setBottomBorderColor(HSSFColor.BLACK.index);
style.setBorderLeft(CellStyle.BORDER_THIN);//左边框
style.setLeftBorderColor(HSSFColor.BLACK.index);
style.setBorderRight(CellStyle.BORDER_THIN);//右边框
style.setRightBorderColor(HSSFColor.BLACK.index);
style.setBorderTop(CellStyle.BORDER_THIN);//头边框
style.setTopBorderColor(HSSFColor.BLACK.index);
style.setWrapText(true);
HSSFFont font = hssfWorkbook.createFont();
font.setColor(HSSFColor.BLACK.index);
font.setFontName("微软雅黑");
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font.setFontHeightInPoints((short) 11);
style.setFont(font);
return style;
}
public static HSSFCellStyle setStyleA(HSSFWorkbook hssfWorkbook){//底色蓝,全居中,加粗
HSSFCellStyle style = hssfWorkbook.createCellStyle();
HSSFPalette palette = hssfWorkbook.getCustomPalette(); //wb HSSFWorkbook对象
//palette.setColorAtIndex((short) 9, (byte) (color.getRed()), (byte) (color.getGreen()), (byte) (color.getBlue()));
//palette.setColorAtIndex((short)9, (byte) (0xff & 251), (byte) (0xff & 161), (byte) (0xff & 161));
//palette.setColorAtIndex((short)10, (byte) (0x66), (byte) (0xcd), (byte) (0xaa));
//palette.setColorAtIndex((short)11, (byte) (255), (byte) (165), (byte) (0));
//style.setFillForegroundColor((short) 9);
style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
//style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);//底色
style.setFillPattern(CellStyle.SOLID_FOREGROUND);//填充方式
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平对齐方式
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直方向对齐方式
style.setBorderBottom(CellStyle.BORDER_THIN);//底边框
style.setBottomBorderColor(HSSFColor.BLACK.index);
style.setBorderLeft(CellStyle.BORDER_THIN);//左边框
style.setLeftBorderColor(HSSFColor.BLACK.index);
style.setBorderRight(CellStyle.BORDER_THIN);//右边框
style.setRightBorderColor(HSSFColor.BLACK.index);
style.setBorderTop(CellStyle.BORDER_THIN);//头边框
style.setTopBorderColor(HSSFColor.BLACK.index);
style.setWrapText(true);
HSSFFont font = hssfWorkbook.createFont();
font.setColor(HSSFColor.BLACK.index);
font.setFontName("微软雅黑");
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font.setFontHeightInPoints((short) 11);
style.setFont(font);
return style;
}
/**
* 字体:11号,微软雅黑,加粗,黑色
* 底色百,靠左,细边框
*/
public static HSSFCellStyle setStyleB(HSSFWorkbook hssfWorkbook){//底色蓝,全居中,加粗
HSSFCellStyle style = hssfWorkbook.createCellStyle();
style.setFillForegroundColor(HSSFColor.WHITE.index);//底色
style.setFillPattern(CellStyle.SOLID_FOREGROUND);//填充方式
style.setAlignment(CellStyle.ALIGN_LEFT);//水平对齐方式
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);//垂直方向对齐方式
style.setBorderBottom(CellStyle.BORDER_THIN);//底边框
style.setBottomBorderColor(HSSFColor.BLACK.index);
style.setBorderLeft(CellStyle.BORDER_THIN);//左边框
style.setLeftBorderColor(HSSFColor.BLACK.index);
style.setBorderRight(CellStyle.BORDER_THIN);//右边框
style.setRightBorderColor(HSSFColor.BLACK.index);
style.setBorderTop(CellStyle.BORDER_THIN);//头边框
style.setTopBorderColor(HSSFColor.BLACK.index);
style.setWrapText(true);
HSSFFont font = hssfWorkbook.createFont();
font.setColor(HSSFColor.BLACK.index);
font.setFontName("微软雅黑");
font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
font.setFontHeightInPoints((short) 11);
style.setFont(font);
return style;
}
/**
* 字体:11号,黑色
* 居中,细边框,黑色
*/
private static HSSFCellStyle setStyleC(HSSFWorkbook hssfWorkbook){//底色蓝,全居中,加粗
HSSFCellStyle style = hssfWorkbook.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平对齐方式
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直方向对齐方式
style.setBorderBottom(CellStyle.BORDER_THIN);//底边框
style.setBottomBorderColor(HSSFColor.BLACK.index);
style.setBorderLeft(CellStyle.BORDER_THIN);//左边框
style.setLeftBorderColor(HSSFColor.BLACK.index);
style.setBorderRight(CellStyle.BORDER_THIN);//右边框
style.setRightBorderColor(HSSFColor.BLACK.index);
style.setBorderTop(CellStyle.BORDER_THIN);//头边框
style.setTopBorderColor(HSSFColor.BLACK.index);
style.setWrapText(true);
HSSFFont font = hssfWorkbook.createFont();
font.setColor(HSSFColor.BLACK.index);
font.setFontHeightInPoints((short) 11);
style.setFont(font);
return style;
}
/**
* 字体:20号,黑色,加粗
* 居中,黑色细边框
*/
private static HSSFCellStyle setStyleD(HSSFWorkbook hssfWorkbook){//底色蓝,全居中,加粗
HSSFCellStyle style = hssfWorkbook.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平对齐方式
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直方向对齐方式
style.setBorderBottom(CellStyle.BORDER_THIN);//底边框
style.setBottomBorderColor(HSSFColor.BLACK.index);
style.setBorderLeft(CellStyle.BORDER_THIN);//左边框
style.setLeftBorderColor(HSSFColor.BLACK.index);
style.setBorderRight(CellStyle.BORDER_THIN);//右边框
style.setRightBorderColor(HSSFColor.BLACK.index);
style.setBorderTop(CellStyle.BORDER_THIN);//头边框
style.setTopBorderColor(HSSFColor.BLACK.index);
style.setWrapText(true);
HSSFFont font = hssfWorkbook.createFont();
font.setColor(HSSFColor.BLACK.index);
font.setFontHeightInPoints((short) 20);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
style.setFont(font);
return style;
}
private static HSSFCellStyle setStyleE(HSSFWorkbook hssfWorkbook){
HSSFCellStyle style = hssfWorkbook.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_RIGHT);//水平对齐方式
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直方向对齐方式
style.setBorderBottom(CellStyle.BORDER_THIN);//底边框
style.setBottomBorderColor(HSSFColor.BLACK.index);
style.setBorderLeft(CellStyle.BORDER_THIN);//左边框
style.setLeftBorderColor(HSSFColor.BLACK.index);
style.setBorderRight(CellStyle.BORDER_THIN);//右边框
style.setRightBorderColor(HSSFColor.BLACK.index);
style.setBorderTop(CellStyle.BORDER_THIN);//头边框
style.setTopBorderColor(HSSFColor.BLACK.index);
style.setWrapText(true);
HSSFFont font = hssfWorkbook.createFont();
font.setColor(HSSFColor.BLACK.index);
font.setFontName("微软雅黑");
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font.setFontHeightInPoints((short) 11);
style.setFont(font);
return style;
}
public static void getExcelStreamByHssfNotify(String[] chineseTitleList,String[] englishTitleList,Integer[] widths,List<HashMap<String,String>> recordList,Map<String,String> map,String sheetTitle,OutputStream out) throws Exception{
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
HSSFSheet sheet = hssfWorkbook.createSheet(sheetTitle);
for(int i = 0;i < widths.length;i++){
int width = widths[i].intValue();
sheet.setColumnWidth((short)i,(short)width);
}
HSSFCellStyle titleStyle = ExcelUtil.setStyleA(hssfWorkbook);
HSSFCellStyle recordStyle = ExcelUtil.setStyleB(hssfWorkbook);
//生成标题行
HSSFRow titleRow = sheet.createRow(1);
for(int i = 0;i < chineseTitleList.length;i++){
HSSFCell cell = titleRow.createCell((short)i);
cell.setCellStyle(titleStyle);
HSSFRichTextString text = new HSSFRichTextString(chineseTitleList[i]);
cell.setCellValue(text);
}
//生成记录
for(int i = 0;i < recordList.size();i++){
HashMap<String,String> valueMap = recordList.get(i);
HSSFRow recordRow = sheet.createRow(i+2);
for(int j = 0;j < englishTitleList.length;j++){
HSSFCell cell = recordRow.createCell((short)j);
cell.setCellStyle(recordStyle);
String key = englishTitleList[j];
String value =valueMap.get(key);
HSSFRichTextString text = new HSSFRichTextString(value);
//System.out.println(key + ":"+ value);
cell.setCellValue(text);
}
}
HSSFRow recordRow = sheet.createRow(0);
String length = map.get("length");
String sbf = map.get("sbf");
//(int rowFrom, short colFrom, int rowTo, short colTo);
Region region=new Region(0, (short)0, 0, (short)(Integer.parseInt(length)-1));
sheet.addMergedRegion(region);
//sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, Integer.parseInt(length)-1));
//CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 0, 0, Integer.parseInt(length)-1);
//sheet.addMergedRegion(cellRangeAddress);
HSSFCell cell = recordRow.createCell((short)0);
HSSFCellStyle style = hssfWorkbook.createCellStyle();
HSSFFont font = hssfWorkbook.createFont();
font.setColor(HSSFColor.RED.index);
font.setFontName("微软雅黑");
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font.setFontHeightInPoints((short) 13);
style.setFont(font);
HSSFRichTextString text = new HSSFRichTextString(sbf);
//System.out.println(key + ":"+ value);
cell.setCellValue(text);
cell.setCellStyle(style);
hssfWorkbook.write(out);
}
public static void extOrderRecordExcel() {
try {
//List<String> orgIdList = this.getOrgInfoIdsByUserId(request, modularId);
String fileName = "orderRecord.xls";
//response.setContentType("application/vnd.ms-excel");
//response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
//response.addHeader("Cache-Control", "no-cache");
String[] chineseTitleList = { "预约人姓名", "证件类型", "证件号码", "手机", "性别","生日",
"地区","体检中心","体检内容","体检日期","时间段","体检编码","创建日期"};
String[] englishTitleList={"examUserName","identificationType","identificationNum","tel","sex","birthday",
"areaName","centerName","itemName","examinationDate","durationName","examCode","createTime" };
Integer[] widths = {3000,2000,7000,4000,2000,4000,7000,10000,10000,4000,3000,3000,6000 };
List<HashMap<String, String>> recordList = new ArrayList<HashMap<String, String>>();
for (int i=0;i<4;i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("examUserName","examUserName");
map.put("identificationType", "identificationType");
map.put("identificationNum", "identificationNum");
map.put("tel","tel");
map.put("sex","tel");
map.put("birthday","tel");
map.put("areaName", "tel");
map.put("centerName","tel");
map.put("itemName", "tel");
map.put("examinationDate", "tel");
map.put("durationName","tel");
map.put("examCode", "tel");
map.put("createTime","tel");
recordList.add(map);
}
String sheetTitle = "体检预约记录";
//OutputStream ouputStream = response.getOutputStream();
FileOutputStream fileOut = null;
fileOut = new FileOutputStream("f:\\workbook.xls");
ExcelUtil.getExcelStreamByHssf(chineseTitleList, englishTitleList, widths, recordList, sheetTitle, fileOut);
//ouputStream.flush();
//ouputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
System.out.println("1111111111111111111");
extOrderRecordExcel();
}
/* //导出体检预约记录
@RequestMapping("/examQuery/extOrderRecordExcel")
public void extOrderRecordExcel(HttpServletRequest request, HttpServletResponse response, String modularId,String name,
String identificationNum,String startTime, String endTime) {
try {
List<String> orgIdList = this.getOrgInfoIdsByUserId(request, modularId);
List<BhExaminationOrderRecord> orderRecordList = examinationService.getRecordList(orgIdList, "2",name,identificationNum,startTime,endTime);
String fileName = "orderRecord.xls";
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
response.addHeader("Cache-Control", "no-cache");
String[] chineseTitleList = { "预约人姓名", "证件类型", "证件号码", "手机", "性别","生日",
"地区","体检中心","体检内容","体检日期","时间段","体检编码","创建日期"};
String[] englishTitleList={"examUserName","identificationType","identificationNum","tel","sex","birthday",
"areaName","centerName","itemName","examinationDate","durationName","examCode","createTime" };
Integer[] widths = {3000,2000,7000,4000,2000,4000,7000,10000,10000,4000,3000,3000,6000 };
List<HashMap<String, String>> recordList = new ArrayList<HashMap<String, String>>();
for (BhExaminationOrderRecord bhExaminationOrderRecord : orderRecordList) {
String identificationType = bhExaminationOrderRecord.getIdentificationType();
String type="其他";
if("S".equalsIgnoreCase(identificationType)){
type="身份证";
}else if("H".equalsIgnoreCase(identificationType)){
type="护照";
}else if("J".equalsIgnoreCase(identificationType)){
type="军官证";
}else{
type="其他";
}
String sex=bhExaminationOrderRecord.getSex();
String xb="";
if("F".equalsIgnoreCase(sex)){
xb="女";
}else if("M".equalsIgnoreCase(sex)){
xb="男";
}
HashMap<String, String> map = new HashMap<>();
map.put("examUserName", bhExaminationOrderRecord.getExamUserName());
map.put("identificationType", type);
map.put("identificationNum", bhExaminationOrderRecord.getIdentificationNum());
map.put("tel", bhExaminationOrderRecord.getMobilePhone());
map.put("sex",xb);
map.put("birthday", bhExaminationOrderRecord.getBirthday());
map.put("areaName", bhExaminationOrderRecord.getAreaName());
map.put("centerName", bhExaminationOrderRecord.getCenterName());
map.put("itemName", bhExaminationOrderRecord.getItemName());
map.put("examinationDate", bhExaminationOrderRecord.getExaminationDate());
map.put("durationName", bhExaminationOrderRecord.getDurationName());
map.put("examCode", Integer.toString(bhExaminationOrderRecord.getExamCode()));
map.put("createTime",fmt.format(bhExaminationOrderRecord.getCreateTime()));
recordList.add(map);
}
String sheetTitle = "体检预约记录";
OutputStream ouputStream = response.getOutputStream();
ExcelUtil.getExcelStreamByHssf(chineseTitleList, englishTitleList, widths, recordList, sheetTitle, ouputStream);
ouputStream.flush();
ouputStream.close();
} catch (Exception e) {
logger.error(e,e);
}
HSSFRow row1 = sheet.createRow(0);//第一行
// 四个参数分别是:起始行,起始列,结束行,结束列
//送花eet.addMergedRegion(new Region(0, (short) 0, 0, (short) 15));
row1.setHeightInPoints(20);
HSSFCell cell1 = row1.createCell((short)8); //--->创建一个单元格
cell1.setCellStyle(style1);
cell1.setCellValue("美元汇率");
HSSFCell cell2 = row1.createCell((short)9); //--->创建一个单元格
cell2.setCellStyle(style1);
cell2.setCellValue("6.5");
HSSFRow row2 = sheet.createRow(1);//第二行
row2.setHeightInPoints(20);
HSSFCell cell3 = row2.createCell((short)8); //--->创建一个单元格
cell3.setCellStyle(style1);
cell3.setCellValue("欧元汇率");
HSSFCell cell4 = row2.createCell((short)9); //--->创建一个单元格
cell4.setCellStyle(style1);
cell4.setCellValue("7.5");
曲杨 2017/4/18 14:24:58
style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
}*/
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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.HSSFPalette;
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.CellRangeAddress;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.hssf.util.Region;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
public class ExcelUtil {
/**
*
* @param excelFile excel文档对象
* @param titleList 标题list
* @param steetNum 所见sheet页
* @param startRowNum 从第几行开始 第一行为0
* @param lastNoCount 从后向前数 丢弃的行数
* @return
*/
public static List<Map<String,String>> getExcelInfoListByHssf(InputStream is,String[] titleList,int steetNum,int startRowNum,int lastNoCount){
List<Map<String,String>> resultList = new ArrayList<Map<String,String>>();
try{
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
HSSFSheet hSSFSheet = hssfWorkbook.getSheetAt(steetNum);
if(hSSFSheet == null){
return resultList;
}
for(int i = startRowNum;i <= hSSFSheet.getLastRowNum() - lastNoCount;i++){
HSSFRow hssfRow = hSSFSheet.getRow(i);
if(hssfRow == null){
continue;
}
if(titleList == null){
continue;
}
Map<String,String> recordMap = new HashMap<String,String>();
for(int j = 0;j < titleList.length;j++){
String title = titleList[j];
HSSFCell hssfCell = hssfRow.getCell((short)j);
if (hssfCell == null) {
continue;
}
String value = getValue(hssfCell);
recordMap.put(title, value);
}
resultList.add(recordMap);
}
return resultList;
}catch(Exception e){
e.printStackTrace();
return resultList;
}
}
//sssssssssssssssssss
public static void getExcelStreamByHssf(String[] chineseTitleList,String[] englishTitleList,Integer[] widths,List<HashMap<String,String>> recordList,String sheetTitle,OutputStream out) throws Exception{
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
HSSFSheet sheet = hssfWorkbook.createSheet(sheetTitle);
sheet.setDefaultRowHeight((short)6000);
sheet.setDefaultRowHeightInPoints((short)6000);
for(int i = 0;i < widths.length;i++){
int width = widths[i].intValue();
sheet.setColumnWidth((short)i,(short)width);
}
HSSFCellStyle titleStyle = ExcelUtil.setStyleA(hssfWorkbook);
HSSFCellStyle recordStyle = ExcelUtil.setStyleB(hssfWorkbook);
//生成标题行
HSSFRow titleRow1 = sheet.createRow(0);
for(int i = 8;i < 9;i++){
HSSFCell cell = titleRow1.createCell((short)8);
//设置单元格 合并 //参数 1:行号 参数 2:起始列号 参数 3:行号 参数 4:终止列号
sheet.addMergedRegion(new Region((short)0,(short)0,(short)0,(short)7));
cell.setCellStyle(recordStyle);
HSSFRichTextString text = new HSSFRichTextString("美元汇率");
cell.setCellValue(text);
}
HSSFRow titleRow2 = sheet.createRow(1);
for(int i = 8;i < 9;i++){
HSSFCell cell = titleRow2.createCell((short)8);
sheet.addMergedRegion(new Region((short)1,(short)0,(short)1,(short)7));
cell.setCellStyle(recordStyle);
HSSFRichTextString text = new HSSFRichTextString("美元汇率");
cell.setCellValue(text);
}
HSSFRow titleRow = sheet.createRow(2);
for(int i = 0;i < chineseTitleList.length;i++){
HSSFCell cell = titleRow.createCell((short)i);
cell.setCellStyle(titleStyle);
HSSFRichTextString text = new HSSFRichTextString(chineseTitleList[i]);
cell.setCellValue(text);
}
//生成记录
for(int i = 0;i < recordList.size();i++){
HashMap<String,String> valueMap = recordList.get(i);
HSSFRow recordRow = sheet.createRow(i+3);
for(int j = 0;j < englishTitleList.length;j++){
HSSFCell cell = recordRow.createCell((short)j);
cell.setCellStyle(recordStyle);
String key = englishTitleList[j];
String value =valueMap.get(key);
HSSFRichTextString text = new HSSFRichTextString(value);
//System.out.println(key + ":"+ value);
cell.setCellValue(text);
}
}
hssfWorkbook.write(out);
}
private static String getValue(HSSFCell hssfCell) {
int cellType = hssfCell.getCellType();
if (cellType == HSSFCell.CELL_TYPE_BOOLEAN) {
// 返回布尔类型的值
return String.valueOf(hssfCell.getBooleanCellValue());
}else if (cellType == HSSFCell.CELL_TYPE_NUMERIC) {
// 返回数值类型的值
double value = hssfCell.getNumericCellValue();
BigDecimal bigDecimal = new BigDecimal(value);
return String.valueOf(bigDecimal.toString());
}else if (cellType == HSSFCell.CELL_TYPE_FORMULA) {
// 返回数值类型的值
return String.valueOf(hssfCell.getCellFormula());
}else {
// 返回字符串类型的值
return String.valueOf(hssfCell.getStringCellValue());
}
}
private static HSSFCellStyle setStyle(HSSFWorkbook hssfWorkbook){//底色蓝,全居中,加粗
HSSFCellStyle style = hssfWorkbook.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平对齐方式
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直方向对齐方式
style.setBorderBottom(CellStyle.BORDER_THIN);//底边框
style.setBottomBorderColor(HSSFColor.BLACK.index);
style.setBorderLeft(CellStyle.BORDER_THIN);//左边框
style.setLeftBorderColor(HSSFColor.BLACK.index);
style.setBorderRight(CellStyle.BORDER_THIN);//右边框
style.setRightBorderColor(HSSFColor.BLACK.index);
style.setBorderTop(CellStyle.BORDER_THIN);//头边框
style.setTopBorderColor(HSSFColor.BLACK.index);
style.setWrapText(true);
HSSFFont font = hssfWorkbook.createFont();
font.setColor(HSSFColor.BLACK.index);
font.setFontName("微软雅黑");
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font.setFontHeightInPoints((short) 11);
style.setFont(font);
return style;
}
public static HSSFCellStyle setStyleA(HSSFWorkbook hssfWorkbook){//底色蓝,全居中,加粗
HSSFCellStyle style = hssfWorkbook.createCellStyle();
HSSFPalette palette = hssfWorkbook.getCustomPalette(); //wb HSSFWorkbook对象
//palette.setColorAtIndex((short) 9, (byte) (color.getRed()), (byte) (color.getGreen()), (byte) (color.getBlue()));
//palette.setColorAtIndex((short)9, (byte) (0xff & 251), (byte) (0xff & 161), (byte) (0xff & 161));
//palette.setColorAtIndex((short)10, (byte) (0x66), (byte) (0xcd), (byte) (0xaa));
//palette.setColorAtIndex((short)11, (byte) (255), (byte) (165), (byte) (0));
//style.setFillForegroundColor((short) 9);
style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
//style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);//底色
style.setFillPattern(CellStyle.SOLID_FOREGROUND);//填充方式
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平对齐方式
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直方向对齐方式
style.setBorderBottom(CellStyle.BORDER_THIN);//底边框
style.setBottomBorderColor(HSSFColor.BLACK.index);
style.setBorderLeft(CellStyle.BORDER_THIN);//左边框
style.setLeftBorderColor(HSSFColor.BLACK.index);
style.setBorderRight(CellStyle.BORDER_THIN);//右边框
style.setRightBorderColor(HSSFColor.BLACK.index);
style.setBorderTop(CellStyle.BORDER_THIN);//头边框
style.setTopBorderColor(HSSFColor.BLACK.index);
style.setWrapText(true);
HSSFFont font = hssfWorkbook.createFont();
font.setColor(HSSFColor.BLACK.index);
font.setFontName("微软雅黑");
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font.setFontHeightInPoints((short) 11);
style.setFont(font);
return style;
}
/**
* 字体:11号,微软雅黑,加粗,黑色
* 底色百,靠左,细边框
*/
public static HSSFCellStyle setStyleB(HSSFWorkbook hssfWorkbook){//底色蓝,全居中,加粗
HSSFCellStyle style = hssfWorkbook.createCellStyle();
style.setFillForegroundColor(HSSFColor.WHITE.index);//底色
style.setFillPattern(CellStyle.SOLID_FOREGROUND);//填充方式
style.setAlignment(CellStyle.ALIGN_LEFT);//水平对齐方式
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);//垂直方向对齐方式
style.setBorderBottom(CellStyle.BORDER_THIN);//底边框
style.setBottomBorderColor(HSSFColor.BLACK.index);
style.setBorderLeft(CellStyle.BORDER_THIN);//左边框
style.setLeftBorderColor(HSSFColor.BLACK.index);
style.setBorderRight(CellStyle.BORDER_THIN);//右边框
style.setRightBorderColor(HSSFColor.BLACK.index);
style.setBorderTop(CellStyle.BORDER_THIN);//头边框
style.setTopBorderColor(HSSFColor.BLACK.index);
style.setWrapText(true);
HSSFFont font = hssfWorkbook.createFont();
font.setColor(HSSFColor.BLACK.index);
font.setFontName("微软雅黑");
font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
font.setFontHeightInPoints((short) 11);
style.setFont(font);
return style;
}
/**
* 字体:11号,黑色
* 居中,细边框,黑色
*/
private static HSSFCellStyle setStyleC(HSSFWorkbook hssfWorkbook){//底色蓝,全居中,加粗
HSSFCellStyle style = hssfWorkbook.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平对齐方式
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直方向对齐方式
style.setBorderBottom(CellStyle.BORDER_THIN);//底边框
style.setBottomBorderColor(HSSFColor.BLACK.index);
style.setBorderLeft(CellStyle.BORDER_THIN);//左边框
style.setLeftBorderColor(HSSFColor.BLACK.index);
style.setBorderRight(CellStyle.BORDER_THIN);//右边框
style.setRightBorderColor(HSSFColor.BLACK.index);
style.setBorderTop(CellStyle.BORDER_THIN);//头边框
style.setTopBorderColor(HSSFColor.BLACK.index);
style.setWrapText(true);
HSSFFont font = hssfWorkbook.createFont();
font.setColor(HSSFColor.BLACK.index);
font.setFontHeightInPoints((short) 11);
style.setFont(font);
return style;
}
/**
* 字体:20号,黑色,加粗
* 居中,黑色细边框
*/
private static HSSFCellStyle setStyleD(HSSFWorkbook hssfWorkbook){//底色蓝,全居中,加粗
HSSFCellStyle style = hssfWorkbook.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平对齐方式
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直方向对齐方式
style.setBorderBottom(CellStyle.BORDER_THIN);//底边框
style.setBottomBorderColor(HSSFColor.BLACK.index);
style.setBorderLeft(CellStyle.BORDER_THIN);//左边框
style.setLeftBorderColor(HSSFColor.BLACK.index);
style.setBorderRight(CellStyle.BORDER_THIN);//右边框
style.setRightBorderColor(HSSFColor.BLACK.index);
style.setBorderTop(CellStyle.BORDER_THIN);//头边框
style.setTopBorderColor(HSSFColor.BLACK.index);
style.setWrapText(true);
HSSFFont font = hssfWorkbook.createFont();
font.setColor(HSSFColor.BLACK.index);
font.setFontHeightInPoints((short) 20);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
style.setFont(font);
return style;
}
private static HSSFCellStyle setStyleE(HSSFWorkbook hssfWorkbook){
HSSFCellStyle style = hssfWorkbook.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_RIGHT);//水平对齐方式
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直方向对齐方式
style.setBorderBottom(CellStyle.BORDER_THIN);//底边框
style.setBottomBorderColor(HSSFColor.BLACK.index);
style.setBorderLeft(CellStyle.BORDER_THIN);//左边框
style.setLeftBorderColor(HSSFColor.BLACK.index);
style.setBorderRight(CellStyle.BORDER_THIN);//右边框
style.setRightBorderColor(HSSFColor.BLACK.index);
style.setBorderTop(CellStyle.BORDER_THIN);//头边框
style.setTopBorderColor(HSSFColor.BLACK.index);
style.setWrapText(true);
HSSFFont font = hssfWorkbook.createFont();
font.setColor(HSSFColor.BLACK.index);
font.setFontName("微软雅黑");
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font.setFontHeightInPoints((short) 11);
style.setFont(font);
return style;
}
public static void getExcelStreamByHssfNotify(String[] chineseTitleList,String[] englishTitleList,Integer[] widths,List<HashMap<String,String>> recordList,Map<String,String> map,String sheetTitle,OutputStream out) throws Exception{
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
HSSFSheet sheet = hssfWorkbook.createSheet(sheetTitle);
for(int i = 0;i < widths.length;i++){
int width = widths[i].intValue();
sheet.setColumnWidth((short)i,(short)width);
}
HSSFCellStyle titleStyle = ExcelUtil.setStyleA(hssfWorkbook);
HSSFCellStyle recordStyle = ExcelUtil.setStyleB(hssfWorkbook);
//生成标题行
HSSFRow titleRow = sheet.createRow(1);
for(int i = 0;i < chineseTitleList.length;i++){
HSSFCell cell = titleRow.createCell((short)i);
cell.setCellStyle(titleStyle);
HSSFRichTextString text = new HSSFRichTextString(chineseTitleList[i]);
cell.setCellValue(text);
}
//生成记录
for(int i = 0;i < recordList.size();i++){
HashMap<String,String> valueMap = recordList.get(i);
HSSFRow recordRow = sheet.createRow(i+2);
for(int j = 0;j < englishTitleList.length;j++){
HSSFCell cell = recordRow.createCell((short)j);
cell.setCellStyle(recordStyle);
String key = englishTitleList[j];
String value =valueMap.get(key);
HSSFRichTextString text = new HSSFRichTextString(value);
//System.out.println(key + ":"+ value);
cell.setCellValue(text);
}
}
HSSFRow recordRow = sheet.createRow(0);
String length = map.get("length");
String sbf = map.get("sbf");
//(int rowFrom, short colFrom, int rowTo, short colTo);
Region region=new Region(0, (short)0, 0, (short)(Integer.parseInt(length)-1));
sheet.addMergedRegion(region);
//sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, Integer.parseInt(length)-1));
//CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 0, 0, Integer.parseInt(length)-1);
//sheet.addMergedRegion(cellRangeAddress);
HSSFCell cell = recordRow.createCell((short)0);
HSSFCellStyle style = hssfWorkbook.createCellStyle();
HSSFFont font = hssfWorkbook.createFont();
font.setColor(HSSFColor.RED.index);
font.setFontName("微软雅黑");
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font.setFontHeightInPoints((short) 13);
style.setFont(font);
HSSFRichTextString text = new HSSFRichTextString(sbf);
//System.out.println(key + ":"+ value);
cell.setCellValue(text);
cell.setCellStyle(style);
hssfWorkbook.write(out);
}
public static void extOrderRecordExcel() {
try {
//List<String> orgIdList = this.getOrgInfoIdsByUserId(request, modularId);
String fileName = "orderRecord.xls";
//response.setContentType("application/vnd.ms-excel");
//response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
//response.addHeader("Cache-Control", "no-cache");
String[] chineseTitleList = { "预约人姓名", "证件类型", "证件号码", "手机", "性别","生日",
"地区","体检中心","体检内容","体检日期","时间段","体检编码","创建日期"};
String[] englishTitleList={"examUserName","identificationType","identificationNum","tel","sex","birthday",
"areaName","centerName","itemName","examinationDate","durationName","examCode","createTime" };
Integer[] widths = {3000,2000,7000,4000,2000,4000,7000,10000,10000,4000,3000,3000,6000 };
List<HashMap<String, String>> recordList = new ArrayList<HashMap<String, String>>();
for (int i=0;i<4;i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("examUserName","examUserName");
map.put("identificationType", "identificationType");
map.put("identificationNum", "identificationNum");
map.put("tel","tel");
map.put("sex","tel");
map.put("birthday","tel");
map.put("areaName", "tel");
map.put("centerName","tel");
map.put("itemName", "tel");
map.put("examinationDate", "tel");
map.put("durationName","tel");
map.put("examCode", "tel");
map.put("createTime","tel");
recordList.add(map);
}
String sheetTitle = "体检预约记录";
//OutputStream ouputStream = response.getOutputStream();
FileOutputStream fileOut = null;
fileOut = new FileOutputStream("f:\\workbook.xls");
ExcelUtil.getExcelStreamByHssf(chineseTitleList, englishTitleList, widths, recordList, sheetTitle, fileOut);
//ouputStream.flush();
//ouputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
System.out.println("1111111111111111111");
extOrderRecordExcel();
}
/* //导出体检预约记录
@RequestMapping("/examQuery/extOrderRecordExcel")
public void extOrderRecordExcel(HttpServletRequest request, HttpServletResponse response, String modularId,String name,
String identificationNum,String startTime, String endTime) {
try {
List<String> orgIdList = this.getOrgInfoIdsByUserId(request, modularId);
List<BhExaminationOrderRecord> orderRecordList = examinationService.getRecordList(orgIdList, "2",name,identificationNum,startTime,endTime);
String fileName = "orderRecord.xls";
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
response.addHeader("Cache-Control", "no-cache");
String[] chineseTitleList = { "预约人姓名", "证件类型", "证件号码", "手机", "性别","生日",
"地区","体检中心","体检内容","体检日期","时间段","体检编码","创建日期"};
String[] englishTitleList={"examUserName","identificationType","identificationNum","tel","sex","birthday",
"areaName","centerName","itemName","examinationDate","durationName","examCode","createTime" };
Integer[] widths = {3000,2000,7000,4000,2000,4000,7000,10000,10000,4000,3000,3000,6000 };
List<HashMap<String, String>> recordList = new ArrayList<HashMap<String, String>>();
for (BhExaminationOrderRecord bhExaminationOrderRecord : orderRecordList) {
String identificationType = bhExaminationOrderRecord.getIdentificationType();
String type="其他";
if("S".equalsIgnoreCase(identificationType)){
type="身份证";
}else if("H".equalsIgnoreCase(identificationType)){
type="护照";
}else if("J".equalsIgnoreCase(identificationType)){
type="军官证";
}else{
type="其他";
}
String sex=bhExaminationOrderRecord.getSex();
String xb="";
if("F".equalsIgnoreCase(sex)){
xb="女";
}else if("M".equalsIgnoreCase(sex)){
xb="男";
}
HashMap<String, String> map = new HashMap<>();
map.put("examUserName", bhExaminationOrderRecord.getExamUserName());
map.put("identificationType", type);
map.put("identificationNum", bhExaminationOrderRecord.getIdentificationNum());
map.put("tel", bhExaminationOrderRecord.getMobilePhone());
map.put("sex",xb);
map.put("birthday", bhExaminationOrderRecord.getBirthday());
map.put("areaName", bhExaminationOrderRecord.getAreaName());
map.put("centerName", bhExaminationOrderRecord.getCenterName());
map.put("itemName", bhExaminationOrderRecord.getItemName());
map.put("examinationDate", bhExaminationOrderRecord.getExaminationDate());
map.put("durationName", bhExaminationOrderRecord.getDurationName());
map.put("examCode", Integer.toString(bhExaminationOrderRecord.getExamCode()));
map.put("createTime",fmt.format(bhExaminationOrderRecord.getCreateTime()));
recordList.add(map);
}
String sheetTitle = "体检预约记录";
OutputStream ouputStream = response.getOutputStream();
ExcelUtil.getExcelStreamByHssf(chineseTitleList, englishTitleList, widths, recordList, sheetTitle, ouputStream);
ouputStream.flush();
ouputStream.close();
} catch (Exception e) {
logger.error(e,e);
}
HSSFRow row1 = sheet.createRow(0);//第一行
// 四个参数分别是:起始行,起始列,结束行,结束列
//送花eet.addMergedRegion(new Region(0, (short) 0, 0, (short) 15));
row1.setHeightInPoints(20);
HSSFCell cell1 = row1.createCell((short)8); //--->创建一个单元格
cell1.setCellStyle(style1);
cell1.setCellValue("美元汇率");
HSSFCell cell2 = row1.createCell((short)9); //--->创建一个单元格
cell2.setCellStyle(style1);
cell2.setCellValue("6.5");
HSSFRow row2 = sheet.createRow(1);//第二行
row2.setHeightInPoints(20);
HSSFCell cell3 = row2.createCell((short)8); //--->创建一个单元格
cell3.setCellStyle(style1);
cell3.setCellValue("欧元汇率");
HSSFCell cell4 = row2.createCell((short)9); //--->创建一个单元格
cell4.setCellStyle(style1);
cell4.setCellValue("7.5");
曲杨 2017/4/18 14:24:58
style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
}*/
}
=======================================================
package com.java.connect.poi;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
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.XSSFWorkbook;
public class POIFillAndColorExample {
public static void main(String[] args) throws IOException {
// Create a workbook object
Workbook workbook = new XSSFWorkbook();
// Create sheet
Sheet sheet = workbook.createSheet();
// Create a row and put some cells in it.
Row row = sheet.createRow((short) 1);
// Aqua background
CellStyle style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.AQUA.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
Cell cell = row.createCell((short) 1);
cell.setCellValue("X1");
cell.setCellStyle(style);
// Orange "foreground", foreground being the fill foreground not the
// font color.
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.AUTOMATIC.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row.createCell((short) 2);
cell.setCellValue("X2");
cell.setCellStyle(style);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.BLUE.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row.createCell((short) 3);
cell.setCellValue("X3");
cell.setCellStyle(style);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.BLUE_GREY.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row.createCell((short) 4);
cell.setCellValue("X4");
cell.setCellStyle(style);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.BRIGHT_GREEN.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row.createCell((short) 5);
cell.setCellValue("X5");
cell.setCellStyle(style);
// Create a row and put some cells in it.
Row row2 = sheet.createRow((short) 2);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.BROWN.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row2.createCell((short) 1);
cell.setCellValue("X6");
cell.setCellStyle(style);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.CORAL.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row2.createCell((short) 2);
cell.setCellValue("X7");
cell.setCellStyle(style);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.CORNFLOWER_BLUE.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row2.createCell((short) 3);
cell.setCellValue("X8");
cell.setCellStyle(style);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.DARK_BLUE.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row2.createCell((short) 4);
cell.setCellValue("X9");
cell.setCellStyle(style);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.DARK_GREEN.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row2.createCell((short) 5);
cell.setCellValue("X10");
cell.setCellStyle(style);
// Create a row and put some cells in it.
Row row3 = sheet.createRow((short) 3);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.DARK_RED.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row3.createCell((short) 1);
cell.setCellValue("X11");
cell.setCellStyle(style);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.DARK_TEAL.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row3.createCell((short) 2);
cell.setCellValue("X12");
cell.setCellStyle(style);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.DARK_YELLOW.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row3.createCell((short) 3);
cell.setCellValue("X13");
cell.setCellStyle(style);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.GOLD.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row3.createCell((short) 4);
cell.setCellValue("X14");
cell.setCellStyle(style);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.GREEN.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row3.createCell((short) 5);
cell.setCellValue("X15");
cell.setCellStyle(style);
// Create a row and put some cells in it.
Row row4 = sheet.createRow((short) 4);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row4.createCell((short) 1);
cell.setCellValue("X16");
cell.setCellStyle(style);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row4.createCell((short) 2);
cell.setCellValue("X17");
cell.setCellStyle(style);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row4.createCell((short) 3);
cell.setCellValue("X18");
cell.setCellStyle(style);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.GREY_80_PERCENT.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row4.createCell((short) 4);
cell.setCellValue("X19");
cell.setCellStyle(style);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.INDIGO.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row4.createCell((short) 5);
cell.setCellValue("X20");
cell.setCellStyle(style);
// Create a row and put some cells in it.
Row row5 = sheet.createRow((short) 5);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.LAVENDER.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row5.createCell((short) 1);
cell.setCellValue("X21");
cell.setCellStyle(style);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.LEMON_CHIFFON.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row5.createCell((short) 2);
cell.setCellValue("X22");
cell.setCellStyle(style);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row5.createCell((short) 3);
cell.setCellValue("X23");
cell.setCellStyle(style);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.LEMON_CHIFFON.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row5.createCell((short) 4);
cell.setCellValue("X24");
cell.setCellStyle(style);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row5.createCell((short) 5);
cell.setCellValue("X25");
cell.setCellStyle(style);
// Create a row and put some cells in it.
Row row6 = sheet.createRow((short) 6);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE
.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row6.createCell((short) 1);
cell.setCellValue("X26");
cell.setCellStyle(style);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.LIGHT_GREEN.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row6.createCell((short) 2);
cell.setCellValue("X27");
cell.setCellStyle(style);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.LIGHT_ORANGE.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row6.createCell((short) 3);
cell.setCellValue("X28");
cell.setCellStyle(style);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.LIGHT_TURQUOISE.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row6.createCell((short) 4);
cell.setCellValue("X29");
cell.setCellStyle(style);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.LIGHT_YELLOW.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row6.createCell((short) 5);
cell.setCellValue("X30");
cell.setCellStyle(style);
// Create a row and put some cells in it.
Row row7 = sheet.createRow((short) 7);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.LIME.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row7.createCell((short) 1);
cell.setCellValue("X31");
cell.setCellStyle(style);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.MAROON.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row7.createCell((short) 2);
cell.setCellValue("X32");
cell.setCellStyle(style);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.OLIVE_GREEN.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row7.createCell((short) 3);
cell.setCellValue("X33");
cell.setCellStyle(style);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.ORANGE.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row7.createCell((short) 4);
cell.setCellValue("X34");
cell.setCellStyle(style);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.ORCHID.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row7.createCell((short) 5);
cell.setCellValue("X35");
cell.setCellStyle(style);
// Create a row and put some cells in it.
Row row8 = sheet.createRow((short) 8);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row8.createCell((short) 1);
cell.setCellValue("X36");
cell.setCellStyle(style);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.PINK.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row8.createCell((short) 2);
cell.setCellValue("X37");
cell.setCellStyle(style);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.PLUM.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row8.createCell((short) 3);
cell.setCellValue("X38");
cell.setCellStyle(style);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.RED.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row8.createCell((short) 4);
cell.setCellValue("X39");
cell.setCellStyle(style);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.ROSE.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row8.createCell((short) 5);
cell.setCellValue("X40");
cell.setCellStyle(style);
// Create a row and put some cells in it.
Row row9 = sheet.createRow((short) 9);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.ROYAL_BLUE.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row9.createCell((short) 1);
cell.setCellValue("X41");
cell.setCellStyle(style);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.SEA_GREEN.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row9.createCell((short) 2);
cell.setCellValue("X42");
cell.setCellStyle(style);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.SKY_BLUE.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row9.createCell((short) 3);
cell.setCellValue("X43");
cell.setCellStyle(style);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.TAN.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row9.createCell((short) 4);
cell.setCellValue("X44");
cell.setCellStyle(style);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.TEAL.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row9.createCell((short) 5);
cell.setCellValue("X45");
cell.setCellStyle(style);
// Create a row and put some cells in it.
Row row10 = sheet.createRow((short) 10);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.TURQUOISE.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row10.createCell((short) 1);
cell.setCellValue("X46");
cell.setCellStyle(style);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.VIOLET.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row10.createCell((short) 2);
cell.setCellValue("X47");
cell.setCellStyle(style);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.WHITE.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row10.createCell((short) 3);
cell.setCellValue("X48");
cell.setCellStyle(style);
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row10.createCell((short) 3);
cell.setCellValue("X49");
cell.setCellStyle(style);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream(
"POIFillAndColorExample.xlsx");
workbook.write(fileOut);
fileOut.close();
}
}
The generated excel files looks like below images.