|
|
|
@ -1,11 +1,14 @@
|
|
|
|
|
package com.dsideal.QingLong.Util;
|
|
|
|
|
|
|
|
|
|
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
|
|
|
|
|
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
|
|
|
|
|
import org.apache.poi.ss.usermodel.*;
|
|
|
|
|
import org.apache.poi.ss.usermodel.DateUtil;
|
|
|
|
|
import org.apache.poi.ss.util.CellRangeAddress;
|
|
|
|
|
import org.apache.poi.ss.util.CellRangeAddressList;
|
|
|
|
|
import org.apache.poi.xssf.usermodel.*;
|
|
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
@ -283,23 +286,21 @@ public class PoiUtil {
|
|
|
|
|
* @param firstRow
|
|
|
|
|
* @param lastRow
|
|
|
|
|
*/
|
|
|
|
|
public static void setIntegerStyle(Workbook wb, int sheetIdx, int colIdx, int firstRow, int lastRow) {
|
|
|
|
|
// 设置整数类型的样式
|
|
|
|
|
CellStyle integerStyle = wb.createCellStyle();
|
|
|
|
|
integerStyle.setDataFormat((short) BuiltinFormats.getBuiltinFormat("0")); // 设置为整数类型
|
|
|
|
|
|
|
|
|
|
for (int i = firstRow; i <= lastRow; i++) {
|
|
|
|
|
Row row = wb.getSheetAt(sheetIdx).getRow(i);
|
|
|
|
|
if (row == null) row=wb.getSheetAt(sheetIdx).createRow(i);
|
|
|
|
|
// 设置整数类型的值
|
|
|
|
|
Cell integerCell = row.getCell(colIdx);
|
|
|
|
|
if (integerCell == null) {
|
|
|
|
|
integerCell = row.createCell(colIdx);
|
|
|
|
|
}
|
|
|
|
|
integerCell.setCellStyle(integerStyle); // 应用整数类型的样式
|
|
|
|
|
}
|
|
|
|
|
public static void setIntegerStyle(XSSFWorkbook wb, int sheetIdx, int colIdx, int firstRow, int lastRow) {
|
|
|
|
|
XSSFSheet sheet = wb.getSheetAt(sheetIdx);
|
|
|
|
|
XSSFDataValidationHelper dvHelper = new XSSFDataValidationHelper(sheet);
|
|
|
|
|
XSSFDataValidationConstraint dvConstraint = (XSSFDataValidationConstraint) dvHelper.createNumericConstraint(
|
|
|
|
|
DataValidationConstraint.ValidationType.INTEGER, XSSFDataValidationConstraint.OperatorType.BETWEEN,
|
|
|
|
|
String.valueOf(Integer.MIN_VALUE), String.valueOf(Integer.MAX_VALUE)
|
|
|
|
|
);
|
|
|
|
|
CellRangeAddressList addressList = new CellRangeAddressList(firstRow, lastRow, colIdx, colIdx);
|
|
|
|
|
XSSFDataValidation validation = (XSSFDataValidation) dvHelper.createValidation(dvConstraint, addressList);
|
|
|
|
|
validation.setSuppressDropDownArrow(false);
|
|
|
|
|
validation.setShowErrorBox(true);
|
|
|
|
|
sheet.addValidationData(validation);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:设置浮点数类型格式
|
|
|
|
|
*
|
|
|
|
@ -309,20 +310,18 @@ public class PoiUtil {
|
|
|
|
|
* @param firstRow
|
|
|
|
|
* @param lastRow
|
|
|
|
|
*/
|
|
|
|
|
public static void setFloatStyle(Workbook wb, int sheetIdx, int colIdx, int firstRow, int lastRow) {
|
|
|
|
|
// 设置浮点数类型的样式
|
|
|
|
|
CellStyle floatStyle = wb.createCellStyle();
|
|
|
|
|
floatStyle.setDataFormat((short) BuiltinFormats.getBuiltinFormat("0.00")); // 设置为浮点数类型,保留两位小数
|
|
|
|
|
for (int i = firstRow; i <= lastRow; i++) {
|
|
|
|
|
Row row = wb.getSheetAt(sheetIdx).getRow(i);
|
|
|
|
|
if (row == null) row=wb.getSheetAt(sheetIdx).createRow(i);
|
|
|
|
|
// 设置浮点数类型的值
|
|
|
|
|
Cell floatCell = row.getCell(colIdx);
|
|
|
|
|
if (floatCell == null) {
|
|
|
|
|
floatCell = row.createCell(colIdx);
|
|
|
|
|
}
|
|
|
|
|
floatCell.setCellStyle(floatStyle); // 应用浮点数类型的样式
|
|
|
|
|
}
|
|
|
|
|
public static void setFloatStyle(XSSFWorkbook wb, int sheetIdx, int colIdx, int firstRow, int lastRow) {
|
|
|
|
|
XSSFSheet sheet = wb.getSheetAt(sheetIdx);
|
|
|
|
|
XSSFDataValidationHelper dvHelper = new XSSFDataValidationHelper(sheet);
|
|
|
|
|
XSSFDataValidationConstraint dvConstraint = (XSSFDataValidationConstraint) dvHelper.createNumericConstraint(
|
|
|
|
|
DataValidationConstraint.ValidationType.DECIMAL, XSSFDataValidationConstraint.OperatorType.BETWEEN,
|
|
|
|
|
String.valueOf(Float.MIN_VALUE), String.valueOf(Float.MAX_VALUE)
|
|
|
|
|
);
|
|
|
|
|
CellRangeAddressList addressList = new CellRangeAddressList(firstRow, lastRow, colIdx, colIdx);
|
|
|
|
|
XSSFDataValidation validation = (XSSFDataValidation) dvHelper.createValidation(dvConstraint, addressList);
|
|
|
|
|
validation.setSuppressDropDownArrow(false);
|
|
|
|
|
validation.setShowErrorBox(true);
|
|
|
|
|
sheet.addValidationData(validation);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -334,19 +333,32 @@ public class PoiUtil {
|
|
|
|
|
* @param firstRow
|
|
|
|
|
* @param lastRow
|
|
|
|
|
*/
|
|
|
|
|
public static void setDateStyle(Workbook wb, int sheetIdx, int colIdx, int firstRow, int lastRow) {
|
|
|
|
|
// 设置日期类型的样式
|
|
|
|
|
CellStyle dateStyle = wb.createCellStyle();
|
|
|
|
|
dateStyle.setDataFormat(wb.getCreationHelper().createDataFormat().getFormat("yyyy-mm-dd")); // 设置为日期类型,格式为yyyy-mm-dd
|
|
|
|
|
for (int i = firstRow; i <= lastRow; i++) {
|
|
|
|
|
Row row = wb.getSheetAt(sheetIdx).getRow(i);
|
|
|
|
|
if (row == null) row=wb.getSheetAt(sheetIdx).createRow(i);
|
|
|
|
|
// 设置日期类型的值
|
|
|
|
|
Cell dateCell = row.getCell(colIdx);
|
|
|
|
|
if (dateCell == null) {
|
|
|
|
|
dateCell = row.createCell(colIdx);
|
|
|
|
|
}
|
|
|
|
|
dateCell.setCellStyle(dateStyle); // 应用日期类型的样式
|
|
|
|
|
}
|
|
|
|
|
public static void setDateStyle(XSSFWorkbook wb, int sheetIdx, int colIdx, int firstRow, int lastRow) {
|
|
|
|
|
XSSFSheet sheet = wb.getSheetAt(sheetIdx);
|
|
|
|
|
XSSFDataValidationHelper dvHelper = new XSSFDataValidationHelper(sheet);
|
|
|
|
|
DataValidationConstraint dvConstraint = dvHelper
|
|
|
|
|
.createDateConstraint(DataValidationConstraint.OperatorType.BETWEEN, "date(1900,1,1)", "date(2099,12,31)", "yyyy/MM/dd");
|
|
|
|
|
CellRangeAddressList addressList = new CellRangeAddressList(firstRow, lastRow, colIdx, colIdx);
|
|
|
|
|
XSSFDataValidation validation = (XSSFDataValidation) dvHelper.createValidation(dvConstraint, addressList);
|
|
|
|
|
validation.setSuppressDropDownArrow(false);
|
|
|
|
|
validation.setShowErrorBox(true);
|
|
|
|
|
sheet.addValidationData(validation);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) throws IOException {
|
|
|
|
|
InputStream is = new FileInputStream("D:\\dsWork\\QingLong\\src\\main\\resource\\Excel\\sheet.xlsx");
|
|
|
|
|
XSSFWorkbook wb = new XSSFWorkbook(is);
|
|
|
|
|
|
|
|
|
|
setIntegerStyle(wb, 0, 2, 0, 20000);
|
|
|
|
|
|
|
|
|
|
setFloatStyle(wb, 0, 3, 0, 20000);
|
|
|
|
|
|
|
|
|
|
setDateStyle(wb,0,4,0,20000);
|
|
|
|
|
//保存
|
|
|
|
|
FileOutputStream fileOut = new FileOutputStream("D:\\dsWork\\QingLong\\src\\main\\resource\\Excel\\b61a2af2-223f-4058-a675-b212e4dd9487_finish.xlsx");
|
|
|
|
|
wb.write(fileOut);
|
|
|
|
|
//关闭Excel
|
|
|
|
|
wb.close();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|