Reputation: 23
I'm trying to modify an excel file but for some reason which I do not understand the method Cell.setCellValue does not work in my code. What I'm actually doing is: -I'm opening an excel file and saving the content that interests me in a HashMap. This works i can print the content of the hashmap. -Then I'm trying to modify another excel file with the data saved in the HashMap but this does not happen for some reason.
Here is my code:
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;
}
I also checked with debugger and the cell values are modified to what it suppose to...
cell.setCellValue(8);
if(cell.getCellType() == CellType.NUMERIC) {
System.out.println("cell: " + cell.getNumericCellValue());
}
prints 8
What i get is the same file .. no modification.
Please help , thank you !
Upvotes: 2
Views: 1626
Reputation: 23
I decided to try another approach in modifying excel files. I'm using UIPath for automation. It works good I managed to do this by using their excel activity dependencies in Studio ( their IDE let's say ).
Upvotes: 0
Reputation: 9473
Excel is designed to work on huge tables. Only the used ones are stored in memory or the document. That means before you can populate a cell, it first has to be created.
In your code I only see that you iterate over the existing cells but you do not try to create them. Maybe that is the issue?
Upvotes: 1