使用java

时间:2016-11-02 05:58:18

标签: java excel apache-poi

我是java的新手。实际上我想使用java在excel表中交换两列。我使用了代码,但我没有得到正确的输出。我添加了我的Excel工作表的截图。我想交换系统名称和日期列。

enter image description here

我还添加了错误输出的屏幕截图。我在1900年9月24日和29日而不是2016年9月1日至30日。

enter image description here

CellStyle cellStyle1 = workbook11.createCellStyle();
CreationHelper createHelper1 = workbook11.getCreationHelper();
cellStyle1.setDataFormat(createHelper1.createDataFormat().getFormat("d-mmm"));
try {
    if (file11.exists()) {
        String dt = sh1.getRow(0).getCell(1).getStringCellValue();
        if (!dt.equalsIgnoreCase("Date")) {
            Iterator<Row> rowIterator1 = sh1.iterator();
            while (rowIterator1.hasNext()) {
                Row row = rowIterator1.next();
                if (row.getCell(1).getStringCellValue().equalsIgnoreCase("Date")) {
                    Cell cl1 = row.getCell(0);
                    Cell cl2 = row.getCell(1);
                    Cell temp = row.getCell(0);
                    Cell temp1 = row.getCell(1);
                    cl1.setCellValue(temp1.getStringCellValue());
                    cl2.setCellValue(temp.getStringCellValue());
                } else {
                    Cell cl1 = row.getCell(0);
                    Cell cl2 = row.getCell(1);
                    cl2.setCellType(Cell.CELL_TYPE_STRING);
                    cl1.setCellType(Cell.CELL_TYPE_STRING);
                    Cell temp = row.getCell(0);
                    Cell temp1 = row.getCell(1);
                    cl1.setCellValue(temp1.getStringCellValue());
                    cl2.setCellValue(temp.getStringCellValue());
                    row.getCell(1).setCellStyle(cellStyle1);
                }
            }
        }
    }
}

修改
据xenteros&#39;回答我尝试了以下内容:

         CellStyle cellStyle1 = workbook11.createCellStyle();
         CreationHelper createHelper1 = workbook11.getCreationHelper();
         cellStyle1.setDataFormat(createHelper1.createDataFormat().getFormat("d-mmm"));
          try {
                if (file11.exists()) {

                  String dt = sh1.getRow(0).getCell(1).getStringCellValue();
                  if (!dt.equalsIgnoreCase("Date")) {
                  Iterator<Row> rowIterator1 = sh1.iterator();
                  while (rowIterator1.hasNext()) {
                  Row row = rowIterator1.next();
                 if (row.getCell(1).getStringCellValue().equalsIgnoreCase("Date")) {
                 Cell cl1 = row.getCell(0);
                 Cell cl2 = row.getCell(1);
                 String temp = new String(cl2.getStringCellValue());
                 cl2.setCellValue(cl1.getStringCellValue());
                 cl1.setCellValue(temp);
          else {

                   Cell cl1 = row.getCell(0);
                   Cell cl2 = row.getCell(1);
                   cl2.setCellType(Cell.CELL_TYPE_STRING);
                   cl1.setCellType(Cell.CELL_TYPE_STRING);
                   String temp = new String(cl2.getStringCellValue());
                   System.out.println(temp);
                   java.util.Date temp2 = cl1.getDateCellValue();
                   cl2.setCellStyle(cellStyle1);
                   cl2.setCellValue(temp2);
                   cl1.setCellType(Cell.CELL_TYPE_STRING);
                   cl1.setCellValue(temp);
           }
}

2 个答案:

答案 0 :(得分:2)

你的问题主要是关于java引用或关于swap算法。

Memory:
+-------------+--------------------+--------+--------+
|   cell 1    |       cell 2       | cell 3 | cell 4 |
+-------------+--------------------+--------+--------+
| ["Date"...] | ["System name"...] |        |        |
+-------------+--------------------+--------+--------+

References before step 5:
+--------+-------+-------+-------+
|  cl1   |  cl2  | temp  | temp1 |
+--------+-------+-------+-------+
| cell 1 | cell2 | cell1 | cell2 |
+--------+-------+-------+-------+

Memory after step 5:
+--------------------+--------------------+--------+--------+
|       cell 1       |       cell 2       | cell 3 | cell 4 |
+--------------------+--------------------+--------+--------+
| ["System name"...] | ["System name"...] |        |        |
+--------------------+--------------------+--------+--------+
References after step 5:
+--------+-------+-------+-------+
|  cl1   |  cl2  | temp  | temp1 |
+--------+-------+-------+-------+
| cell 1 | cell2 | cell1 | cell2 |
+--------+-------+-------+-------+


1. Cell cl1 = row.getCell(0);
2. Cell cl2 = row.getCell(1);
3. Cell temp = row.getCell(0);
4. Cell temp1 = row.getCell(1);
5. cl1.setCellValue(temp1.getStringCellValue());
6. cl2.setCellValue(temp.getStringCellValue());

所以......步骤6导致将cl2 cellValue设置为temp(该数据位于cell1中)值,该值是“系统名称”。

以下内容可行,但不需要。

1. Cell cl1 = row.getCell(0);
2. Cell cl2 = row.getCell(1);
3. String temp1 = new String(cl1.getStringCellValue());
4. String temp2 = new String(cl2.getStringCellValue());
5. cl1.setCellValue(temp2);
6. cl2.setCellValue(temp1);

您要做的是交换单元格的内容。没有必要自己交换单元格。看看你能做些什么:

1. Cell cl0 = row.getCell(0);
2. Cell cl1 = row.getCell(1);
3. String temp = new String(cl1.getStringValue());
4. cl1.setStringValue(cl0.getStringValue());
5. cl0.setStringValue(temp);


1. Cell cl0 = row.getCell(0);
2. Cell cl1 = row.getCell(1);
3. String temp = new String(cl1.getStringValue());
4. java.util.Date temp2 = cl0.getDateValue();
5. cl1.setCellStyle(cellStyle1);
6. cl1.setCellValue(temp2);
7. cl0.setCellType(Cell.CELL_TYPE_STRING);
8. cl0.setCellValue(temp);

修改

由于OP很难,我在这里粘贴整个代码。

try {
    if (file11.exists()) {
        String dt = sh1.getRow(0).getCell(1).getStringCellValue();
        if (!dt.equalsIgnoreCase("Date")) {
            Iterator<Row> rowIterator1 = sh1.iterator();
            while (rowIterator1.hasNext()) {
                Row row = rowIterator1.next();
                if (row.getCell(1).getStringCellValue().equalsIgnoreCase("Date")) {
                    Cell cl0 = row.getCell(0);
                    Cell cl1 = row.getCell(1);
                    String temp = new String(cl1.getStringValue());
                    cl1.setStringValue(cl0.getStringValue());
                    cl0.setStringValue(temp);
                } else {
                    Cell cl0 = row.getCell(0);
                    Cell cl1 = row.getCell(1);
                    String temp = new String(cl1.getStringValue());
                    java.util.Date temp2 = cl0.getDateValue();
                    cl1.setCellStyle(cellStyle1);
                    cl1.setCellValue(temp2);
                    cl0.setCellType(Cell.CELL_TYPE_STRING);
                    cl0.setCellValue(temp);
                }
            }
        }
    }
}

答案 1 :(得分:0)

我得到了这个问题的答案。

尝试{

 if (file11.exists()) {
 String dt = sh1.getRow(0).getCell(1).getStringCellValue();
 if (!dt.equalsIgnoreCase("Date")) {
 Iterator<Row> rowIterator1 = sh1.iterator();
 while (rowIterator1.hasNext()) {
 Row row = rowIterator1.next();                    
 DataFormatter df = new DataFormatter();//instantiate DataFormatter class for reading the cell without changing the cell type
 Cell cl0 = row.getCell(0);
 Cell cl1 = row.getCell(1);
 CellStyle cs1 = cl0.getCellStyle();
 CellStyle cs2 = cl1.getCellStyle();
 String s1 = new String(df.formatCellValue(cl0));//store cell value as string
 String s2 = new String(df.formatCellValue(cl1));//store cell value as string
 cl1.setCellValue(s1);//perform swapping
 cl0.setCellStyle(cs2);
 cl1.setCellStyle(cs1);//perform swapping on formatting
 cl0.setCellValue(s2);//perform swapping  
            }
        }
    } else {
        System.out.println("File does not exist..................");
    }
 }
catch (Exception e) {
        } finally {
           FileOutputStream out = new FileOutputStream(Report_File2);
    workbook11.write(out);
    out.close();
}