如何在没有使用java覆盖的情况下将列表数据写入特定列中的现有xlsx文件

时间:2016-07-20 12:10:57

标签: java excel

我有两个&x; xlsx' files' sss.xlsx'和' abc.xlsx'。

在' sss.xlsx'文件中有一些标题(仅限列,不包含任何数据)和#abc.xlsx'文件有数据。

我想复制来自' abc'提交给' sss'通过检查列名逐列文件。如果列名匹配,则将该列数据复制到" sss.xlsx" 在同一列中(保留在sss文件中给出的列)。

我试过下面的代码。我能写,但写标题被删除。我没有得到我出错的地方。

代码:

public class Read {
     private static int flag;

    public static void main(String[] args) throws IOException{

        //Create blank workbook
          HSSFWorkbook workbook1 = new HSSFWorkbook();
          //Create a blank sheet
          HSSFSheet spreadsheet = workbook1.createSheet(
          "  Info ");

         ArrayList<String> lst = new ArrayList<String>();
         Read read = new Read();

         File f2 = new File("sss.xlsx");
         FileInputStream ios2 = new FileInputStream(f2);
         XSSFWorkbook workbook2 = new XSSFWorkbook(ios2);

         XSSFSheet sheet2 = workbook2.getSheetAt(0);
         int noOfColumns = sheet2.getRow(0).getLastCellNum();
         System.out.println(noOfColumns);
         //String temp2=f.getSheet();

         Iterator<Row> rowIterator = sheet2.iterator();
         while (rowIterator.hasNext()) {
              Row row = rowIterator.next();
              Iterator <Cell> cellIterator = row.cellIterator();
              while (cellIterator.hasNext()) {
                Cell cell1 = cellIterator.next();
                String temp2=cell1.getStringCellValue().toString();
                System.out.print(cell1.getStringCellValue() + "\n");

         lst = read.extractExcelContentByColumnIndex(temp2);

         File f3 = new File("sss.xlsx");
         FileInputStream ios = new FileInputStream(f3);
         XSSFWorkbook workbook3 = new XSSFWorkbook(ios);
       String  temp1=f3.getName();
       //String colName="Mob No";
         XSSFSheet sheet3 = workbook3.getSheetAt(0);
         //String temp2=f.getSheet();
         int columnIndex1=0;
         Iterator<Row> rowIterator3 = sheet3.iterator();
         //columndata =  new ArrayList<String>();

         while (rowIterator3.hasNext()) {

             Row row3 = rowIterator3.next();

             Iterator<Cell> cellIterator3 = row3.cellIterator();
        while (cellIterator3.hasNext()) {

         Cell cell3 = cellIterator3.next();
         String temp5=cell3.getStringCellValue().toString();
         //System.out.println(temp);
         if(temp5.equals(temp2)){
              columnIndex1 = cell1.getColumnIndex();

             System.out.println(columnIndex1);
             flag=1;
             break;
         }
             }
             if(flag==1)
             {
                 flag = 0;
                 break;

             }

         }
         while (rowIterator.hasNext()) {

             Row row6 = rowIterator.next();
            // System.out.println(row.getRowNum());
             Iterator<Cell> cellIterator6 = row6.cellIterator();
             while (cellIterator.hasNext()) {

                   Cell cell = cellIterator.next();

                 if(row.getRowNum() > 0){ //To filter column headings
                     if(cell.getColumnIndex() == columnIndex1){// To match column index

                       //  }

                     }
                 }
             }
         }
         for(int RowNum=0; RowNum<lst.size();RowNum++){

              HSSFRow row1 = spreadsheet.createRow(RowNum+1);

                  HSSFCell cell6 = row1.createCell(columnIndex1);

                  cell6.setCellValue(lst.get(RowNum).toString());

               }

             }

          //Write the workbook in file system
         FileOutputStream out = new FileOutputStream(new File("sss.xlsx"));
         // System.out.println(lst);
         workbook1.write(out);
         out.close();
     }
    }

    public  ArrayList<String> extractExcelContentByColumnIndex( String colName)
     {
            ArrayList<String> columndata = null;
            int columnIndex= 0;
            int flag=0;
            try {
                File f = new File("abc.xlsx");

                FileInputStream ios = new FileInputStream(f);
                XSSFWorkbook workbook = new XSSFWorkbook(ios);
              String  temp1=f.getName();

                XSSFSheet sheet = workbook.getSheetAt(0);
                //String temp2=f.getSheet();
                Iterator<Row> rowIterator = sheet.iterator();
                columndata =  new ArrayList<String>();

                while (rowIterator.hasNext()) {

                    Row row = rowIterator.next();

                    Iterator<Cell> cellIterator = row.cellIterator();
               while (cellIterator.hasNext()) {

                Cell cell1 = cellIterator.next();
                String temp=cell1.getStringCellValue().toString();
                //System.out.println(temp);
                if(temp.equals(colName)){
                    columnIndex=cell1.getColumnIndex();
                    flag=1;
                    break;
                }
                    }
                    if(flag==1)
                    {
                        flag = 0;
                        break;

                    }

                }
                while (rowIterator.hasNext()) {

                    Row row = rowIterator.next();
                   // System.out.println(row.getRowNum());
                    Iterator<Cell> cellIterator = row.cellIterator();
                    while (cellIterator.hasNext()) {

                          Cell cell = cellIterator.next();

                        if(row.getRowNum() > -1){ //To filter column headings
                            if(cell.getColumnIndex() == columnIndex){// To match column index
                                switch (cell.getCellType()) {
                                case Cell.CELL_TYPE_NUMERIC:

                                    columndata.add(cell.getNumericCellValue()+"");
                                    break;
                                case Cell.CELL_TYPE_STRING:

                                    columndata.add(cell.getStringCellValue());
                                    break;

                                }

                            }
                        }
                    }
                }
                ios.close();

                System.out.println(colName);
                for(String ele : columndata)
                {

                System.out.println(ele);

                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return columndata;

        }

}

提前致谢!

0 个答案:

没有答案