我是java中的新成員。其實我想用java交換Excel表中的兩列。我使用了代碼,但沒有得到正確的輸出。我添加了我的Excel表單的截圖。我想交換系統名稱和日期列。使用java在excel表中的列之間交換
我也加我的不正確的輸出的屏幕截圖。我得到了24和1900年1月29日,而不是1-30th九月2016
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);
}
}
哦,來吧!我已經格式化了你的代碼,所以很好!你爲什麼要摧毀它?那些空行是什麼?! – xenteros
我已經向你的代碼添加了很長的解釋。你能明白嗎?它有助於你還是需要進一步的解釋? – xenteros
請正確縮進您的代碼。就目前而言,結構很難猜測。 –