2016-11-02 24 views
0

我是java中的新成員。其實我想用java交換Excel表中的兩列。我使用了代碼,但沒有得到正確的輸出。我添加了我的Excel表單的截圖。我想交換系統名稱和日期列。使用java在excel表中的列之間交換

enter image description here

我也加我的不正確的輸出的屏幕截圖。我得到了24和1900年1月29日,而不是1-30th九月2016

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的回答我已經試過如下:

  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); 
      } 
} 
+0

哦,來吧!我已經格式化了你的代碼,所以很好!你爲什麼要摧毀它?那些空行是什麼?! – xenteros

+0

我已經向你的代碼添加了很長的解釋。你能明白嗎?它有助於你還是需要進一步的解釋? – xenteros

+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(); 
} 
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的(這數據是在小區1)值,它是在目前‘系統名稱’。

以下將工作,但沒有必要。

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); 
       } 
      } 
     } 
    } 
} 
+0

此代碼僅交換系統名稱和日期行不列。這不會交換所有的值。可能是因爲字符串,因爲temp1和temp2是字符串。 – abcd

+1

是的。這隻適用於if語句。好的,我會給你整個代碼。 – xenteros

+1

@abcd我剛剛爲else語句添加了工作代碼。如果有幫助,請將其標記爲解決方案並給予滿意的答覆。 – xenteros