Hoje trabalharemos com o JExcelAPI. Framwework Java Excel. Ele \u00e9 excelente para manipula\u00e7\u00e3o de planilhas eletr\u00f4nicas no formato XLS. A vers\u00e3o que utilizaremos trabalha tamb\u00e9m com o formato XLSX. <\/p>\n
Para quem precisa utlizar java e excel esta \u00e9 uma \u00f3tima ferramenta.<\/p>\n
\u00c9 poss\u00edvel realizar leitura e grava\u00e7\u00e3o de planilhas. Imagine que voc\u00ea precisa fazer exporta\u00e7\u00e3o de uma lista de seu sistema para uma planilha eletr\u00f4nica. Essa \u00e9 uma das solu\u00e7\u00f5es
\nque voc\u00ea deve analisar e testar em Java.<\/p>\n
<\/a><\/p>\n <\/p>\n Vamos iniciar digitando o c\u00f3digo abaixo em um programa Java b\u00e1sico.<\/p>\n Abra\u00e7os e bons estudos. Vida que segue!<\/p>\n xStream<\/a><\/p>\nAmbiente a ser configurado: <\/H2>
\nJDK 7
\nJExcelAPI – 2.7.12 <\/p>\n\r\npackage br.com.feltex.lab.excel;\r\n\r\nimport java.io.File;\r\nimport java.util.Calendar;\r\nimport java.util.Date;\r\nimport java.util.Random;\r\n\r\nimport jxl.Workbook;\r\nimport jxl.format.Colour;\r\nimport jxl.write.DateFormat;\r\nimport jxl.write.DateTime;\r\nimport jxl.write.Label;\r\nimport jxl.write.Number;\r\nimport jxl.write.WritableCell;\r\nimport jxl.write.WritableCellFormat;\r\nimport jxl.write.WritableFont;\r\nimport jxl.write.WritableSheet;\r\nimport jxl.write.WritableWorkbook;\r\n\r\npublic class CriarExcel {\r\n\tpublic static void main(String[] args) {\r\n\t\tSystem.out.println(\"Inicio\");\r\n\t\ttry {\r\n\t\t\tWritableWorkbook planilha = Workbook.createWorkbook(new File(\r\n\t\t\t\t\t\"c:\/temp\/saida.xls\"));\r\n\t\t\t\/\/ Adicionando o nome da aba\r\n\t\t\tWritableSheet aba = planilha.createSheet(\"ListaAlunos\", 0);\r\n\r\n\t\t\t\/\/ Cabe\u00e7alhos\r\n\t\t\tString cabecalho[] = new String[5];\r\n\t\t\tcabecalho[0] = \"ID\";\r\n\t\t\tcabecalho[1] = \"Nome\";\r\n\t\t\tcabecalho[2] = \"Telefone\";\r\n\t\t\tcabecalho[3] = \"E-mail\";\r\n\t\t\tcabecalho[4] = \"Data Cadastro\";\r\n\r\n\t\t\t\/\/ Cor de fundo das celular\r\n\t\t\tColour bckcolor = Colour.DARK_GREEN;\r\n\t\t\tWritableCellFormat cellFormat = new WritableCellFormat();\r\n\t\t\tcellFormat.setBackground(bckcolor);\r\n\r\n\t\t\t\/\/ Cor e tipo de fonte\r\n\t\t\tWritableFont fonte = new WritableFont(WritableFont.ARIAL);\r\n\t\t\tfonte.setColour(Colour.GOLD);\r\n\t\t\tcellFormat.setFont(fonte);\r\n\r\n\t\t\t\/\/ Write the Header to the excel file\r\n\t\t\tfor (int i = 0; i < cabecalho.length; i++) {\r\n\t\t\t\tLabel label = new Label(i, 0, cabecalho[i]);\r\n\t\t\t\taba.addCell(label);\r\n\t\t\t\tWritableCell cell = aba.getWritableCell(i, 0);\r\n\t\t\t\tcell.setCellFormat(cellFormat);\r\n\t\t\t}\r\n\r\n\t\t\tfor (int linha = 1; linha < 10; linha++) { \/\/ N\u00famero da linha\r\n\t\t\t\tRandom numeroAleatorio = new Random();\r\n\r\n\t\t\t\tNumber number = new Number(0, linha,\r\n\t\t\t\t\t\tnumeroAleatorio.nextInt(2000));\r\n\t\t\t\taba.addCell(number);\r\n\r\n\t\t\t\tLabel label = new Label(1, linha, \"Jose da Silva\");\r\n\t\t\t\taba.addCell(label);\r\n\r\n\t\t\t\tlabel = new Label(2, linha, \"2230-6625\");\r\n\t\t\t\taba.addCell(label);\r\n\r\n\t\t\t\tlabel = new Label(3, linha, \"josesilva@feltex.com.br\");\r\n\t\t\t\taba.addCell(label);\r\n\r\n\t\t\t\tDate data = Calendar.getInstance().getTime();\r\n\t\t\t\tDateFormat customDateFormat = new DateFormat(\r\n\t\t\t\t\t\t\"dd MMM yyyy hh:mm:ss\");\r\n\t\t\t\tWritableCellFormat dateFormat = new WritableCellFormat(\r\n\t\t\t\t\t\tcustomDateFormat);\r\n\t\t\t\tDateTime dateCell = new DateTime(4, linha, data, dateFormat);\r\n\t\t\t\taba.addCell(dateCell);\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\tplanilha.write();\r\n\t\t\t\/\/ Fecha o arquivo\r\n\t\t\tplanilha.close();\r\n\r\n\t\t} catch (Exception e) {\r\n\t\t\te.printStackTrace();\r\n\t\t}\r\n\t\tSystem.out.println(\"Fim\");\r\n\t}\r\n}\r\n<\/pre>\n
Conclus\u00e3o <\/H2>
\n Esta API disponibiliza uma gamad de recursos para manipular excel que torna o trabalho de exporta\u00e7\u00e3o de dados bem simples.
\n Para fazer download do Jar do JExcelAPI clique aqui<\/a><\/p>\nLinks relacionados<\/H2><\/p>\n