1. POI操作Excel
1.1. 依賴
<!--操作excel-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.0</version>
</dependency>
1.2. 讀取Excel
1.2.1. Excel文件內容
1.2.2. 代碼
/**
* 讀取excel
*/
public static void readExcel() {
InputStream inputStream = null;
XSSFWorkbook xssfWorkbook = null;
try {
String past = "/操作excel.xlsx";
inputStream = new FileInputStream(past);
xssfWorkbook = new XSSFWorkbook(inputStream);
//獲取sheet的個數
int numberOfSheets = xssfWorkbook.getNumberOfSheets();
//獲取指定的sheet
System.out.println(numberOfSheets);
//通過指定名稱獲取
XSSFSheet sheet = xssfWorkbook.getSheet("筆記本");
//通過下標獲取
XSSFSheet sheetAt = xssfWorkbook.getSheetAt(1);
if (sheetAt != null) {
//最后一行有數據的
int lastRowNum = sheetAt.getLastRowNum();
XSSFRow row;
short lastCellNum;
XSSFCell cell;
for (int i = 0; i <= lastRowNum; i++) {
//獲取指定行
row = sheetAt.getRow(i);
if (row == null) {
continue;
}
//最后一列有數據的
lastCellNum = row.getLastCellNum();
for (int j = 0; j <= lastCellNum; j++) {
cell = row.getCell(j);
if (cell == null) {
continue;
}
//數據類型
CellType cellType = cell.getCellType();
//字符串
if (CellType.STRING == cellType) {
System.out.println(cell.toString());
}
//數字
else if (CellType.NUMERIC == cellType) {
try {
System.out.println(cell.getDateCellValue());
} catch (Exception e) {
System.out.println(cell.toString());
}
}
//……
else {
System.out.println(cell.toString());
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
1.2.3. 控制台輸出結果
2
便簽名稱
便簽分類
創建時間
創建人
擁有人
小明的便簽
學習便簽
Tue Sep 03 00:00:00 CST 2019
小明
小明
小明的個人便簽
個人便簽
Sun Sep 08 00:00:00 CST 2019
小明
小明
1.3. 生成excel
1.3.1. 代碼
/**
* 生成excel
*/
public static void creatExcel() {
XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
//創建一個sheet
XSSFSheet sheet1 = xssfWorkbook.createSheet("第一個新建的sheet");
//設置高度和寬度,也可以每行每列單獨分開設置
//參數為字符個數
sheet1.setDefaultColumnWidth(20);
sheet1.setDefaultRowHeight((short) (33 * 20));
//第二個參數為字符寬度的1/256
sheet1.setColumnWidth(5, 30 * 256);
//設置單元格樣式
XSSFCellStyle cellStyle = xssfWorkbook.createCellStyle();
// 字體樣式
Font fontStyle = xssfWorkbook.createFont();
fontStyle.setBold(true);
// 字體
fontStyle.setFontName("等線");
// 大小
fontStyle.setFontHeightInPoints((short) 11);
// 將字體樣式添加到單元格樣式中
cellStyle.setFont(fontStyle);
//水平居中
cellStyle.setAlignment(HorizontalAlignment.CENTER);
//垂直居中
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
//設置 單元格填充色
DefaultIndexedColorMap defaultIndexedColorMap = new DefaultIndexedColorMap();
XSSFColor clr = new XSSFColor(defaultIndexedColorMap);
byte[] bytes = {
(byte) 217,
(byte) 217,
(byte) 217
};
clr.setRGB(bytes);
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cellStyle.setFillForegroundColor(clr);
//設置單元格不為鎖定,可編輯,反是用了這個樣式的都可編輯
cellStyle.setLocked(false);
//鎖定整個sheet不可編輯
sheet1.protectSheet("1231312");
//創建一行數據
XSSFRow row;
XSSFCell cell;
row = sheet1.createRow(0);
cell = row.createCell(0);
//設值
cell.setCellValue("2");
//合並單元格
CellRangeAddress cra = new CellRangeAddress(1, 1, 0, 3); // 起始行, 終止行, 起始列, 終止列
sheet1.addMergedRegion(cra);
//設置合並單元格的樣式
// 使用RegionUtil類為合並后的單元格添加邊框
// 下邊框
RegionUtil.setBorderBottom(BorderStyle.MEDIUM_DASHED, cra, sheet1);
// 左邊框
RegionUtil.setBorderLeft(BorderStyle.MEDIUM_DASHED, cra, sheet1);
row = sheet1.getRow(1);
//設置合並單元格內的文本樣式
//但這個單元格的邊框樣式會覆蓋上面設置的合並單元格的樣式
CellUtil.getCell(row, 0).setCellStyle(cellStyle);
//設置單個單元格的樣式
row = sheet1.createRow(2);
cell = row.createCell(0);
cell.setCellStyle(cellStyle);
//設置數據校驗
//序列校驗
String[] strArray = {
"星期一",
"星期二",
"星期三"
};
XSSFDataValidationHelper dvHelper = new XSSFDataValidationHelper((XSSFSheet) sheet1);
XSSFDataValidationConstraint dvConstraint = (XSSFDataValidationConstraint) dvHelper.createExplicitListConstraint(strArray);
CellRangeAddressList addressList = new CellRangeAddressList(3, 3, 0, 2);
XSSFDataValidation validation = (XSSFDataValidation) dvHelper.createValidation(dvConstraint, addressList);
//顯示報錯提示框
validation.setShowErrorBox(true);
validation.createErrorBox("錯誤提示", "只能選擇指定的內容!");
//設置單元格右側顯示剪頭符號,顯示可用的選項,默認為true
validation.setSuppressDropDownArrow(true);
//顯示提示信息
validation.setShowPromptBox(true);
validation.createPromptBox("提示信息", "請選擇星期填入!");
sheet1.addValidationData(validation);
//保護工作薄不可被修改
xssfWorkbook.lockStructure();
//這個不知道有啥用
xssfWorkbook.lockRevision();
//鎖定excel的窗口大小,不能無限制的橫向,縱向拉伸。
xssfWorkbook.lockWindows();
xssfWorkbook.createSheet("第二個人sheet");
OutputStream outputStream = null;
try {
outputStream = new FileOutputStream("/創建excel.xlsx");
xssfWorkbook.write(outputStream);
outputStream.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
1.3.2. 生成excel文件內容
1.4. 參考
https://www.cnblogs.com/gavincoder/p/9051803.html