package com.testritegroup.datahub.print.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.Map.Entry;
import java.util.regex.Pattern;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Drawing;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Picture;
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.ss.usermodel.WorkbookFactory;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/**
* @className:POIExcelUtil.java
* @classDescription:POI操作类
*/
public class POIExcelUtil {
private static Log log = LogFactory.getLog(POIExcelUtil.class);
// ------------------------写Excel-----------------------------------
/**
* 创建workBook对象 xlsx(2007以上版本)
*
* @return
*/
public static Workbook createWorkbook() {
return createWorkbook(false);
}
/**
* 创建WorkBook对象
*
* @param flag
* true:xlsx(1997-2007) false:xls(2007以下)
* @return
*/
public static Workbook createWorkbook(boolean flag) {
Workbook wb;
if (flag) {
wb = new XSSFWorkbook();
} else {
wb = new HSSFWorkbook();
}
return wb;
}
/**
* 添加图片
*
* @param wb
* workBook对象
* @param sheet
* sheet对象
* @param picFileName
* 图片文件名称(全路径)
* @param picType
* 图片类型
* @param row
* 图片所在的行
* @param col
* 图片所在的列
*/
public static void addPicture(Workbook wb, Sheet sheet, String picFileName,
int picType, int row, int col) {
InputStream is = null;
try {
// 读取图片
is = new FileInputStream(picFileName);
byte[] bytes = IOUtils.toByteArray(is);
int pictureIdx = wb.addPicture(bytes, picType);
is.close();
// 写图片
CreationHelper helper = wb.getCreationHelper();
Drawing drawing = sheet.createDrawingPatriarch();
ClientAnchor anchor = helper.createClientAnchor();
// 设置图片的位置
anchor.setCol1(col);
anchor.setRow1(row);
Picture pict = drawing.createPicture(anchor, pictureIdx);
pict.resize();
} catch (Exception e) {
try {
if (is != null) {
is.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
}
/**
* 创建Cell 默认为水平和垂直方式都是居中
*
* @param style
* CellStyle对象
* @param row
* Row对象
* @param column
* 单元格所在的列
* @return
*/
public static Cell createCell(CellStyle style, Row row, short column) {
return createCell(style, row, column, XSSFCellStyle.ALIGN_CENTER,
XSSFCellStyle.ALIGN_CENTER);
}
/**
* 创建Cell并设置水平和垂直方式
*
* @param style
* CellStyle对象
* @param row
* Row对象
* @param column
* 单元格所在的列
* @param halign
* 水平对齐方式:XSSFCellStyle.VERTICAL_CENTER.
* @param valign
* 垂直对齐方式:XSSFCellStyle.ALIGN_LEFT
*/
public static Cell createCell(CellStyle style, Row row, short column,
short halign, short valign) {
Cell cell = row.createCell(column);
setAlign(style, halign, valign);
cell.setCellStyle(style);
return cell;
}
/**
* 合并单元格
*
* @param sheet
* @param firstRow
* 开始行
* @param lastRow
* 最后行
* @param firstCol
* 开始列
* @param lastCol
* 最后列
*/
public static void mergeCell(Sheet sheet, int firstRow, int lastRow,
int firstCol, int lastCol) {
sheet.addMergedRegion(new CellRangeAddress(firstRow, lastRow, firstCol,
lastCol));
}
// ---------------------------------设置样式-----------------------
/**
* 设置单元格对齐方式
*
* @param style
* @param halign
* @param valign
* @return
*/
public static CellStyle setAlign(CellStyle style, short halign, short valign) {
style.setAlignment(halign);
style.setVerticalAlignment(valign);
return style;
}
/**
* 设置单元格边框(四个方向的颜色一样)
*
* @param style
* style对象
* @param borderStyle
* 边框类型 :dished-虚线 thick-加粗 double-双重 dotted-有点的
* CellStyle.BORDER_THICK
* @param borderColor
* 颜色 IndexedColors.GREEN.getIndex()
* @return
*/
public static Ce