zoukankan      html  css  js  c++  java
  • 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();
        }
  • 相关阅读:
    第72届奥斯卡最佳动画奖《老人与海》
    关不掉的手机应用程序
    李嘉诚:知识并不一定使你的财富增加
    Linux之父Linus Torvalds谈软件开发管理经验
    Google 正式发布Dart编程语言
    代码本身其实并不重要,重要的是用户
    22个基于HTML5开发的网站
    看看耶鲁大学心态 ,送给正在奋斗的人 !
    用 git 维护 vim 代码
    谈程序语言的设计及程序员心态
  • 原文地址:https://www.cnblogs.com/feiye512/p/7793111.html
Copyright © 2011-2022 走看看