java excel 第一列赋值_java poi excel操作 把第一列放到最后去

@Overridepublic void adjustExcleColumnPosition(String filePath,int col) throwsException{

File file=newFile(filePath);

InputStream inputStream=newFileInputStream(file);

HSSFWorkbook workBook=newHSSFWorkbook(inputStream);

HSSFSheet firstSheet=workBook.getSheetAt(0);for(int i=0;i<=firstSheet.getLastRowNum();i++){

HSSFRow row=firstSheet.getRow(i);//记录每行第一列 数据

String firstCellValue=row.getCell(0).getStringCellValue();for(int j=0;j

HSSFCell currentCell=row.getCell(j);if(null ==currentCell){

currentCell=row.createCell(j);

}if(j==col-1){//如果是最后一列了 把合同附件名称和编号 放到最后 循环下一行

currentCell.setCellValue(firstCellValue);continue;

}

HSSFCell nextCell=row.getCell(j+1);if(null ==nextCell){

nextCell=row.createCell(j+1);

}

String nextCellValue=nextCell.getStringCellValue();

currentCell.setCellValue(nextCellValue);

}

}for(int i=0;i<=firstSheet.getLastRowNum();i++){

HSSFRow row=firstSheet.getRow(i);

HSSFCell firstCell=row.getCell(0);

HSSFCell lastCell=row.getCell(col-1);

String firstCellValue=firstCell.getStringCellValue();

String lastCellValue=lastCell.getStringCellValue();if(null!=firstCellValue&&null!=lastCellValue){

System.out.println(firstCellValue+"----"+lastCellValue);

}

}

ByteArrayOutputStream outByte=newByteArrayOutputStream();

workBook.write(outByte);byte[] contents=outByte.toByteArray();

InputStream ins=newByteArrayInputStream(contents);

OutputStream out= newFileOutputStream(file);int bytesRead = 0;byte[] buffer = new byte[8192];while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {

out.write(buffer,0, bytesRead);

}

out.close();

ins.close();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值