生成工具类导入导出excel java代码
时间: 2023-03-30 22:03:29 浏览: 154
可以使用Apache POI库来实现Excel的导入导出功能,具体的Java代码可以参考以下示例:
// 导入Excel文件
FileInputStream inputStream = new FileInputStream(new File("文件路径"));
Workbook workbook = new XSSFWorkbook(inputStream);
Sheet sheet = workbook.getSheetAt();
Iterator<Row> iterator = sheet.iterator();
while (iterator.hasNext()) {
Row currentRow = iterator.next();
Iterator<Cell> cellIterator = currentRow.iterator();
while (cellIterator.hasNext()) {
Cell currentCell = cellIterator.next();
System.out.print(currentCell.getStringCellValue() + "\t");
}
System.out.println();
}
inputStream.close();
// 导出Excel文件
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet1");
Row headerRow = sheet.createRow();
headerRow.createCell().setCellValue("姓名");
headerRow.createCell(1).setCellValue("年龄");
Row dataRow = sheet.createRow(1);
dataRow.createCell().setCellValue("张三");
dataRow.createCell(1).setCellValue(20);
FileOutputStream outputStream = new FileOutputStream("文件路径");
workbook.write(outputStream);
outputStream.close();
阅读全文