Apache POI (处理Excel)
- 主要的类有:
- XSSFWorkbook 整个文档对象 (XLSX格式的)
- XSSFSheet 单个sheet对象
- XSSFRow 一行对象
- XSSFCell 一个单元格对象
- HSSFWorkbook 整个文档对象 (XLS格式的)
- HSSFSheet 单个sheet对象
- HSSFRow 一行对象
- HSSFCell 一个单元格对象
写入Excel中
public static void main(String[] args) throws IOException {
writeXLSXFile();
}
public static void writeXLSXFile() throws IOException {
// Excel文档名称
String excelFileName = "D:/test.xlsx";
// Excel工作簿名称
String sheetName = "Sheet1";
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet(sheetName);
// 创建5行
for (int r = 0; r < 5; r++) {
XSSFRow row = sheet.createRow(r);
// 创建5列
for (int c = 0; c < 5; c++) {
XSSFCell cell = row.createCell(c);
// 给Excel单元格赋值
cell.setCellValue("Cell" + r + " " + c);
}
}
FileOutputStream fileOut = new FileOutputStream(excelFileName);
wb.write(fileOut);
fileOut.flush();
fileOut.close();
}
读取Excel文件
public static void main(String[] args) throws IOException {
readXLSXFile();
}
@SuppressWarnings("all")
public static void readXLSXFile() throws IOException {
InputStream input = new FileInputStream("D:/test.xlsx");
XSSFWorkbook wb = new XSSFWorkbook(input);
// 获取第一个sheet
XSSFSheet sheet = wb.getSheetAt(0);
XSSFRow row;
XSSFCell cell;
// 获取Excel的行数
Iterator rows = sheet.rowIterator();
while (rows.hasNext()) {
// 获取到Excel的单元格
row = (XSSFRow) rows.next();
Iterator cells = row.cellIterator();
while (cells.hasNext()) {
cell = (XSSFCell) cells.next();
// 判断单元格的数据类型
if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING) {
// 类型是字符串就输出字符串
System.out.println(cell.getStringCellValue() + " ");
} else if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {
// 类型是数字就输出数字
System.out.println(cell.getNumericCellValue() + " ");
}
}
}
System.out.println();
}
读取CSV文件
- CSV全称: Comma-Seperated Values文件 (逗号分隔)
- 广义CSV文件,可以由空格 / Tab键 / 分号 完成字段分隔
- 第三方包: Apache Commons CSV
- CSVFormat 文档格式
- CSVParser 解析文档
- CSVRecord 一行记录
- CSVPrinter 写入文档
导入Apache Commons CSV文件
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.5</version>
</dependency>
读取CSV文件
public static void main(String[] args) throws Exception {
readCSVWithName();
}
/**
* 用索引输出CSV文件数据
* @return void
* @throws Exception
* */
public static void readCSVWithIndex() throws Exception {
Reader in = new FileReader("C:\\Users\\Wong\\Desktop\\test.csv");
Iterable<CSVRecord> records = CSVFormat.EXCEL.parse(in);
for (CSVRecord record : records) {
// 0代表第一列
System.out.println(record.get(0));
}
}
/**
* 根据列别名遍历输出
* @return void
* @throws Exception
* */
public static void readCSVWithName() throws Exception {
Reader in = new FileReader("C:\\Users\\Wong\\Desktop\\test.csv");
// 给第一第二列取别名,根据别名遍历输出
Iterable<CSVRecord> records = CSVFormat.RFC4180
.withHeader("name1", "name2").parse(in);
for (CSVRecord record : records) {
System.out.println(record.get("name1"));
}
}
写入CSV文件
public static void main(String[] args) throws Exception {
writeCSV();
}
/**
* 写入CSV文件
* @return void
* @throws Exception
* */
public static void writeCSV() throws Exception {
try (CSVPrinter printer = new CSVPrinter(new FileWriter("person.csv"),
CSVFormat.EXCEL)) {
// 字段名
printer.printRecord("id", "name", "firstName",
"lastName", "birthday");
// 字段内容
printer.printRecord(1, "Tom", "jong", "Doe",
LocalDate.of(1977, 1, 4));
// 换行
printer.println();
printer.printRecord(2, "mary", "tang", "meyer",
LocalDate.of(1985, 3, 29));
} catch (IOException e) {
e.printStackTrace();
}
}
文件内容