使用java在excel表中的列之间交换

我是新的Java。 其实我想交换使用Java的Excel表中的两列。 我使用的代码,但我没有得到正确的输出。 我添加了我的Excel表的截图。 我想交换系统名称和date列。

在这里输入图像说明

我还添加了我的不正确输出的屏幕截图。 我于1900年1月24日和29日,而不是2016年9月30日。

在这里输入图像说明

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

你的问题基本上是关于java引用或关于swapalgorithm。

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

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

尝试{

  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(); }