You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

491 lines
15 KiB
Java

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.chenxuan.constants;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
/**
* excel util
* @author Jordan_Li
*
*/
public class ExcelUtils {
public static final String OFFICE_EXCEL_2003_POSTFIX = "xls";
public static final String OFFICE_EXCEL_2010_POSTFIX = "xlsx";
public static final String EMPTY = "";
public static final String POINT = ".";
public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
/**
* get path suffix
*
* @param path
* @return
*/
public static String getPostfix(String path) {
if (path == null || EMPTY.equals(path.trim())) {
return EMPTY;
}
if (path.contains(POINT)) {
return path.substring(path.lastIndexOf(POINT) + 1, path.length());
}
return EMPTY;
}
/**
* cell style
*
* @param hssfCell
* @return
*/
@SuppressWarnings({ "static-access" })
public static String getHValue(HSSFCell hssfCell) {
if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
return String.valueOf(hssfCell.getBooleanCellValue());
} else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {
String cellValue = "";
if (HSSFDateUtil.isCellDateFormatted(hssfCell)) {
Date date = HSSFDateUtil.getJavaDate(hssfCell.getNumericCellValue());
cellValue = sdf.format(date);
} else {
DecimalFormat df = new DecimalFormat("#.##");
cellValue = df.format(hssfCell.getNumericCellValue());
String strArr = cellValue.substring(cellValue.lastIndexOf(POINT) + 1, cellValue.length());
if (strArr.equals("00")) {
cellValue = cellValue.substring(0, cellValue.lastIndexOf(POINT));
}
}
return cellValue;
} else {
return String.valueOf(hssfCell.getStringCellValue());
}
}
/**
* cell style
*
* @param xssfCell
* @return
*/
public static String getXValue(XSSFCell xssfCell) {
if (xssfCell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
return String.valueOf(xssfCell.getBooleanCellValue());
} else if (xssfCell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
String cellValue = "";
if (DateUtil.isCellDateFormatted(xssfCell)) {
Date date = DateUtil.getJavaDate(xssfCell.getNumericCellValue());
cellValue = sdf.format(date);
} else {
DecimalFormat df = new DecimalFormat("#.##");
cellValue = df.format(xssfCell.getNumericCellValue());
String strArr = cellValue.substring(cellValue.lastIndexOf(POINT) + 1, cellValue.length());
if (strArr.equals("00")) {
cellValue = cellValue.substring(0, cellValue.lastIndexOf(POINT));
}
}
return cellValue;
} else {
return String.valueOf(xssfCell.getStringCellValue());
}
}
/**
* create row data
*
* @param currentRow
*
* @param textList
*
*/
public static void creatRow(HSSFRow currentRow, String[] textList) {
if (textList != null && textList.length > 0) {
int i = 0;
for (String cellValue : textList) {
HSSFCell userNameLableCell = currentRow.createCell(i++);
userNameLableCell.setCellValue(cellValue);
}
}
}
public static void creatborderRow(HSSFWorkbook hwb, HSSFRow currentRow,
String[] textList, HSSFCellStyle unLock, HSSFCellStyle locked) {
if (textList != null && textList.length > 0) {
int i = 0;
for (String cellValue : textList) {
HSSFCell userNameLableCell = currentRow.createCell(i++);
userNameLableCell.setCellValue(cellValue);
}
}
}
public static void creatRow(HSSFRow currentRow, Object[] textList) {
if (textList != null && textList.length > 0) {
int i = 0;
for (Object cellValue : textList) {
HSSFCell userNameLableCell = currentRow.createCell(i++);
userNameLableCell.setCellValue(cellValue.toString());
}
}
}
public static void creatFlexibleRow(HSSFRow currentRow, String[] textList,
int beginNum, int endNum) {
if (textList != null && textList.length > 0) {
int i = 0;
for (int j = beginNum; j < textList.length - endNum; j++) {
HSSFCell userNameLableCell = currentRow.createCell(i++);
userNameLableCell.setCellValue(textList[j]);
}
}
}
public static void creatFlexibleRow(HSSFRow currentRow, Object[] textList,
int beginNum, int endNum) {
if (textList != null && textList.length > 0) {
int i = 0;
for (int j = beginNum; j < textList.length - endNum; j++) {
HSSFCell userNameLableCell = currentRow.createCell(i++);
userNameLableCell.setCellValue(textList[j].toString());
}
}
}
public static void creatFlexibleRow(HSSFRow currentRow, Object[] textList,
int beginNum, int endNum, int CellNum, String CellValue) {
if (textList != null && textList.length > 0) {
HSSFCell userNameLableCellT = currentRow.createCell(0);
userNameLableCellT.setCellValue(CellValue);
for (int j = beginNum; j < textList.length - endNum; j++) {
HSSFCell userNameLableCell = currentRow.createCell(CellNum++);
userNameLableCell.setCellValue(textList[j].toString());
}
}
}
/**
* create row data with style
*
* @param currentRow
* @param textList
*/
public static void creatStyleRow(HSSFRow currentRow, String[] textList,
HSSFWorkbook workbook) {
HSSFCellStyle style = workbook.createCellStyle();
/*style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
style.setFillForegroundColor(HSSFColor.LAVENDER.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setBorderBottom(HSSFCellStyle.BORDER_DOTTED);
style.setBorderLeft(HSSFCellStyle.BORDER_DOTTED);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);*/
HSSFFont font = workbook.createFont();
font.setFontName("Verdana");
font.setColor(HSSFColor.BLUE.index);
style.setFont(font);
if (textList != null && textList.length > 0) {
int i = 0;
for (String cellValue : textList) {
HSSFCell userNameLableCell = currentRow.createCell(i++);
userNameLableCell.setCellStyle(style);
userNameLableCell.setCellValue(cellValue);
}
}
}
/**
* create row data with no color
*
* @param currentRow
* @param textList
*/
public static void creatNoColorRow(Row currentRow, String[] textList,
Workbook workbook) {
if (textList != null && textList.length > 0) {
int i = 0;
for (String cellValue : textList) {
Cell userNameLableCell = currentRow.createCell(i++);
currentRow.setHeightInPoints(20);
userNameLableCell.setCellValue(cellValue);
}
}
}
/**
* data valid list
* @param selectTextList
* @param naturalRowIndex
* @param naturalColumnIndex
* @return
*/
public static DataValidation getDataValidationList(String[] selectTextList,
int naturalRowIndex, int naturalColumnIndex) {
DVConstraint constraint = DVConstraint
.createExplicitListConstraint(selectTextList);
int firstRow = naturalRowIndex - 1;
int lastRow = naturalRowIndex - 1;
int firstCol = naturalColumnIndex - 1;
int lastCol = naturalColumnIndex - 1;
CellRangeAddressList regions = new CellRangeAddressList(firstRow,
lastRow, firstCol, lastCol);
DataValidation data_validation_list = new HSSFDataValidation(regions,
constraint);
return data_validation_list;
}
/**
* data valid list
*
* @param formulaString
* @param naturalRowIndex
* @param naturalColumnIndex
* @return
*/
public static DataValidation getDataValidationByFormula(
String formulaString, int naturalRowIndex, int naturalColumnIndex) {
DVConstraint constraint = DVConstraint
.createFormulaListConstraint(formulaString);
int firstRow = naturalRowIndex - 1;
int lastRow = naturalRowIndex - 1;
int firstCol = naturalColumnIndex - 1;
int lastCol = naturalColumnIndex - 1;
CellRangeAddressList regions = new CellRangeAddressList(firstRow,
lastRow, firstCol, lastCol);
DataValidation data_validation_list = new HSSFDataValidation(regions,
constraint);
return data_validation_list;
}
/**
* create excel name
* @param workbook
*
* @param fistMenuList
*
* @param secondMenuList
*/
public static void creatExcelNameList(HSSFWorkbook workbook,
String[] fistMenuList, List<String[]> secondMenuList,
String hideSheetName, String workForm) {
Name name;
name = workbook.createName();
name.setNameName(workForm);
if (fistMenuList.length > 0) {
name.setRefersToFormula(hideSheetName + "!$A$1:$"
+ intToExcelIndex(fistMenuList.length) + "$1");
}
if (null != secondMenuList && secondMenuList.size() > 0) {
for (int i = 0; i < secondMenuList.size(); i++) {
String[] secondMenu = (String[]) secondMenuList.get(i);
name = workbook.createName();
name.setNameName(secondMenu[0]);
if (secondMenu.length > 0) {
name.setRefersToFormula(hideSheetName + "!$B$" + (2 + i)
+ ":$" + intToExcelIndex(secondMenu.length) + "$"
+ (2 + i) + "");
}
}
}
}
/**
* set workbook style
*
* @param workbook
* @date 2016-8-3 14:55:33
* @author mary_ma
*/
public static void setCellStyle(Workbook workbook) {
CellStyle headstyle = workbook.createCellStyle();
//居中
headstyle.setAlignment(HorizontalAlignment.CENTER);
//垂直
headstyle.setVerticalAlignment(VerticalAlignment.CENTER);
headstyle.setWrapText(true);
Font headfont = workbook.createFont();
headfont.setFontName("新宋体");
headfont.setFontHeightInPoints((short)13);
//headfont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
headstyle.setFont(headfont);
}
public static void titleStyle(Workbook wb, Row currentRow, XSSFSheet sheet, String name, int lastCol) {
//标题样式
XSSFCellStyle titleStyle = (XSSFCellStyle) wb.createCellStyle();
//居中
titleStyle.setAlignment(HorizontalAlignment.CENTER);
//垂直
titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
Font ztFont = wb.createFont();
// 设置字体为斜体字
ztFont.setItalic(false);
//将字体大小设置为18px
ztFont.setFontHeightInPoints((short)18);
//将“宋体”字体应用到当前单元格上
ztFont.setFontName("宋体");
//字体加粗
ztFont.setBold(true);
titleStyle.setFont(ztFont);
// 创建单元格excel的单元格参数为列索引可以是0255之间的任何一个
Cell cell = currentRow.createCell(0);
// 合并单元格CellRangeAddress构造参数依次表示起始行截至行起始列 截至列
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, lastCol));
// 设置单元格内容
cell.setCellValue(name);
cell.setCellStyle(titleStyle);
}
/**
* generation hide sheet
*
* @param workbook
* @param hideSheetName
* @param firstMenuList
* @param secondMenuList
*/
public static void creatHideSheet(HSSFWorkbook workbook,
String hideSheetName, String[] firstMenuList,
List<String[]> secondMenuList) {
HSSFSheet hideselectinfosheet = workbook.createSheet(hideSheetName);
HSSFRow firstMenuRow = hideselectinfosheet.createRow(0);
creatRow(firstMenuRow, firstMenuList);
if (null != secondMenuList && secondMenuList.size() > 0) {
for (int i = 0; i < secondMenuList.size(); i++) {
String[] secondMenu = (String[]) secondMenuList.get(i);
HSSFRow secondMenuRow = hideselectinfosheet.createRow(i + 1);
creatRow(secondMenuRow, secondMenu);
}
}
workbook.setSheetHidden(workbook.getSheetIndex(hideSheetName), true);
}
/**
* generation cascading drop-down menu
*
* @param userinfosheet1
* @param naturalRowIndex
*/
public static void creatAppRow(HSSFSheet userinfosheet1,
int naturalRowIndex, int firstMenuIndex, int secondMenuIndex,
String workForm) {
DataValidation data_validation_list = getDataValidationByFormula(workForm, naturalRowIndex,
firstMenuIndex);
userinfosheet1.addValidationData(data_validation_list);
DataValidation data_validation_list2 = getDataValidationByFormula("INDIRECT($"
+ intToExcelIndex(firstMenuIndex) + ""
+ naturalRowIndex + ")", naturalRowIndex,
secondMenuIndex);
userinfosheet1.addValidationData(data_validation_list2);
}
/**
* generation non-cascading drop-down menu
*
* @param userinfosheet1
* @param selectTextList
* @param naturalColumnIndex
*/
public static void creatAloneRow(HSSFSheet userinfosheet1,
String[] selectTextList, int firstRow, int lastRow,
int naturalColumnIndex) {
DVConstraint constraint = DVConstraint
.createExplicitListConstraint(selectTextList);
int firstCol = naturalColumnIndex - 1;
int lastCol = naturalColumnIndex - 1;
CellRangeAddressList regions = new CellRangeAddressList(firstRow,
lastRow, firstCol, lastCol);
DataValidation data_validation_list = new HSSFDataValidation(regions,
constraint);
userinfosheet1.addValidationData(data_validation_list);
}
/**
* alphabet convert to number
* @param i
*
* @return
*/
private static String intToExcelIndex(int i) {
String s = "";
int m = i % 26;
i = i / 26;
while (i != 0 || m != 0) {
if (m == 0) {
i--;
m = 26;
}
s = (char) (m + (char) ('A') - 1) + s;
m = i % 26;
i = i / 26;
}
return s;
}
/**
* hide row
*
* @param currentRow
* @param textList
* @param workbook
*/
public static void creatHidenRow(HSSFRow currentRow, String[] textList,
HSSFWorkbook workbook, boolean isLock) {
HSSFCellStyle style = workbook.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setLocked(isLock);
if (textList != null && textList.length > 0) {
int i = 0;
for (String cellValue : textList) {
HSSFCell userNameLableCell = currentRow.createCell(i++);
userNameLableCell.setCellStyle(style);
userNameLableCell.setCellValue(cellValue);
}
currentRow.setZeroHeight(true);
}
}
/**
* get style
* @param workbook
* @param isLock
* @return
*/
public static HSSFCellStyle getStyle(HSSFWorkbook workbook, boolean isLock) {
HSSFCellStyle headStyle = workbook.createCellStyle();
// headStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// headStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
// headStyle.setFillForegroundColor(HSSFColor.LIGHT_TURQUOISE.index);
// headStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
// headStyle.setBorderBottom(HSSFCellStyle.BORDER_DOTTED);
// headStyle.setBorderLeft(HSSFCellStyle.BORDER_DOTTED);
// headStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
// headStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
headStyle.setLocked(isLock);
headStyle.setWrapText(true);
return headStyle;
}
}