gpt4 book ai didi

java - 使用java在Excel工作表中的列之间交换

转载 作者:行者123 更新时间:2023-12-02 03:15:30 25 4
gpt4 key购买 nike

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

enter image description here

我还添加了错误输出的屏幕截图。我得到的是 1900 年 1 月 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的回答我尝试了以下方法:

         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引用或关于交换算法。

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

关于java - 使用java在Excel工作表中的列之间交换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40373352/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com