Code: Select all
package com.company;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.*;
import static com.company.Extenso5.extenso;
public class Main {
public static void main(String[] args) {
try{
// Abrir canal de "leitura/input" e carregar ficheiro
FileInputStream file = new FileInputStream(new File( "C:\\Temp\\Java\\translate.xlsx"));
// Declaraçao e instanciação de objectos POI, para oe elementos da folha de cálculo
XSSFWorkbook workbook = new XSSFWorkbook(file);
Sheet sheet = workbook.getSheetAt(0);
Cell cell;
Row row;
// Quantas linhas tem a folha
int rows = sheet.getPhysicalNumberOfRows();
// Loop pelas linhas com informação
String getterS, extVal;
for(int r = 1; r < rows; r++) {
row = sheet.getRow(r);
if(row != null) {
cell = row.getCell((short)0);
if(cell != null) {
// READ da célula
getterS = String.valueOf(cell.getNumericCellValue());
getterS = getterS.substring(0, getterS.indexOf("."));
// Obter o valor por extenso
extVal = extenso(getterS);
// WRITE para a célula (col + 1)
cell = row.createCell(1);
cell.setCellValue(extVal);
}
}
}
// Abrir um "canal de output e Gravação", paralelo ao de Leitura
FileOutputStream output = new FileOutputStream(new File( "C:\\Temp\\Java\\translate.xlsx"));
// Gravar
workbook.write(output);
// Fechar operações
output.close();
workbook.close();
file.close();
System.out.println("Terminado.");
}
catch (FileNotFoundException naoHaFicheiro){
System.out.println("No file: " + naoHaFicheiro);
}
catch (IOException erroIO){
System.out.println("IO Error: " + erroIO);
}
}
}