一、簡介
Apache POI是一個用於創建、讀取和操作Microsoft Office格式文件的Java庫。它支持的文件格式包括Excel、Word和PowerPoint等。通過Apache POI,我們可以輕鬆地讀取、寫入或修改Excel文件、Word文檔等Microsoft Office格式文件。
Apache POI的核心API是HSSF(Horrible Spreadsheet Format)、XSSF(XML Spreadsheet Format)和SXSSF(Streaming Usermodel API)。它們支持不同的Excel格式文件,可以根據實際需求進行選擇。另外,Apache POI還支持對word文檔進行讀寫操作。下面將詳細介紹如何使用Apache POI進行Office文檔的操作。
二、讀取Excel文件
對於讀取Excel文件,Apache POI提供了HSSF和XSSF兩個核心API。下面以HSSF為例進行介紹。
1. 讀取Excel文件中的數據
public static void readExcelFile(String filePath) throws IOException {
FileInputStream fileInputStream = new FileInputStream(filePath);
Workbook workbook = new HSSFWorkbook(fileInputStream);
// 獲取第一個sheet
Sheet sheet = workbook.getSheetAt(0);
for (int i = 0; i <= sheet.getLastRowNum(); i++) {
Row row = sheet.getRow(i);
for (int j = 0; j < row.getLastCellNum(); j++) {
Cell cell = row.getCell(j);
System.out.print(cell.getStringCellValue() + "\t");
}
System.out.println();
}
fileInputStream.close();
}
上面的代碼中,首先通過HSSFWorkbook類讀取Excel文件,然後獲取第一個sheet。接着,遍歷每一行的每一個單元格,並輸出其值。
2. 讀取Excel文件中的特定數據
public static void readExcelFileWithCondition(String filePath, String condition) throws IOException {
FileInputStream fileInputStream = new FileInputStream(filePath);
Workbook workbook = new HSSFWorkbook(fileInputStream);
// 獲取第一個sheet
Sheet sheet = workbook.getSheetAt(0);
for (int i = 0; i <= sheet.getLastRowNum(); i++) {
Row row = sheet.getRow(i);
for (int j = 0; j < row.getLastCellNum(); j++) {
Cell cell = row.getCell(j);
if (cell.getStringCellValue().contains(condition)) {
System.out.println("找到特定數據:" + cell.getStringCellValue());
break;
}
}
}
fileInputStream.close();
}
上面的代碼中,通過參數傳入特定數據的條件,然後在讀取Excel文件時進行判斷,如果單元格中包含該條件,則輸出該單元格的值。
三、寫入Excel文件
對於寫入Excel文件,Apache POI同樣提供了HSSF和XSSF兩個核心API。下面以HSSF為例進行介紹。
1. 寫入Excel文件中的數據
public static void writeExcelFile(String filePath) throws IOException {
Workbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet("sheet1");
Row row = sheet.createRow(0);
row.createCell(0).setCellValue("姓名");
row.createCell(1).setCellValue("年齡");
row = sheet.createRow(1);
row.createCell(0).setCellValue("張三");
row.createCell(1).setCellValue(18);
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
workbook.write(fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
}
上面的代碼中,首先創建一個HSSFWorkbook對象,然後創建一個名為”sheet1″的sheet。接着,創建第一行,並設置單元格的值。最後,將文件保存到指定路徑下。
2. 寫入Excel文件中的數據並設置樣式
public static void writeExcelFileWithStyle(String filePath) throws IOException{
Workbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet("sheet1");
// 創建樣式
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
// 第一行
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("姓名");
cell.setCellStyle(cellStyle);
cell = row.createCell(1);
cell.setCellValue("年齡");
cell.setCellStyle(cellStyle);
// 數據行
row = sheet.createRow(1);
cell = row.createCell(0);
cell.setCellValue("張三");
cell = row.createCell(1);
cell.setCellValue(18);
// 保存文件
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
workbook.write(fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
}
上面的代碼中,首先創建一個HSSFWorkbook對象,然後創建一個名為”sheet1″的sheet。接着,創建樣式並應用到第一行的單元格中。最後,將文件保存到指定路徑下。
四、操作Word文檔
除了Excel文件,Apache POI還支持對Word文檔的讀寫操作。下面以讀取Word文檔為例進行介紹。
public static void readWordFile(String filePath) throws IOException {
FileInputStream fileInputStream = new FileInputStream(filePath);
XWPFDocument xwpfDocument = new XWPFDocument(fileInputStream);
List paragraphs = xwpfDocument.getParagraphs();
for (XWPFParagraph paragraph : paragraphs) {
System.out.println(paragraph.getText());
}
fileInputStream.close();
}
上面的代碼中,首先通過XWPFDocument類讀取Word文件,然後獲取所有段落,並輸出每個段落的內容。
五、小結
通過本文的介紹,我們了解了Apache POI庫的基本使用。可以看出,Apache POI大大簡化了Office文檔的操作,讓Java程序員更加輕鬆地處理Excel、Word等文檔。當然,Apache POI還提供了豐富的API,可以滿足更加複雜的場景需求。希望本文能夠幫助讀者更好地掌握Apache POI的使用。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/272302.html