java使用POI jar包读写xls文件
生活随笔
收集整理的這篇文章主要介紹了
java使用POI jar包读写xls文件
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
主要使用poi jar來操作excel文件。代碼中用到的數(shù)據(jù)庫表信息見ORACLE之表。使用public ArrayList<Person> getPersonAllRecords()獲得所有的記錄。
?
1 public class PersonXLS { 2 3 public static void main(String[] args) throws IOException { 4 5 Workbook newExcel=new HSSFWorkbook();//創(chuàng)建一個(gè)excel 6 DBUtil dbUtil=new DBUtil();//數(shù)據(jù)庫工具類 7 Sheet newSheet0=newExcel.createSheet("Person Info");//創(chuàng)建一個(gè)sheet 8 String[] title={"MAP_ID","PERSON_CODE","PERSON_NAME","SEX","INSERT_TIME","UPDATE_TIME","POSITION","SALARY","DEPT"};//表頭 9 SimpleDateFormat sdf; 10 Row newRow0=newSheet0.createRow(0);//創(chuàng)建表頭 11 int i=0; 12 for(String s:title){//寫入表頭 13 Cell cell=newRow0.createCell(i++); 14 cell.setCellValue(s); 15 } 16 long sql_begin = System.currentTimeMillis(); 17 dbUtil.getConn();//數(shù)據(jù)庫連接 18 ArrayList<Person> persons=dbUtil.getPersonAllRecords();//獲取記錄 19 sdf=new SimpleDateFormat("MM/dd/yyyy"); 20 for(i=0;i<persons.size();i++){//寫入記錄 21 Row newRow=newSheet0.createRow(i+1); 22 newRow.createCell(0).setCellValue(persons.get(i).getMap_id()); 23 newRow.createCell(1).setCellValue(persons.get(i).getPerson_code()); 24 newRow.createCell(2).setCellValue(persons.get(i).getPerson_name()); 25 newRow.createCell(3).setCellValue(persons.get(i).getSex()); 26 newRow.createCell(4).setCellValue(sdf.format(persons.get(i).getInsert_time())); 27 newRow.createCell(5).setCellValue(sdf.format(persons.get(i).getUpdate_time())); 28 newRow.createCell(6).setCellValue(persons.get(i).getPosition()); 29 newRow.createCell(7).setCellValue(persons.get(i).getSalary()); 30 newRow.createCell(8).setCellValue(persons.get(i).getDept()); 31 } 32 sdf=new SimpleDateFormat("yyyyMMddHHmmss"); 33 FileOutputStream fos=new FileOutputStream("PersonInfo"+sdf.format(new Date())+System.currentTimeMillis()+".xls");//創(chuàng)建 34 newExcel.write(fos);//向輸入流寫入數(shù)據(jù) 35 System.out.println("sql cost :"+(System.currentTimeMillis()-sql_begin)+"ms.");//數(shù)據(jù)操作花費(fèi)時(shí)間 36 } 37 }?
output:
?
轉(zhuǎn)載于:https://www.cnblogs.com/hoaprox/p/5332427.html
總結(jié)
以上是生活随笔為你收集整理的java使用POI jar包读写xls文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: batch insert 1 milli
- 下一篇: 别让千里马被驴踢死