台账模版导入功能修复
This commit is contained in:
@@ -239,18 +239,47 @@ public class ExcelUtil {
|
||||
* @param strings 下拉内容
|
||||
*/
|
||||
public static void selectList(Workbook workbook, int firstCol, int lastCol, String[] strings) {
|
||||
Sheet sheet = workbook.getSheetAt(0);
|
||||
// 生成下拉列表
|
||||
// 只对(x,x)单元格有效
|
||||
CellRangeAddressList cellRangeAddressList = new CellRangeAddressList(2, 65535, firstCol, lastCol);
|
||||
// 生成下拉框内容
|
||||
DataValidationHelper dvHelper = sheet.getDataValidationHelper();
|
||||
XSSFDataValidationConstraint dvConstraint = (XSSFDataValidationConstraint)
|
||||
dvHelper.createExplicitListConstraint(strings);
|
||||
XSSFDataValidation validation = (XSSFDataValidation) dvHelper.createValidation(
|
||||
dvConstraint, cellRangeAddressList);
|
||||
// 对sheet页生效
|
||||
sheet.addValidationData(validation);
|
||||
// Sheet sheet = workbook.getSheetAt(0);
|
||||
// // 生成下拉列表
|
||||
// // 只对(x,x)单元格有效
|
||||
// CellRangeAddressList cellRangeAddressList = new CellRangeAddressList(2, 65535, firstCol, lastCol);
|
||||
// // 生成下拉框内容
|
||||
// DataValidationHelper dvHelper = sheet.getDataValidationHelper();
|
||||
// XSSFDataValidationConstraint dvConstraint = (XSSFDataValidationConstraint)
|
||||
// dvHelper.createExplicitListConstraint(strings);
|
||||
// XSSFDataValidation validation = (XSSFDataValidation) dvHelper.createValidation(
|
||||
// dvConstraint, cellRangeAddressList);
|
||||
// // 对sheet页生效
|
||||
// sheet.addValidationData(validation);
|
||||
|
||||
Sheet targetSheet = workbook.getSheetAt(0);
|
||||
|
||||
// ===================== 关键:每个下拉用唯一名称的隐藏Sheet =====================
|
||||
String uniqueHiddenName = "dropdown_" + firstCol + "_" + lastCol + "_" + System.currentTimeMillis();
|
||||
Sheet hiddenSheet = workbook.createSheet(uniqueHiddenName);
|
||||
// 隐藏数据源Sheet
|
||||
workbook.setSheetHidden(workbook.getSheetIndex(hiddenSheet), true);
|
||||
|
||||
// 写入选项到隐藏Sheet
|
||||
for (int i = 0; i < strings.length; i++) {
|
||||
Row row = hiddenSheet.createRow(i);
|
||||
Cell cell = row.createCell(0);
|
||||
cell.setCellValue(strings[i]);
|
||||
}
|
||||
|
||||
// 构造引用公式(无长度限制)
|
||||
String formula = uniqueHiddenName + "!$A$1:$A$" + strings.length;
|
||||
|
||||
// 下拉作用范围:第3行 ~ 65536行
|
||||
CellRangeAddressList range = new CellRangeAddressList(2, 65535, firstCol, lastCol);
|
||||
DataValidationHelper helper = targetSheet.getDataValidationHelper();
|
||||
DataValidationConstraint constraint = helper.createFormulaListConstraint(formula);
|
||||
DataValidation validation = helper.createValidation(constraint, range);
|
||||
|
||||
// 必加:防止下拉冲突、不显示
|
||||
validation.setShowErrorBox(true);
|
||||
validation.setShowPromptBox(true);
|
||||
targetSheet.addValidationData(validation);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,13 +4,15 @@ import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
|
||||
import com.njcn.common.pojo.exception.BusinessException;
|
||||
import org.apache.poi.ss.usermodel.Workbook;
|
||||
import org.apache.poi.ss.usermodel.*;
|
||||
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
|
||||
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @author hongawen
|
||||
@@ -76,6 +78,59 @@ public class PoiUtil {
|
||||
fileName = URLEncoder.encode(fileName, CharsetUtil.UTF_8);
|
||||
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
|
||||
response.setContentType("application/octet-stream;charset=" + CharsetUtil.UTF_8);
|
||||
//将带*号的列变成红色
|
||||
Sheet sheetAt = workbook.getSheetAt(0);
|
||||
//获取列数
|
||||
int physicalNumberOfCells = sheetAt.getRow(0).getPhysicalNumberOfCells();
|
||||
//没有表格标题,只有表格头
|
||||
for (int i = 0; i < 2; i++) {
|
||||
//获取行
|
||||
Row row = sheetAt.getRow(i);
|
||||
if (Objects.isNull(row)) {
|
||||
continue;
|
||||
}
|
||||
for (int j = 0; j < physicalNumberOfCells; j++) {
|
||||
//获取单元格对象
|
||||
Cell cell = row.getCell(j);
|
||||
//获取单元格样式对象
|
||||
CellStyle cellStyle = workbook.createCellStyle();
|
||||
//获取单元格内容对象
|
||||
Font font = workbook.createFont();
|
||||
font.setFontHeightInPoints((short) 12);
|
||||
//一定要装入 样式中才会生效
|
||||
cellStyle.setFont(font);
|
||||
//设置居中对齐
|
||||
cellStyle.setAlignment(HorizontalAlignment.CENTER);
|
||||
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
||||
//设置单元格字体颜色
|
||||
cell.setCellStyle(cellStyle);
|
||||
//获取当前值
|
||||
String stringCellValue = cell.getStringCellValue();
|
||||
if (stringCellValue.contains("*")) {
|
||||
// 创建一个富文本
|
||||
XSSFRichTextString xssfRichTextString = new XSSFRichTextString(stringCellValue);
|
||||
int startIndex = stringCellValue.indexOf("*");
|
||||
int entIndex = stringCellValue.lastIndexOf("*");
|
||||
if (entIndex != 0) {
|
||||
Font font3 = workbook.createFont();
|
||||
font3.setFontHeightInPoints((short) 12);
|
||||
font3.setColor(Font.COLOR_NORMAL);
|
||||
xssfRichTextString.applyFont(0, entIndex, font3);
|
||||
}
|
||||
//设置带*样式
|
||||
Font font1 = workbook.createFont();
|
||||
font1.setFontHeightInPoints((short) 12);
|
||||
font1.setColor(Font.COLOR_RED);
|
||||
xssfRichTextString.applyFont(startIndex, entIndex + 1, font1);
|
||||
//其他样式
|
||||
Font font2 = workbook.createFont();
|
||||
font2.setFontHeightInPoints((short) 12);
|
||||
font2.setColor(Font.COLOR_NORMAL);
|
||||
xssfRichTextString.applyFont(entIndex + 1, stringCellValue.length(), font2);
|
||||
cell.setCellValue(xssfRichTextString);
|
||||
}
|
||||
}
|
||||
}
|
||||
workbook.write(outputStream);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
|
||||
Reference in New Issue
Block a user