📅  最后修改于: 2023-12-03 15:41:42.154000             🧑  作者: Mango
Apache POI是一个用于操作各种Microsoft Office格式文件的Java API。包括:Excel,Word,PowerPoint等。
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
try {
FileInputStream fileInputStream = new FileInputStream(new File("path/to/file.xlsx"));
XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream);
XSSFSheet sheet = workbook.getSheetAt(0);
for (Row row : sheet) {
for (Cell cell : row) {
switch (cell.getCellType()) {
case STRING:
System.out.print(cell.getStringCellValue() + "\t");
break;
case NUMERIC:
System.out.print(cell.getNumericCellValue() + "\t");
break;
default:
System.out.println();
}
}
System.out.println();
}
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("Sheet1");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("Hello, World!");
try {
FileOutputStream outputStream = new FileOutputStream("path/to/file.xlsx");
workbook.write(outputStream);
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
XWPFDocument document = new XWPFDocument(new FileInputStream(new File("path/to/file.docx")));
for (XWPFParagraph para : document.getParagraphs()) {
System.out.println(para.getText());
}
document.close();
} catch (IOException e) {
e.printStackTrace();
}
XWPFDocument document = new XWPFDocument();
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText("Hello, World!");
try {
FileOutputStream outputStream = new FileOutputStream("path/to/file.docx");
document.write(outputStream);
outputStream.close();
document.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
XMLSlideShow slideShow = new XMLSlideShow(new FileInputStream(new File("path/to/file.pptx")));
for (XSLFSlide slide : slideShow.getSlides()) {
for (XSLFShape shape : slide.getShapes()) {
if (shape instanceof XSLFTextShape) {
System.out.println(((XSLFTextShape) shape).getText());
}
}
}
slideShow.close();
} catch (IOException e) {
e.printStackTrace();
}
XMLSlideShow slideShow = new XMLSlideShow();
XSLFSlide slide = slideShow.createSlide();
XSLFTextShape shape = slide.createTextBox();
shape.setText("Hello, World!");
try {
FileOutputStream outputStream = new FileOutputStream("path/to/file.pptx");
slideShow.write(outputStream);
outputStream.close();
slideShow.close();
} catch (IOException e) {
e.printStackTrace();
}
Apache POI是一个强大的Java API,用于操作各种Microsoft Office格式文件。它提供了易于使用的API和简化工具,可帮助程序员快速读取、创建和编辑Excel、Word和PowerPoint文件。如果你想扩展应用程序的功能并增强用户交互性,那么就试试Apache POI吧!