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

@Override 
    public void adjustExcleColumnPosition(String filePath,int col) throws Exception{
        File file=new File(filePath);
        InputStream inputStream=new FileInputStream(file);
        HSSFWorkbook workBook=new HSSFWorkbook(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<col;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=new ByteArrayOutputStream();       
        workBook.write(outByte);
        byte[] contents=outByte.toByteArray();
        InputStream ins=new ByteArrayInputStream(contents);
        OutputStream out = new FileOutputStream(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();
    }
原文地址:https://www.cnblogs.com/feiye512/p/7793111.html