public static void text() {
String finalXlsxPath = "C:\\test.xlsx";
File file = new File(finalXlsxPath);
//定义输入流对象
FileInputStream excelFileInputStream;
try {
excelFileInputStream = new FileInputStream(file);
// 拿到文件转化为JavaPoi可操纵类型
Workbook workbook = WorkbookFactory.create(excelFileInputStream);
excelFileInputStream.close();
获取excel表格
Sheet sheet = workbook.getSheetAt(0);
//获取单元格的row和cell
// 获取行
String x = sheet.getRow(6).getCell(0).toString();
Row row = sheet.getRow(6);
// 获取列
row.createCell(4);
Cell cell = row.getCell(4);
//设置单元的值
cell.setCellValue(x);
sheet = workbook.getSheetAt(1);//获取第二张工作表
Row currentrow = sheet.getRow(1);//获取第二行
Cell currentcell = currentrow.createCell(1);//创建并在第二个单元格中写入数据
currentcell.setCellValue(median);
currentrow = sheet.getRow(2);
currentcell = currentrow.createCell(1);
currentcell.setCellValue(division);
currentrow = sheet.getRow(3);
currentcell = currentrow.createCell(1);
currentcell.setCellValue(numOfK);
//写入数据
FileOutputStream excelFileOutPutStream = new FileOutputStream(file);
workbook.write(excelFileOutPutStream);
excelFileOutPutStream.flush();
excelFileOutPutStream.close();
System.out.println("指定单元格设置数据写入完成");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (EncryptedDocumentException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("数据导出成功");
}
public static Workbook getWorkbok(File file) throws IOException{
Workbook wb = null;
FileInputStream in = new FileInputStream(file);
if(file.getName().endsWith(EXCEL_XLS)){ //Excel 2003
wb = new HSSFWorkbook(in);
}else if(file.getName().endsWith(EXCEL_XLSX)){ // Excel 2007/2010
wb = new XSSFWorkbook(in);
}
return wb;
}