pom.xml
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
创建excel
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileOutputStream;
import java.io.IOException;
public class CreateExcel {
private static final String EXCEL_XLS = "xls";
private static final String EXCEL_XLSX = "xlsx";
public static void main(String args[]) {
createExcel("E:\\123.xls");
createExcel("E:\\123.xlsx");
}
private static void createExcel(String filePath) {
FileOutputStream out = null;
try {
Workbook wb = null;
if (StringUtils.substringAfterLast(filePath, ".").equals(EXCEL_XLS)) {
wb = new HSSFWorkbook();
}
if (StringUtils.substringAfterLast(filePath, ".").equals(EXCEL_XLSX)) {
wb = new XSSFWorkbook();
}
if (wb == null) {
System.out.println("文件后缀不规范,无法操作excel文件,后缀示例:" + EXCEL_XLS + ", " + EXCEL_XLSX);
return;
}
out = new FileOutputStream(filePath);
wb.createSheet();
wb.setSheetName(0, "Sheet1");
wb.write(out);
out.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.flush();
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
写入excel
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class WriteExcel {
public static void main(String args[]) {
List<Object> rowList = new ArrayList<>();
for (int i = 1; i <= 10; i++) {
List<String> cellList = new ArrayList<>();
for (int j = 1; j <= 10; j++) {
cellList.add(i + "行" + j + "列");
}
rowList.add(cellList);
}
writeExcel(rowList, "E:\\123.xls");
writeExcel(rowList, "E:\\123.xlsx");
deleteMessage(2, 5, "E:\\123.xls");
deleteMessage(6, 9, "E:\\123.xlsx");
}
public static void writeExcel(List<Object> rowList, String filePath) {
OutputStream out = null;
try {
File file = new File(filePath);
Workbook workbook = getWorkbook(file);
Sheet sheet = workbook.getSheetAt(0);
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
Font font;
CellStyle colorStyle = workbook.createCellStyle();
font = workbook.createFont();
font.setColor(IndexedColors.RED.getIndex());
colorStyle.setFont(font);
colorStyle.setAlignment(HorizontalAlignment.CENTER);
colorStyle.setVerticalAlignment(VerticalAlignment.CENTER);
CellStyle boldStyle = workbook.createCellStyle();
font = workbook.createFont();
font.setBold(true);
boldStyle.setFont(font);
boldStyle.setAlignment(HorizontalAlignment.CENTER);
boldStyle.setVerticalAlignment(VerticalAlignment.CENTER);
for (int i = 0; i < rowList.size(); i++) {
Row row = sheet.createRow(i);
List<String> cellList = (ArrayList<String>) rowList.get(i);
for (int j = 0; j < cellList.size(); j++) {
Cell cell = row.createCell(j);
cell.setCellValue(cellList.get(j));
cell.setCellStyle(cellStyle);
if (i == 0) {
cell.setCellStyle(boldStyle);
}
if (i == (rowList.size() - 1)) {
cell.setCellStyle(colorStyle);
}
}
}
sheet.setColumnWidth(0, (int) ((10 + 0.72) * 256));
out = new FileOutputStream(filePath);
workbook.write(out);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.flush();
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void deleteMessage(int rowMin, int rowMax, String filePath) {
OutputStream out = null;
try {
File file = new File(filePath);
Workbook workbook = getWorkbook(file);
Sheet sheet = workbook.getSheetAt(0);
int rowNum = sheet.getLastRowNum() + 1;
System.out.println("原始数据总行数:" + rowNum);
if (rowNum < rowMax) {
rowMax = rowNum;
}
if (rowNum < rowMin) {
System.out.println("数据量不够");
return;
}
for (int i = rowMin; i <= rowMax; i++) {
Row row = sheet.getRow(i - 1);
if (row != null) {
sheet.removeRow(row);
}
}
out = new FileOutputStream(filePath);
workbook.write(out);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.flush();
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static Workbook getWorkbook(File file) throws IOException, InvalidFormatException {
FileInputStream in = new FileInputStream(file);
return WorkbookFactory.create(in);
}
}
读取excel
import org.apache.poi.ss.usermodel.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class ReadExcel {
public static void main(String args[]) {
readExcel("E:\\123.xls");
readExcel("E:\\123.xlsx");
}
private static void readExcel(String filePath) {
FileInputStream in = null;
try {
File file = new File(filePath);
in = new FileInputStream(file);
Workbook workbook = WorkbookFactory.create(in);
int sheetCount = workbook.getNumberOfSheets();
System.out.println(filePath + "文件总共有" + sheetCount + "个工作表");
for (int n = 0; n < sheetCount; n++) {
Sheet sheet = workbook.getSheetAt(n);
int rowCount = sheet.getLastRowNum() + 1;
System.out.println("第" + (n + 1) + "个工作表" + sheet.getSheetName() + "总共有" + rowCount + "行数据");
for (int i = 0; i < rowCount; i++) {
Row row = sheet.getRow(i);
if (row != null) {
int cellCount = row.getLastCellNum();
System.out.print("[" + (i + 1) + "][" + cellCount + "]\t");
for (int j = 0; j < cellCount; j++) {
Cell cell = row.getCell(j);
if (cell != null) {
System.out.print(getValue(cell));
}
System.out.print("\t");
}
} else {
System.out.print("[" + (i + 1) + "][0]\t");
}
System.out.print("\n");
}
}
in.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private static Object getValue(Cell cell) {
Object obj = "";
switch (cell.getCellTypeEnum()) {
case BOOLEAN:
obj = cell.getBooleanCellValue();
break;
case ERROR:
obj = cell.getErrorCellValue();
break;
case NUMERIC:
obj = cell.getNumericCellValue();
break;
case STRING:
obj = cell.getStringCellValue();
break;
default:
break;
}
return obj;
}
}