Galera, olha só, tenho essa classe abaixo que importa um xml, converte e exporta para Excel apenas alguns campos, porém não estou conseguindo exportar os valores de Qtde e Moeda nos formatos padrões como inteiro e moeda R$:
Minha Classe de conversão XmlToExcelConverter:
//Classe que importa o xml e exporta para Excel
import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
* Created by Fabio Eduardo Argenton fabio.argenton@hotmail.com
*/
public class XmlToExcelConverter {
private static Workbook workbook;
private static int rowNum;
//Cabeçalho da NF
private final static int infNFe_NF_COLUMN = 0;
private final static int infNFe_DATA_COLUMN = 1;
private final static int infNFe_FORNECEDOR_COLUMN = 2;
//Detalhes da NF
private final static int infNFe_MATERIAL_COLUMN = 3;
private final static int infNFe_DESCRICAO_COLUMN = 4;
private final static int infNFe_UM_COLUMN = 5;
private final static int infNFe_QUANTIDADE_COLUMN = 6;//Aqui é o número (Qtde)
private final static int infNFe_CUSTO_UNITARIO_COLUMN = 7;//Aqui é o valor (R$)
private final static int infNFe_CUSTO_TOTAL_COLUMN = 8;//Aqui é o valor (R$)
public static void main(String[] args) throws Exception {
getAndReadXml();
}
/**
*
* Faz o download de um arquivo XML, lê os valores da substância e do
* produto e, em seguida, escreve-os para linhas em um arquivo excel.
*
* @throws Exception
*/
private static void getAndReadXml() throws Exception {
System.out.println("getAndReadXml");
File xmlFile = new File("C:/Temp/teste.xml");
//Chama o método para gravar o cabeçalho da Planilha
geraCabeçalhoNF();
//Após gravar o cabeçalho começa a gravas os detalhes da NF
Sheet sheet = workbook.getSheetAt(0);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xmlFile);
//Passa pelo nó "infNFe" capturando o cabeçalho da NF
NodeList nList = doc.getElementsByTagName("infNFe");
for (int i = 0; i < nList.getLength(); i++) {
System.out.println("Processing element " + (i + 1) + "/" + nList.getLength());
Node node = nList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
//Captura o texto de cada tag do xml especificado abaixo (Cabeçalho da NF)
Element element = (Element) node;
String infNFeNF = element.getElementsByTagName("nNF").item(0).getTextContent();
String infNFeFornecedor = element.getElementsByTagName("xNome").item(0).getTextContent();
//Passa pelo nó "prod" capturando cada registro (detalhes da NF)
NodeList pns = element.getElementsByTagName("prod");
for (int j = 0; j < pns.getLength(); j++) {
Node prod = pns.item(j);
if (prod.getNodeType() == Node.ELEMENT_NODE) {
//Captura o texto de cada tag do xml especificado abaixo
Element product = (Element) prod;
String pnMaterial = product.getElementsByTagName("cProd").item(0).getTextContent();
String pnDescricao = product.getElementsByTagName("xProd").item(0).getTextContent();
String pnUM = product.getElementsByTagName("uCom").item(0).getTextContent();
String pnQuantidade = product.getElementsByTagName("qCom").item(0).getTextContent();//Aqui é o número (Qtde)
String pnCustoUnitario = product.getElementsByTagName("vUnCom").item(0).getTextContent();//Aqui é o valor(R$)
String pnCustoTotal = product.getElementsByTagName("vProd").item(0).getTextContent();//Aqui é o valor(R$)
CreationHelper createHelper = workbook.getCreationHelper();
DataFormat format = workbook.createDataFormat();
CellStyle styleText1 = workbook.createCellStyle();
styleText1.setAlignment(HorizontalAlignment.CENTER);
CellStyle styleText2 = workbook.createCellStyle();
styleText2.setAlignment(HorizontalAlignment.LEFT);
CellStyle styleNumber = workbook.createCellStyle();
styleNumber.setAlignment(HorizontalAlignment.CENTER);
styleNumber.setDataFormat(format.getFormat("#.#"));//Tentei fazer isso mas não deu certo
CellStyle styleMoeda = workbook.createCellStyle();
styleMoeda.setAlignment(HorizontalAlignment.CENTER);
styleMoeda.setDataFormat(format.getFormat("[$R$-pt-BR] #.##0,00"));//Tentei fazer isso mas não deu certo
CellStyle dateCellStyle = workbook.createCellStyle();
dateCellStyle.setAlignment(HorizontalAlignment.CENTER);
dateCellStyle.setDataFormat(createHelper.createDataFormat().getFormat("dd/MM/yyyy"));
Row row = sheet.createRow(rowNum++);
//Cabeçalho da NF
Cell cell = row.createCell(infNFe_NF_COLUMN);
cell.setCellValue(infNFeNF);
cell.setCellStyle(styleText1);
cell = row.createCell(infNFe_DATA_COLUMN);
cell.setCellValue(new Date());
cell.setCellStyle(dateCellStyle);
cell = row.createCell(infNFe_FORNECEDOR_COLUMN);
cell.setCellValue(infNFeFornecedor);
cell.setCellStyle(styleText2);
//Detalhes da NF
cell = row.createCell(infNFe_MATERIAL_COLUMN);
cell.setCellValue(pnMaterial);
cell.setCellStyle(styleText1);
cell = row.createCell(infNFe_DESCRICAO_COLUMN);
cell.setCellValue(pnDescricao);
cell.setCellStyle(styleText2);
cell = row.createCell(infNFe_UM_COLUMN);
cell.setCellValue(pnUM);
cell.setCellStyle(styleText1);
cell = row.createCell(infNFe_QUANTIDADE_COLUMN);//Aqui tem o número (Qtde)
cell.setCellValue(pnQuantidade);
cell.setCellStyle(styleNumber);//Tentei fazer isso mas não deu certo
cell.setCellType(CellType.NUMERIC);//Tentei fazer isso mas não deu certo
cell = row.createCell(infNFe_CUSTO_UNITARIO_COLUMN);//Aqui tem o valor (R$)
cell.setCellValue(pnCustoUnitario);
cell.setCellStyle(styleMoeda);//Tentei fazer isso mas não deu certo
cell = row.createCell(infNFe_CUSTO_TOTAL_COLUMN);//Aqui tem o valor (R$)
cell.setCellValue(pnCustoTotal);
cell.setCellStyle(styleMoeda);//Tentei fazer isso mas não deu certo
}
}
}
}
//Gera o arquivo excel na pasta selecionada
FileOutputStream fileOut = new FileOutputStream("C:/Temp/Excel-Out-MWM.xlsx");
workbook.write(fileOut);
workbook.close();
fileOut.close();
// //Deleta o xml após conversão para xlsx
// if (xmlFile.exists()) {
// System.out.println("delete file-> " + xmlFile.getAbsolutePath());
// if (!xmlFile.delete()) {
// System.out.println("file '" + xmlFile.getAbsolutePath() + "' was not deleted!");
// }
// }
System.out.println("getAndReadXml finished, processed " + nList.getLength() + " infNFe!");
}
/**
* Metodo para gravar as linhas referentes aos Cabeçalho da planilha
*/
private static void geraCabeçalhoNF() {
workbook = new XSSFWorkbook();
rowNum = 0;
Sheet sheet = workbook.createSheet();
Font boldFont = workbook.createFont();
boldFont.setBold(true);
CellStyle styleText = workbook.createCellStyle();
styleText.setAlignment(HorizontalAlignment.CENTER);
styleText.setFont(boldFont);
CellStyle styleText2 = workbook.createCellStyle();
styleText2.setAlignment(HorizontalAlignment.LEFT);
styleText2.setFont(boldFont);
Row row = sheet.createRow(rowNum++);
Cell cell = row.createCell(infNFe_NF_COLUMN);
cell.setCellValue("NF");
cell.setCellStyle(styleText);
cell = row.createCell(infNFe_DATA_COLUMN);
cell.setCellValue("Data");
cell.setCellStyle(styleText);
cell = row.createCell(infNFe_FORNECEDOR_COLUMN);
cell.setCellValue("Fornecedor");
cell.setCellStyle(styleText2);
cell = row.createCell(infNFe_MATERIAL_COLUMN);
cell.setCellValue("Material");
cell.setCellStyle(styleText);
cell = row.createCell(infNFe_DESCRICAO_COLUMN);
cell.setCellValue("Descricao");
cell.setCellStyle(styleText2);
cell = row.createCell(infNFe_UM_COLUMN);
cell.setCellValue("UM");
cell.setCellStyle(styleText);
cell = row.createCell(infNFe_QUANTIDADE_COLUMN);
cell.setCellValue("Quantidade");
cell.setCellStyle(styleText);
cell = row.createCell(infNFe_CUSTO_UNITARIO_COLUMN);
cell.setCellValue("R$ Unit.");
cell.setCellStyle(styleText);
cell = row.createCell(infNFe_CUSTO_TOTAL_COLUMN);
cell.setCellValue("R$ Total");
cell.setCellStyle(styleText);
}
}
Os campos de Qtde e Valor estão na tag <prod>
Qtde que não estou conseguindo exportar no Excel em formato padrão de número: <qCom>18.0000</qCom> o correto é 18
Valorque não estou conseguindo exportar no Excel em formato padrão de moeda em reais: <vUnCom>96.8500000000</vUnCom>o correto é R$ 96,85
Aqui está meu xml:
ml version="1.0" encoding="UTF-8"?><nfeProc versao="3.10" xmlns="http://www.portalfiscal.inf.br/nfe">
<NFe xmlns="http://www.portalfiscal.inf.br/nfe">
<infNFe versao="3.10" Id="NFe35180502735564000106550010000651451879514583">
<ide>
<cUF>35</cUF>
<cNF>87951458</cNF>
<natOp>S102-Vendas</natOp>
<indPag>1</indPag>
<mod>55</mod>
<serie>1</serie>
<nNF>65145</nNF>
<dhEmi>2018-05-22T09:35:00-03:00</dhEmi>
<tpNF>1</tpNF>
<idDest>1</idDest>
<cMunFG>3518800</cMunFG>
<tpImp>1</tpImp>
<tpEmis>1</tpEmis>
<cDV>3</cDV>
<tpAmb>1</tpAmb>
<finNFe>1</finNFe>
<indFinal>0</indFinal>
<indPres>9</indPres>
<procEmi>0</procEmi>
<verProc>121132075</verProc>
</ide>
<emit>
<CNPJ>02735564000106</CNPJ>
<xNome>SCBR AUTOMACAO DO BRASIL LTDA</xNome>
<xFant>Scan Brasil</xFant>
<enderEmit>
<xLgr>Rua Jose Sarraceni</xLgr>
<nro>141</nro>
<xBairro>Vila Augusta</xBairro>
<cMun>3518800</cMun>
<xMun>Guarulhos</xMun>
<UF>SP</UF>
<CEP>07025250</CEP>
<cPais>1058</cPais>
<xPais>Brasil</xPais>
<fone>1124237500</fone>
</enderEmit>
<IE>796286964119</IE>
<IM>27241394</IM>
<CNAE>4751200</CNAE>
<CRT>3</CRT>
</emit>
<dest>
<CNPJ>02162259000326</CNPJ>
<xNome>INTERNATIONAL IND.AUTOMOTIVA DA AMERICA DO SUL LTDA</xNome>
<enderDest>
<xLgr>AVENIDA ODILA CHAVES RODRIGUES</xLgr>
<nro>1227</nro>
<xBairro>PARQUE INDUSTRIAL</xBairro>
<cMun>3525904</cMun>
<xMun>Jundiai</xMun>
<UF>SP</UF>
<CEP>13213087</CEP>
<cPais>1058</cPais>
<xPais>Brasil</xPais>
<fone>1138823815</fone>
</enderDest>
<indIEDest>1</indIEDest>
<IE>407531352115</IE>
<email>jose.witt@navistar.com.br</email>
</dest>
<det nItem="1">
<prod>
<cProd>MOTOR.</cProd>
<cEAN/>
<xProd>MOTOR.</xProd>
<NCM>85013110</NCM>
<CFOP>5102</CFOP>
<uCom>PC</uCom>
<qCom>18.0000</qCom>
<vUnCom>96.8500000000</vUnCom>
<vProd>96.85</vProd>
<cEANTrib/>
<uTrib>PC</uTrib>
<qTrib>1.0000</qTrib>
<vUnTrib>96.8500000000</vUnTrib>
<indTot>1</indTot>
</prod>
<imposto>
<vTotTrib>23.18</vTotTrib>
<ICMS>
<ICMS00>
<orig>0</orig>
<CST>00</CST>
<modBC>3</modBC>
<vBC>96.85</vBC>
<pICMS>18.0000</pICMS>
<vICMS>17.43</vICMS>
</ICMS00>
</ICMS>
<IPI>
<cEnq>999</cEnq>
<IPITrib>
<CST>99</CST>
<vBC>0.00</vBC>
<pIPI>0.0000</pIPI>
<vIPI>0.00</vIPI>
</IPITrib>
</IPI>
<PIS>
<PISAliq>
<CST>01</CST>
<vBC>96.85</vBC>
<pPIS>0.6500</pPIS>
<vPIS>0.63</vPIS>
</PISAliq>
</PIS>
<COFINS>
<COFINSAliq>
<CST>01</CST>
<vBC>96.85</vBC>
<pCOFINS>3.0000</pCOFINS>
<vCOFINS>2.91</vCOFINS>
</COFINSAliq>
</COFINS>
</imposto>
</det>
<det nItem="2">
<prod>
<cProd>DPR 001</cProd>
<cEAN/>
<xProd>DISCO PLASTICO</xProd>
<NCM>39203000</NCM>
<CFOP>5102</CFOP>
<uCom>UN</uCom>
<qCom>21.0000</qCom>
<vUnCom>75.0000000000</vUnCom>
<vProd>150.00</vProd>
<cEANTrib/>
<uTrib>UN</uTrib>
<qTrib>2.0000</qTrib>
<vUnTrib>75.0000000000</vUnTrib>
<indTot>1</indTot>
</prod>
<imposto>
<vTotTrib>35.90</vTotTrib>
<ICMS>
<ICMS00>
<orig>1</orig>
<CST>00</CST>
<modBC>3</modBC>
<vBC>150.00</vBC>
<pICMS>18.0000</pICMS>
<vICMS>27.00</vICMS>
</ICMS00>
</ICMS>
<IPI>
<cEnq>999</cEnq>
<IPITrib>
<CST>99</CST>
<vBC>0.00</vBC>
<pIPI>0.0000</pIPI>
<vIPI>0.00</vIPI>
</IPITrib>
</IPI>
<PIS>
<PISAliq>
<CST>01</CST>
<vBC>150.00</vBC>
<pPIS>0.6500</pPIS>
<vPIS>0.98</vPIS>
</PISAliq>
</PIS>
<COFINS>
<COFINSAliq>
<CST>01</CST>
<vBC>150.00</vBC>
<pCOFINS>3.0000</pCOFINS>
<vCOFINS>4.50</vCOFINS>
</COFINSAliq>
</COFINS>
</imposto>
</det>
<total>
<ICMSTot>
<vBC>246.85</vBC>
<vICMS>44.43</vICMS>
<vICMSDeson>0.00</vICMSDeson>
<vBCST>0.00</vBCST>
<vST>0.00</vST>
<vProd>246.85</vProd>
<vFrete>0.00</vFrete>
<vSeg>0.00</vSeg>
<vDesc>0.00</vDesc>
<vII>0.00</vII>
<vIPI>0.00</vIPI>
<vPIS>1.61</vPIS>
<vCOFINS>7.41</vCOFINS>
<vOutro>0.00</vOutro>
<vNF>246.85</vNF>
<vTotTrib>59.08</vTotTrib>
</ICMSTot>
</total>
<transp>
<modFrete>0</modFrete>
</transp>
<cobr>
<fat>
<nFat>65145</nFat>
<vOrig>246.85</vOrig>
<vLiq>246.85</vLiq>
</fat>
<dup>
<nDup>1</nDup>
<dVenc>2018-06-20</dVenc>
<vDup>246.85</vDup>
</dup>
</cobr>
<infAdic>
<infCpl>PI: 131462 - Pedido: 4501368826-Credito em Conta</infCpl>
</infAdic>
</infNFe>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
<SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
<Reference URI="#NFe35180502735564000106550010000651451879514583">
<Transforms>
<Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
<Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
<DigestValue>TCosCAEb7SMzRr+SWcVFaqje7qQ=</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>NWra9SqvlvntykNGqleEvuHRlDoJ0g50a1Ux6m/PpGn23BP9lkjz8WgmIFV9Gg2cyeyJXjl+OhCbzBUh9Qm2yLZH96W37yPb3SYJ/d+Jl6VCa1odoiKZ1ZQ/mEJv4mVkhjAMp6lMrkNk8KvU75SLAl19OD+dE6uTTbr6ERHdRv0=</SignatureValue>
<KeyInfo>
<X509Data>
<X509Certificate>MIICOTCCAaKgAwIBAgIQJpLn3fwH17RNXJHw5VenaDANBgkqhkiG9w0BAQUFADBbMVkwVwYDVQQDHlAAdwB3AHcALgBmAHMAaQBzAHQALgBjAG8AbQAuAGIAcgAgACgAUwBFAE0AIABWAEEATABJAEQAQQBEAEUAIABKAFUAUgDNAEQASQBDAEEAKTAeFw0xNTEyMjQwODI1MjVaFw0xODEyMjQwODI1MjVaMFsxWTBXBgNVBAMeUAB3AHcAdwAuAGYAcwBpAHMAdAAuAGMAbwBtAC4AYgByACAAKABTAEUATQAgAFYAQQBMAEkARABBAEQARQAgAEoAVQBSAM0ARABJAEMAQQApMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDX4BYmJiNwrPz9At2ioXYkMuMWAOf669VPS3SAzIKkJLhSE7Bf9sbTjGHRFZhs6STxKkMBKKnx6dqAEMGlQwDSU42/kqhGya28SUn3HigH2w3dR/536Gt4eAxyLp9ODNXUqWTpnraWCeT41kcnJ27cmOAUMxzxDcsA93n32L4u8wIDAQABMA0GCSqGSIb3DQEBBQUAA4GBAAN89zf+wSFL+4+2Ic8mGva8Z51EzYzNgYEqXwEMHxw4jCxxbsuxewQWedSGWi3SFZla6zmNUEA+UO95tSZ353klTvWDzut2BFSJOPLcMu8HPTTdnMt4ofVeVqKyi2vp+3t78vjqDAAfSAu5C9wT7stoUx41Nok+L6WbmpOIgvHy</X509Certificate>
</X509Data>
</KeyInfo>
</Signature>
</NFe>
<protNFe versao="3.10">
<infProt>
<tpAmb>1</tpAmb>
<verAplic>121132075</verAplic>
<chNFe>35180502735564000106550010000651451879514583</chNFe>
<dhRecbto>2018-05-22T09:37:46-03:00</dhRecbto>
<nProt>135180343925727</nProt>
<digVal>TCosCAEb7SMzRr+SWcVFaqje7qQ=</digVal>
<cStat>100</cStat>
<xMotivo>Autorizado o uso da NF-e</xMotivo>
</infProt>
</protNFe>
</nfeProc>