Java读取写入Excel表格数据

	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;
    }

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值