import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.FileInputStream; import java.io.IOException; public class ReadExcelFile { public static void main(String[] args) { try { FileInputStream file = new FileInputStream("path/to/excel.xlsx"); Workbook workbook = new XSSFWorkbook(file); Sheet sheet = workbook.getSheetAt(0); for (Row row : sheet) { for (Cell cell : row) { CellType cellType = cell.getCellType(); if (cellType == CellType.STRING) { System.out.print(cell.getStringCellValue() + "\t"); } else if (cellType == CellType.NUMERIC) { System.out.print(cell.getNumericCellValue() + "\t"); } } System.out.println(); } workbook.close(); file.close(); } catch (IOException e) { e.printStackTrace(); } } } import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.FileOutputStream; import java.io.IOException; public class WriteExcelFile { public static void main(String[] args) { Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("Sheet1"); Row row = sheet.createRow(0); Cell cell1 = row.createCell(0); cell1.setCellValue("Hello"); Cell cell2 = row.createCell(1); cell2.setCellValue("World"); try { FileOutputStream file = new FileOutputStream("path/to/excel.xlsx"); workbook.write(file); workbook.close(); file.close(); } catch (IOException e) { e.printStackTrace(); } } }


上一篇:
下一篇:
切换中文