Olá tenho programa onde ele le o arquivo e grava normal mas se numa coluna, linha tiver valor juros maior 0> ele vai pegar essa linha vai triplicar essa linha mudando 2 valores em cada linha e depois que fizer isso ele volta a rotina normal ler e grava.Meu codico segue pra vcs da uma olhada…Se alguem tiver uma solução eu agradeço muito pois eu ja pesquisei na net mas não encontrei nada.
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.ParseException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class NomeClasse {
public static void main(String[] args) throws ParseException, FileNotFoundException, IOException {
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("C:/exemplo/Arquivosaida.txt")), "UTF-8"));
HSSFWorkbook wb = null;
try {
wb = new HSSFWorkbook(new FileInputStream("C:/exemplo/arquivodeentrada.xls"));
} catch (IOException ex) {
ex.printStackTrace();
}
HSSFSheet sheet = wb.getSheet("PPInfo");
for (int linha_inicial = 3; linha_inicial <= sheet.getPhysicalNumberOfRows(); linha_inicial++) {
HSSFRow row = sheet.getRow(linha_inicial);
if (row == null) {
break;
}
HSSFCell fornecedor = row.getCell(2);
if (fornecedor == null) {
break;
}
//Aqui le!
HSSFCell discricao = row.getCell(3);
HSSFCell valor_debito = row.getCell(4);
HSSFCell data = row.getCell(6);
HSSFCell valor_lcto = row.getCell(7);
HSSFCell valor_juros = row.getCell(8);
HSSFCell valor_deconto = row.getCell(9);
HSSFCell valor_contabil = row.getCell(10);
//Aqui transforma em string
Date dData = data.getDateCellValue();
SimpleDateFormat formato = new SimpleDateFormat("dd/MM/yyyy");
String sData = formato.format(dData);
String valorDebito = String.format("%.2f", valor_debito.getNumericCellValue());
String valorLcto = String.format("%.2f", valor_lcto.getNumericCellValue());
String valorJuros = getCellStringValue(valor_juros);
//Aqui e valor juros não for zero em sua linha ele tem fazer o que eu expliquei lá em cima...
if(valorJuros != null) {
valorJuros = valorJuros.replace(".0","").replace(".","");
} else{
valorJuros = "0";
}
String valorDeconto = String.format("%.2f", valor_deconto.getNumericCellValue());
String valorContabil = String.format("%.2f", valor_contabil.getNumericCellValue());
String valorCredito = "9";
String sFornecedor = getCellStringValue(fornecedor);
String sDiscricao = getCellStringValue(discricao);
String sBrenk = getCellStringValue(fornecedor);
int juros = Integer.parseInt(valorJuros);
if(juros > 0) {
System.out.println("Valor: "+juros);
}
String space = String.format("%-10s", " ");
//Aqui grava!
StringBuilder linha = new StringBuilder();
linha.append("C;"); //1
linha.append("00472;"); //5
linha.append(sData); //10
linha.append(";");
linha.append(space); //10
linha.append(";");
linha.append(String.format("%11s", valorDebito.replace(",00", "")).replace(" ", "0").replace("00000000000", " ")); //11
linha.append(";");
linha.append(String.format("%11s", valorCredito.replace(",00", "")).replace(" ", "0").replace("00000000000", " ")); //11
linha.append(";");
linha.append(String.format("%16s", valorContabil.replace(",", "")).replace(" ", "0")); //16
linha.append(";");
linha.append("00000");
linha.append(";");
linha.append(String.format("%-300s", "Fornecedor:" + sFornecedor + " Descrição:" + sDiscricao).replace("null", " ")); //300
linha.append("\r\n");
writer.write(linha.toString());
//System.out.println(linha.toString());
}
writer.close();
}
public static String getCellStringValue(HSSFCell cell) {
String value = null;
try {
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_FORMULA:
value = cell.getCellFormula();
break;
case HSSFCell.CELL_TYPE_NUMERIC:
value = String.valueOf(cell.getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_STRING:
value = cell.getStringCellValue();
break;
default:
}
} catch (Exception e) {
//e.printStackTrace();
value = "";
}
return value;
}
}
O codico está comentado
obrigado