用java写一个用ExcelReader和ExcelWriter 的例子
时间: 2023-03-11 21:34:08 浏览: 360
答:可以这样写:import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;public class ExcelDemo {
public static void main(String[] args) throws IOException {
// 读取Excel
File excelFile = new File("input.xlsx");
FileInputStream fis = new FileInputStream(excelFile);
XSSFWorkbook workbook = new XSSFWorkbook(fis);
Sheet sheet = workbook.getSheetAt(0);
Row row = sheet.getRow(0);
Cell cell = row.getCell(0);
String cellValue = cell.getStringCellValue();
System.out.println("Cell Value: " + cellValue);
fis.close(); // 写入Excel
FileOutputStream fos = new FileOutputStream(excelFile);
cell.setCellValue("I have changed the cell value");
workbook.write(fos);
fos.close();
}
}
阅读全文