introduzir
Jacarta POI é um conjunto de APIs Java para acessar documentos formatados pela Microsoft. O Jacarta POI consiste em muitos componentes, incluindo o HSSF para operar arquivos de formato do Excel e HWPF para o Word Operating Word. Entre vários componentes, apenas o HSSF para o Operation Excel é relativamente maduro. Página inicial oficial http://poi.apache.org/index.html, documentação da API http://poi.apache.org/apidocs/index.html
concluir
Comentários completos foram adicionados ao código.
importar java.io.fileInputStream; importar java.io.fileOutputStream; importar java.io.inputStream; importar java.io.outputStream; importar java.util.arraylist; import java.util.list; import orgache.poi.hssf.ermodel.Usermodel.util.list; importação.apache.poi.hssf.ermodel.Usermodel.util.list; import.apache.poi.hssf.ermodel.Usermodel.util.list; org.apache.poi.hssf.usermodel.hssfcellStyle; importar org.apache.poi.hssf.usermodel.hssfrow; import orod.apache.poi.hsf.usermodel.hsfsheet; import.apache.poi.hsf.hsf.hssfsSheet; Exceloperate {public static void main (string [] args) {// crie tabela Excel Createexcel (getStudent ()); // LEIA LISTA DE TABELA DE EXCEL <VERGEM> LIST = ReadExcel (); System.out.println (list.toString ()); } / *** Inicialize dados** @return Data* / Lista estática privada <Dentulion> getStudent () {list <denty> list = new ArrayList <vertware> (); Aluno estudante1 = novo aluno ("Xiao Ming", 8, "Segunda Grau"); Aluno estudante2 = novo aluno ("xiao guang", 9, "segunda série"); Aluno estudante3 = novo aluno ("xiao hua", 10, "quarta série"); list.add (Student1); list.add (Student2); list.add (Student3); lista de retorno; } / ** * Crie Excel * * @param List * Data * / private estático void createExcel (list <vertware> list) {// Crie um arquivo Excel HSSFWorkbook WorkBows = new HSSFWorkbook (); // Crie uma planilha HSSFSheet Sheet = Workbook.CreateSheet ("Student Tabela Um"); // Adicione a linha do cabeçalho hssfrow hssfrow = sheet.crerowrow (0); // Defina o formato da célula para centralizar o hssfcellstyle celularySyle = Work.CreateCellStyle (); CellStyle.Setalignment (HSSFCellStyle.align_center); // Adicionar conteúdo de cabeçalho hssfcell headcell = hssfrow.createCell (0); headcell.setCellValue ("nome"); headcell.setCellStyle (CellStyle); headcell = hssfrow.createCell (1); headcell.setCellValue ("Age"); headcell.setCellStyle (CellStyle); headcell = hssfrow.createCell (2); headcell.setCellValue ("grau"); headcell.setCellStyle (CellStyle); // Adicione o conteúdo de dados para (int i = 0; i <list.size (); i ++) {hssfrow = sheet.createrow ((int) i+1); Aluno estudante = list.get (i); // Crie uma célula e defina o valor HSSFCell Cell = hssfrow.createCell (0); Cell.SetCellValue (Student.getName ()); Cell.setCellStyle (CellStyle); célula = hssfrow.createCell (1); Cell.SetCellValue (Student.getage ()); Cell.setCellStyle (CellStyle); célula = hssfrow.createCell (2); Cell.SetCellValue (Student.getGrade ()); Cell.setCellStyle (CellStyle); } // Salvar arquivo Excel Try {outputStream outputStream = new FileOutputStream ("d: /students.xls"); Work.write (outputStream); outputStream.Close (); } catch (Exceção e) {e.printStackTrace (); }} / ** * Leia o Excel * * @return Data Collection * / Private Static List <Arguns> readExcel () {list <vertware> list = new ArrayList <Dentudy> (); Pasta de trabalho do HSSFWorkbook = NULL; tente {// leia o arquivo do Excel inputStream inputStream = new FileInputStream ("d: /students.xls"); pasta de trabalho = novo HSSFWorkbook (InputStream); inputStream.close (); } catch (Exceção e) {e.printStackTrace (); } // Planilha de loop para (int numsheet = 0; numsheet <WorkBork.getNumberOfSheets (); numSheet ++) {hssfsheet hssfsheet = workbook.getSheetat (numheet); if (hssfsheet == null) {continua; } // Linha de loop para (int rownum = 1; rownum <= hssfsheet.getLastrownum (); rownum ++) {hssfrow hssfrow = hssfsheet.getRow (rownum); if (hssfrow == null) {continua; } // salve o conteúdo na célula no aluno de coleção estudante = new Student (); HSSFCell Cell = hssfrow.getCell (0); if (célula == null) {continue; } Student.SetName (Cell.getStringCellValue ()); célula = hssfrow.getCell (1); if (célula == null) {continue; } Student.setage ((int) Cell.getNumericCellValue ()); célula = hssfrow.getCell (2); if (célula == null) {continue; } Student.setGrade (Cell.getStringCellValue ()); list.add (aluno); }} Lista de retorno; }}Anexe o código da aula do aluno
Public Class Student {private String Name; private Int Age; Grade Private String; public student () {} public student (nome da string, idade int, grau de string) {super (); this.name = nome; this.age = idade; this.Grade = GRADE; } public string getName () {return name; } public void setName (nome da string) {this.name = name; } public int getage () {Age de retorno; } public void setage (int Age) {this.age = Age; } public string getGrade () {return Grade; } public void setGrade (grau de string) {this.grade = GRADE; } @Override public string tostring () {return }} Resultados do teste
Tabelas do Excel exportadas
Alunos
Imprima os dados do Read Excel
[Estudante [nome = xiao Ming, idade = 8, grau = segunda série], aluno [nome = xiao guang, idade = 9, grau = 3], aluno [nome = xiao hua, idade = 10, grau = 4]]
Resumir
O acima é o conteúdo inteiro deste artigo. Espero que o conteúdo deste artigo seja de ajuda para o seu estudo ou trabalho. Se você tiver alguma dúvida, pode deixar uma mensagem para se comunicar.