Java 兴趣点编辑 excel 文件 - cell.setCellValue 不工作
Java POI edit excel file - cell.setCellValue not working
我正在尝试修改 excel 文件,但出于某种我不理解的原因,Cell.setCellValue 方法在我的代码中不起作用。
我实际上在做的是:
-我正在打开一个 excel 文件并将我感兴趣的内容保存在一个 HashMap 中。这有效,我可以打印哈希图的内容。
- 然后我试图用保存在 HashMap 中的数据修改另一个 excel 文件,但由于某种原因这并没有发生。
这是我的代码:
public File manipulateDocumentITM(File xlFile) {
// ADDING DATA FROM AN EXCEL FILE TO A HASHMAP
HashMap<Integer, ArrayList<Date>> hashMap = new HashMap<>();
try {
FileInputStream inFile = new FileInputStream(xlFile);
Workbook workbookInFile = new XSSFWorkbook(inFile);
Sheet sheetInFile = workbookInFile.getSheetAt(0);
Iterator<Row> rowIteratorInFile = sheetInFile.iterator();
int rowCountInFile = 5, key = 0, countEmpty = 0, rowCountModelFile = 10;
while (rowIteratorInFile.hasNext()) {
ArrayList<Date> arrayList = new ArrayList<>();
Row rowInFile = rowIteratorInFile.next();
if (rowInFile.getRowNum() == rowCountInFile) {
key++;
Iterator<Cell> cellIteratorInFile = rowInFile.cellIterator();
arrayList = new ArrayList<>();
while (cellIteratorInFile.hasNext()) {
Cell cell = cellIteratorInFile.next();
if ((cell.getCellType() == CellType.NUMERIC) && (cell.getColumnIndex() != 0)) {
Date data = cell.getDateCellValue();
arrayList.add(data);
}
}
hashMap.put(key, arrayList);
rowCountInFile = rowCountInFile + 4;
}
}
inFile.close();
// DATA SAVED IN HASHMAP ABOVE NEXT IM JUST PRINTING THE VALUES IN THE HASHMAP
for (Integer I : hashMap.keySet()) {
ArrayList<Date> replaceArray = hashMap.get(I);
System.out.println("***");
for (int i = 0; i < replaceArray.size(); i++) {
System.out.println(replaceArray.get(i).getHours());
}
}
// CODE THAT SUPPOSE TO MODIFY EXCEL FILE WITH THE DATA FROM THE HASHMAP
String modelPath = "/home/h1dr0/Documents/unimineral/Model foaie de prezenta (another copy).xlsx";
FileInputStream modelFile = new FileInputStream(modelPath);
Workbook workbookModel = new XSSFWorkbook(modelFile);
Sheet sheetModelFile = workbookModel.getSheetAt(0);
Iterator<Row> rowIteratorModelFile = sheetModelFile.iterator();
ArrayList<Date> replaceArray2 = new ArrayList<>();
Iterator it = hashMap.entrySet().iterator();
while (rowIteratorModelFile.hasNext()) {
Row rowModelFile = rowIteratorModelFile.next();
if (rowModelFile.getRowNum() == rowCountModelFile) {
Iterator<Cell> cellIteratorModelFile = rowModelFile.cellIterator();
Map.Entry pair = (Map.Entry)it.next();
replaceArray2 = (ArrayList<Date>) pair.getValue();
while (cellIteratorModelFile.hasNext()) {
Cell cell = cellIteratorModelFile.next();
if (replaceArray2.size() != 0) {
for (int i = 0; i < replaceArray2.size(); i++) {
if ((replaceArray2.get(i).getHours() != 0) && replaceArray2.get(i).toString() != "" && (cell.getColumnIndex() != 18)) {
// THIS DOES NOT WORK
cell.setCellValue(replaceArray2.get(i).getHours());
}
else {
cell.setCellValue(" ");
}
}
} else {
cell.setCellValue(" ");
}
}
rowCountModelFile = rowCountModelFile + 3;
}
}
modelFile.close();
//}
FileOutputStream outputStream = new FileOutputStream("/home/h1dr0/Documents/unimineral/generate.xlsx",false);
workbookModel.write(outputStream);
outputStream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
return xlFile;
}
我还使用调试器进行了检查,单元格值已修改为预期的值...
cell.setCellValue(8);
if(cell.getCellType() == CellType.NUMERIC) {
System.out.println("cell: " + cell.getNumericCellValue());
}
打印 8
我得到的是同一个文件..没有修改。
请帮忙,谢谢!
Excel 旨在处理大表。只有使用过的才会存储在内存或文档中。这意味着在填充单元格之前,必须先创建它。
在你的代码中,我只看到你迭代了现有的单元格,但没有尝试创建它们。也许这就是问题所在?
我决定尝试另一种方法来修改 excel 文件。我正在使用 UIPath 进行自动化。它工作得很好我设法通过在 Studio 中使用他们的 excel activity 依赖项(他们的 IDE 让我们说)。
我正在尝试修改 excel 文件,但出于某种我不理解的原因,Cell.setCellValue 方法在我的代码中不起作用。 我实际上在做的是: -我正在打开一个 excel 文件并将我感兴趣的内容保存在一个 HashMap 中。这有效,我可以打印哈希图的内容。 - 然后我试图用保存在 HashMap 中的数据修改另一个 excel 文件,但由于某种原因这并没有发生。
这是我的代码:
public File manipulateDocumentITM(File xlFile) {
// ADDING DATA FROM AN EXCEL FILE TO A HASHMAP
HashMap<Integer, ArrayList<Date>> hashMap = new HashMap<>();
try {
FileInputStream inFile = new FileInputStream(xlFile);
Workbook workbookInFile = new XSSFWorkbook(inFile);
Sheet sheetInFile = workbookInFile.getSheetAt(0);
Iterator<Row> rowIteratorInFile = sheetInFile.iterator();
int rowCountInFile = 5, key = 0, countEmpty = 0, rowCountModelFile = 10;
while (rowIteratorInFile.hasNext()) {
ArrayList<Date> arrayList = new ArrayList<>();
Row rowInFile = rowIteratorInFile.next();
if (rowInFile.getRowNum() == rowCountInFile) {
key++;
Iterator<Cell> cellIteratorInFile = rowInFile.cellIterator();
arrayList = new ArrayList<>();
while (cellIteratorInFile.hasNext()) {
Cell cell = cellIteratorInFile.next();
if ((cell.getCellType() == CellType.NUMERIC) && (cell.getColumnIndex() != 0)) {
Date data = cell.getDateCellValue();
arrayList.add(data);
}
}
hashMap.put(key, arrayList);
rowCountInFile = rowCountInFile + 4;
}
}
inFile.close();
// DATA SAVED IN HASHMAP ABOVE NEXT IM JUST PRINTING THE VALUES IN THE HASHMAP
for (Integer I : hashMap.keySet()) {
ArrayList<Date> replaceArray = hashMap.get(I);
System.out.println("***");
for (int i = 0; i < replaceArray.size(); i++) {
System.out.println(replaceArray.get(i).getHours());
}
}
// CODE THAT SUPPOSE TO MODIFY EXCEL FILE WITH THE DATA FROM THE HASHMAP
String modelPath = "/home/h1dr0/Documents/unimineral/Model foaie de prezenta (another copy).xlsx";
FileInputStream modelFile = new FileInputStream(modelPath);
Workbook workbookModel = new XSSFWorkbook(modelFile);
Sheet sheetModelFile = workbookModel.getSheetAt(0);
Iterator<Row> rowIteratorModelFile = sheetModelFile.iterator();
ArrayList<Date> replaceArray2 = new ArrayList<>();
Iterator it = hashMap.entrySet().iterator();
while (rowIteratorModelFile.hasNext()) {
Row rowModelFile = rowIteratorModelFile.next();
if (rowModelFile.getRowNum() == rowCountModelFile) {
Iterator<Cell> cellIteratorModelFile = rowModelFile.cellIterator();
Map.Entry pair = (Map.Entry)it.next();
replaceArray2 = (ArrayList<Date>) pair.getValue();
while (cellIteratorModelFile.hasNext()) {
Cell cell = cellIteratorModelFile.next();
if (replaceArray2.size() != 0) {
for (int i = 0; i < replaceArray2.size(); i++) {
if ((replaceArray2.get(i).getHours() != 0) && replaceArray2.get(i).toString() != "" && (cell.getColumnIndex() != 18)) {
// THIS DOES NOT WORK
cell.setCellValue(replaceArray2.get(i).getHours());
}
else {
cell.setCellValue(" ");
}
}
} else {
cell.setCellValue(" ");
}
}
rowCountModelFile = rowCountModelFile + 3;
}
}
modelFile.close();
//}
FileOutputStream outputStream = new FileOutputStream("/home/h1dr0/Documents/unimineral/generate.xlsx",false);
workbookModel.write(outputStream);
outputStream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
return xlFile;
}
我还使用调试器进行了检查,单元格值已修改为预期的值...
cell.setCellValue(8);
if(cell.getCellType() == CellType.NUMERIC) {
System.out.println("cell: " + cell.getNumericCellValue());
}
打印 8
我得到的是同一个文件..没有修改。
请帮忙,谢谢!
Excel 旨在处理大表。只有使用过的才会存储在内存或文档中。这意味着在填充单元格之前,必须先创建它。
在你的代码中,我只看到你迭代了现有的单元格,但没有尝试创建它们。也许这就是问题所在?
我决定尝试另一种方法来修改 excel 文件。我正在使用 UIPath 进行自动化。它工作得很好我设法通过在 Studio 中使用他们的 excel activity 依赖项(他们的 IDE 让我们说)。