1、依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.1</version>
</dependency>
2、代码
package com.excel;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
public class ExcelUtil {
public static ByteArrayInputStream writeExcelWithTitle(List<String> title, List<List<String>> data,List<Integer> protectIndex) {
if (title == null || data == null || title.size() != data.get(0).size()) {
return null;
}
ByteArrayOutputStream out = null;
HSSFWorkbook workbook = new HSSFWorkbook();
try {
out = new ByteArrayOutputStream();
Sheet sheet = workbook.createSheet();
sheet.protectSheet("edu");
CellStyle lockStyle = workbook.createCellStyle();
lockStyle.setLocked(true);
lockStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
lockStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
CellStyle unlockStyle = workbook.createCellStyle();
unlockStyle.setLocked(false);
Row row = sheet.createRow(0);
for (int i = 0; i < title.size(); i++) {
Cell cell = row.createCell(i);
cell.setCellValue(title.get(i));
}
Map<Integer, Integer> index = null;
if(protectIndex != null){
index = protectIndex.parallelStream().collect(Collectors.toMap(e -> e, e -> e));
}
for (int j = 0; j < data.size(); j++) {
row = sheet.createRow(j + 1);
for (int i = 0; i < data.get(j).size(); i++) {
Cell cell = row.createCell(i);
if(index != null){
if(index.containsKey(i)){
cell.setCellStyle(lockStyle);
}else{
cell.setCellStyle(unlockStyle);
}
}
cell.setCellValue(data.get(j).get(i));
}
}
workbook.write(out);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (workbook != null) {
workbook.close();
}
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return new ByteArrayInputStream(out.toByteArray());
}
public static void writeExcelWithTitle(List<String> title, List<List<String>> data,List<Integer> protectIndex, String fileName) {
if (title == null || data == null || title.size() != data.get(0).size()) {
return;
}
FileOutputStream out = null;
HSSFWorkbook workbook = null;
try {
workbook = new HSSFWorkbook();
out = new FileOutputStream(fileName);
Sheet sheet = workbook.createSheet();
sheet.protectSheet("edu");
CellStyle lockStyle = workbook.createCellStyle();
lockStyle.setLocked(true);
lockStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
lockStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
CellStyle unlockStyle = workbook.createCellStyle();
unlockStyle.setLocked(false);
Map<Integer, Integer> index = null;
if(protectIndex != null){
index = protectIndex.parallelStream().collect(Collectors.toMap(e -> e, e -> e));
}
Row row = sheet.createRow(0);
for (int i = 0; i < title.size(); i++) {
Cell cell = row.createCell(i);
cell.setCellStyle(lockStyle);
cell.setCellValue(title.get(i));
}
for (int j = 0; j < data.size(); j++) {
row = sheet.createRow(j + 1);
for (int i = 0; i < data.get(j).size(); i++) {
Cell cell = row.createCell(i);
if(index.containsKey(i)){
cell.setCellStyle(lockStyle);
}else{
cell.setCellStyle(unlockStyle);
}
cell.setCellValue(data.get(j).get(i));
}
}
workbook.write(out);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (workbook != null) {
workbook.close();
}
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
List<String> title = new ArrayList<String>();
title.add("姓名");
title.add("性别");
title.add("年龄");
List<List<String>> data = new ArrayList<>();
List<String> element1 = new ArrayList<String>();
element1.add("张三");
element1.add("男");
element1.add("23");
List<String> element2 = new ArrayList<String>();
element2.add("李四");
element2.add("男");
element2.add("29");
data.add(element1);
data.add(element2);
String fileName = "D:\\test.xls";
writeExcelWithTitle(title, data, Arrays.asList(0,1),fileName);
}
}