创建 Excel 文件
准备
新建 Maven Project,引入依赖:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
创建行和列
import java.io.File;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class ExcelTests {
public static void main(String[] args) throws IOException {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Sheet 1");
for (int rowIndex = 0; rowIndex < 10; rowIndex++) {
HSSFRow row = sheet.createRow(rowIndex); // 创建行
for (int columnIndex = 0; columnIndex < 10; columnIndex++) {
HSSFCell cell = row.createCell(columnIndex); // 创建列
cell.setCellValue("R" + rowIndex + ":C" + columnIndex);
}
}
workbook.write(new File("excel.xls"));
workbook.close();
}
}
设置列宽
设置列宽(第 19 行):
import java.io.File;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class ExcelTests {
public static void main(String[] args) throws IOException {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Sheet 1"); // 工作表
for (int rowIndex = 0; rowIndex < 10; rowIndex++) {
HSSFRow row = sheet.createRow(rowIndex); // 创建行
for (int columnIndex = 0; columnIndex < 10; columnIndex++) {
if (rowIndex == 0 && columnIndex == 0) { // 首行首列
sheet.setColumnWidth(columnIndex, 256 * 20); // 设置列宽,第二个参数表示十个字符宽度
}
HSSFCell cell = row.createCell(columnIndex); // 创建列
cell.setCellValue("R" + rowIndex + ":C" + columnIndex);
}
}
workbook.write(new File("excel.xls"));
workbook.close();
}
}
注意:其他行的首列的宽度是受第一行、第一列的影响而变宽,并非我们设置的。
设置字体颜色
设置字体颜色(第 25 ~ 31 行):
import java.io.File;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.IndexedColors;
public class ExcelTests {
public static void main(String[] args) throws IOException {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Sheet 1"); // 工作表
for (int rowIndex = 0; rowIndex < 10; rowIndex++) {
HSSFRow row = sheet.createRow(rowIndex); // 创建行
for (int columnIndex = 0; columnIndex < 10; columnIndex++) {
HSSFCell cell = row.createCell(columnIndex); // 创建列
cell.setCellValue("R" + rowIndex + ":C" + columnIndex);
if (rowIndex == columnIndex) {
HSSFFont font = workbook.createFont();
font.setColor(IndexedColors.RED.getIndex()); // 字体颜色
HSSFCellStyle cellStyle = workbook.createCellStyle(); // 创建样式
cellStyle.setFont(font);
cell.setCellStyle(cellStyle); // 设置样式
}
}
}
workbook.write(new File("excel.xls"));
workbook.close();
}
}
设置网页超链接
设置网页超链接(第 18、27 ~ 29 行):
import java.io.File;
import java.io.IOException;
import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCreationHelper;
import org.apache.poi.hssf.usermodel.HSSFHyperlink;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class ExcelTests {
public static void main(String[] args) throws IOException {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Sheet 1"); // 工作表
HSSFCreationHelper creationHelper = workbook.getCreationHelper();
for (int rowIndex = 0; rowIndex < 10; rowIndex++) {
HSSFRow row = sheet.createRow(rowIndex); // 创建行
for (int columnIndex = 0; columnIndex < 10; columnIndex++) {
HSSFCell cell = row.createCell(columnIndex); // 创建列
cell.setCellValue("R" + rowIndex + ":C" + columnIndex);
if (columnIndex == 0) {
HSSFHyperlink hyperlink = creationHelper.createHyperlink(HyperlinkType.URL);
hyperlink.setAddress("https://www.baidu.com/");
cell.setHyperlink(hyperlink);
}
}
}
workbook.write(new File("excel.xls"));
workbook.close();
}
}
读取 Excel 文件
提示:以下使用的 Excel 文件来自上文最后一个。
读取行和列
import java.io.File;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
public class ExcelReadTests {
public static void main(String[] args) throws IOException {
Workbook workbook = WorkbookFactory.create(new File("excel.xls"));
int nSheets = workbook.getNumberOfSheets();
for (int sheetIndex = 0; sheetIndex < nSheets; sheetIndex++) {
Sheet sheet = workbook.getSheetAt(sheetIndex);
int nRow = sheet.getLastRowNum() + 1;
for (int rowIndex = 0; rowIndex < nRow; rowIndex++) {
Row row = sheet.getRow(rowIndex);
if (row == null)
continue;
int nColumn = row.getPhysicalNumberOfCells();
for (int columnIndex = 0; columnIndex < nColumn; columnIndex++) {
System.out.printf("%s ", row.getCell(columnIndex).getStringCellValue());
}
System.out.println();
}
}
workbook.close();
}
}
输出:
R0:C0 R0:C1 R0:C2 R0:C3 R0:C4 R0:C5 R0:C6 R0:C7 R0:C8 R0:C9
R1:C0 R1:C1 R1:C2 R1:C3 R1:C4 R1:C5 R1:C6 R1:C7 R1:C8 R1:C9
R2:C0 R2:C1 R2:C2 R2:C3 R2:C4 R2:C5 R2:C6 R2:C7 R2:C8 R2:C9
R3:C0 R3:C1 R3:C2 R3:C3 R3:C4 R3:C5 R3:C6 R3:C7 R3:C8 R3:C9
R4:C0 R4:C1 R4:C2 R4:C3 R4:C4 R4:C5 R4:C6 R4:C7 R4:C8 R4:C9
R5:C0 R5:C1 R5:C2 R5:C3 R5:C4 R5:C5 R5:C6 R5:C7 R5:C8 R5:C9
R6:C0 R6:C1 R6:C2 R6:C3 R6:C4 R6:C5 R6:C6 R6:C7 R6:C8 R6:C9
R7:C0 R7:C1 R7:C2 R7:C3 R7:C4 R7:C5 R7:C6 R7:C7 R7:C8 R7:C9
R8:C0 R8:C1 R8:C2 R8:C3 R8:C4 R8:C5 R8:C6 R8:C7 R8:C8 R8:C9
R9:C0 R9:C1 R9:C2 R9:C3 R9:C4 R9:C5 R9:C6 R9:C7 R9:C8 R9:C9
获取单元格的超链接
获取单元格的超链接(第 31 行):
import java.io.File;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
public class ExcelReadTests {
public static void main(String[] args) throws IOException {
Workbook workbook = WorkbookFactory.create(new File("excel.xls"));
int nSheets = workbook.getNumberOfSheets();
for (int sheetIndex = 0; sheetIndex < nSheets; sheetIndex++) {
Sheet sheet = workbook.getSheetAt(sheetIndex);
int nRow = sheet.getLastRowNum() + 1;
for (int rowIndex = 0; rowIndex < nRow; rowIndex++) {
Row row = sheet.getRow(rowIndex);
if (row == null)
continue;
int nColumn = row.getPhysicalNumberOfCells();
for (int columnIndex = 0; columnIndex < nColumn; columnIndex++) {
Cell cell = row.getCell(columnIndex);
System.out.printf("%s ", cell.getStringCellValue());
if (columnIndex == 0) {
System.out.printf("(%s) ", cell.getHyperlink().getAddress());
}
}
System.out.println();
}
}
workbook.close();
}
}
输出:
R0:C0 (https://www.baidu.com/) R0:C1 R0:C2 R0:C3 R0:C4 R0:C5 R0:C6 R0:C7 R0:C8 R0:C9
R1:C0 (https://www.baidu.com/) R1:C1 R1:C2 R1:C3 R1:C4 R1:C5 R1:C6 R1:C7 R1:C8 R1:C9
R2:C0 (https://www.baidu.com/) R2:C1 R2:C2 R2:C3 R2:C4 R2:C5 R2:C6 R2:C7 R2:C8 R2:C9
R3:C0 (https://www.baidu.com/) R3:C1 R3:C2 R3:C3 R3:C4 R3:C5 R3:C6 R3:C7 R3:C8 R3:C9
R4:C0 (https://www.baidu.com/) R4:C1 R4:C2 R4:C3 R4:C4 R4:C5 R4:C6 R4:C7 R4:C8 R4:C9
R5:C0 (https://www.baidu.com/) R5:C1 R5:C2 R5:C3 R5:C4 R5:C5 R5:C6 R5:C7 R5:C8 R5:C9
R6:C0 (https://www.baidu.com/) R6:C1 R6:C2 R6:C3 R6:C4 R6:C5 R6:C6 R6:C7 R6:C8 R6:C9
R7:C0 (https://www.baidu.com/) R7:C1 R7:C2 R7:C3 R7:C4 R7:C5 R7:C6 R7:C7 R7:C8 R7:C9
R8:C0 (https://www.baidu.com/) R8:C1 R8:C2 R8:C3 R8:C4 R8:C5 R8:C6 R8:C7 R8:C8 R8:C9
R9:C0 (https://www.baidu.com/) R9:C1 R9:C2 R9:C3 R9:C4 R9:C5 R9:C6 R9:C7 R9:C8 R9:C9