導入
Jakarta Poiは、MicrosoftフォーマットドキュメントにアクセスするためのJava APIのセットです。 Jakarta Poiは、Excelフォーマットファイルを操作するためのHSSFや、操作用のHWPFを含む多くのコンポーネントで構成されています。さまざまなコンポーネントの中で、Excelを操作するためのHSSFのみが比較的成熟しています。公式ホームページhttp://poi.apache.org/index.html、APIドキュメントhttp://poi.apache.org/apidocs/index.html
成し遂げる
完全なコメントがコードに追加されました。
java.io.fileinputStream; Import java.io.fileoutputStream; Import java.io.inputStream; Import java.io.outputStream; Import Java.util.ArrayList; Import java.util.list; import org.apache.poi.hssf.usermodel.hssfcell; org.apache.poi.hssf.usermodel.HSSFCellStyle;import org.apache.poi.hssf.usermodel.HSSFRow;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;public class exceloperate {public static void main(string [] args){// Excel Table CreateExcel(getStudent()); // Excelテーブルリスト<Student> List = ReadExcel(); System.out.println(list.toString()); } / ***データを初期化** @return data* / private static list <Student> getStudent(){list <Student> list = new ArrayList <Student>(); Student Student1 = New Student( "Xiao Ming"、8、 "2年生"); Student Student2 = new Student( "Xiao Guang"、9、 "2年生"); Student Student3 = new Student( "Xiao Hua"、10、「4年生」); list.add(desute1); list.add(desudent2); list.add(desudent3);返品リスト。 } / ** * excel * * @param list * data * / private static void createexcel(list <sudtent> list){// excelファイルhssfworkbook workbook = new hssfworkbook(); //ワークシートを作成するhssfsheetシート= workbook.createsheet( "Student Table One"); //ヘッダー行を追加するhssfrow hssfrow = sheet.createrow(0); //セル形式を中心に設定しますhssfcellstyle cellstyle = workbook.createcellstyle(); CellStyle.SetAlignment(hssfcellstyle.align_center); //ヘッダーコンテンツを追加hssfcell headcell = hssfrow.createcell(0); headcell.setCellValue( "name"); headcell.setCellStyle(CellStyle); headcell = hssfrow.createcell(1); headcell.setCellValue( "age"); headcell.setCellStyle(CellStyle); headcell = hssfrow.createcell(2); headcell.setCellValue( "Grade"); headcell.setCellStyle(CellStyle); //(int i = 0; i <list.size(); i ++){hssfrow = sheet.createrow((int)i+1);学生学生= list.get(i); //セルを作成し、値を設定しますhssfcellセル= hssfrow.createcell(0); cell.setCellValue(Student.getName()); Cell.SetCellStyle(CellStyle); cell = hssfrow.createcell(1); cell.setCellValue(Student.Getage()); Cell.SetCellStyle(CellStyle); cell = hssfrow.createcell(2); cell.setCellValue(Student.GetGrade()); Cell.SetCellStyle(CellStyle); } // excelファイルを保存してください{outputstream outputStream = new fileoutputStream( "d:/students.xls"); workbook.write(outputStream); outputStream.close(); } catch(Exception e){e.printstacktrace(); }} / ** * excel * * @return data collection * / private static list <Student> readexcel(){list <sudtent> list = new arraylist <sustent>(); hssfworkbookワークブック= null; try {// excel file inputstream inputstream = new fileinputStream( "d:/students.xls"); workbook = new HSSFWorkBook(inputStream); inputstream.close(); } catch(Exception e){e.printstacktrace(); } //(int numsheet = 0; numsheet <workbook.getnumberofsheets(); numsheet ++){hssfsheet hssfsheet = workbook.getsheetat(numsheet); if(hssfsheet == null){続行; } //ループrow for(int rownum = 1; rownum <= hssfsheet.getlastrownum(); rownum ++){hssfrow hssfrow = hssfsheet.getrow(rownum); if(hssfrow == null){継続; } //セル内の内容をコレクションに保存します学生= new Student(); hssfcell cell = hssfrow.getCell(0); if(cell == null){続行; } Student.setName(cell.getStringCellValue()); cell = hssfrow.getCell(1); if(cell == null){続行; } Student.Setage((int)cell.getNumericCellValue()); cell = hssfrow.getCell(2); if(cell == null){続行; } Student.setGrade(cell.getStringCellValue()); list.add(学生); }} return list; }}学生クラスのコードを添付します
パブリッククラスの学生{プライベート文字列名;プライベートインクエイジ;プライベートストリンググレード。 Public Student(){} public Student(string name、int age、string grad){super(); this.name = name; this.age = age; this.grade = grade; } public string getname(){return name; } public void setName(string name){this.name = name; } public int getage(){return age; } public void Setage(int age){this.age = age; } public string getGrade(){return grade; } public void setGrade(string grade){this.grade = grade; } @Override public String toString(){return "sustent [name =" + name + "、age =" + age + "、grade =" + grade + "]"; }}テスト結果
エクスポートされたExcelテーブル
学生
読み取りExcelデータを印刷します
[学生[name = xiao ming、age = 8、grade = 2年生]、学生[name = xiao guang、age = 9、grade = 3]、学生[name = xiao hua、age = 10、grade = 4]]]
要約します
上記は、この記事のコンテンツ全体です。この記事の内容があなたの研究や仕事に役立つことを願っています。ご質問がある場合は、メッセージを残してコミュニケーションをとることができます。