1. 程式人生 > >POI實現Excel導入導出

POI實現Excel導入導出

export 工作表 參數 type() del led line hellip reat

我們知道要創建一張excel你得知道excel由什麽組成,比如說sheet也就是一個工作表格,例如一行,一個單元格,單元格格式,單元格內容格式…這些都對應著poi裏面的一個類。

一個excel表格:

HSSFWorkbook wb = new HSSFWorkbook();

一個工作表格(sheet):

HSSFSheet sheet = wb.createSheet("測試表格");

一行(row):

HSSFRow row1 = sheet.createRow(0);

一個單元格(cell):

HSSFCell cell2 = row2.createCell((short)0)

單元格格式(cellstyle):

HSSFCellStyle style4 = wb.createCellStyle()

單元格內容格式()

HSSFDataFormat format= wb.createDataFormat();

1:首先創建一個po對象

package entity;

public class Student {
    private int no;
    private String name;
    
private int age; private String grage; public Student(int no, String name, int age, String grage) { super(); this.no = no; this.name = name; this.age = age; this.grage = grage; } public int getNo() { return no; } public
void setNo(int no) { this.no = no; } 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 getGrage() { return grage; } public void setGrage(String grage) { this.grage = grage; } }

2實現導出的功能:

 1 package demo;
 2 
 3 import java.io.FileOutputStream;
 4 import java.io.IOException;
 5 import java.sql.SQLException;
 6 import java.util.ArrayList;
 7 import java.util.Date;
 8 import java.util.List;
 9 
10 import org.apache.poi.hssf.usermodel.HSSFCell;
11 import org.apache.poi.hssf.usermodel.HSSFCellStyle;
12 import org.apache.poi.hssf.usermodel.HSSFDataFormat;
13 import org.apache.poi.hssf.usermodel.HSSFRow;
14 import org.apache.poi.hssf.usermodel.HSSFSheet;
15 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
16 import org.apache.poi.hssf.util.Region;
17 import org.apache.poi.ss.usermodel.Font;
18 
19 import entity.Student;
20 
21 public class Export_demo {
22     public static void main(String[] args) {
23         export();
24     }
25     
26     public static void export(){
27         List<Student> studens=new ArrayList<Student>();
28         for (int i = 1; i <=20; i++) {
29             Student s=new Student(i, "a"+i, 20+i-20, "三年級");
30             studens.add(s);
31         }
32         
33         HSSFWorkbook wb = new HSSFWorkbook();//創建一個excel文件
34         HSSFSheet sheet=wb.createSheet("學生信息");//創建一個工作薄
35         sheet.setColumnWidth((short)3, 20* 256);    //---》設置單元格寬度,因為一個單元格寬度定了那麽下面多有的單元格高度都確定了所以這個方法是sheet的  
36         sheet.setColumnWidth((short)4, 20* 256);    //--->第一個參數是指哪個單元格,第二個參數是單元格的寬度  
37         sheet.setDefaultRowHeight((short)300);    // ---->有得時候你想設置統一單元格的高度,就用這個方法
38         HSSFDataFormat format= wb.createDataFormat();   //--->單元格內容格式  
39         HSSFRow row1 = sheet.createRow(0);   //--->創建一行  
40           // 四個參數分別是:起始行,起始列,結束行,結束列 (單個單元格) 
41         sheet.addMergedRegion(new Region(0, (short) 0, 0, (short)5));//可以有合並的作用 
42         HSSFCell cell1 = row1.createCell((short)0);   //--->創建一個單元格  
43         cell1.setCellValue("學生信息總覽");
44         
45        
46         sheet.addMergedRegion(new Region(1, (short) 0, 1, (short)0));
47         HSSFRow row2= sheet.createRow(1);   ////創建第二列 標題
48         HSSFCell fen = row2.createCell((short)0);   //--->創建一個單元格  
49         fen.setCellValue("編號/屬性 ");
50         HSSFCell no = row2.createCell((short)1);   //--->創建一個單元格  
51         no.setCellValue("姓名 ");
52         HSSFCell age = row2.createCell((short)2);   //--->創建一個單元格  
53         age.setCellValue("年齡 ");
54         HSSFCell grage = row2.createCell((short)3);   //--->創建一個單元格  
55         grage.setCellValue("年級 ");
56         
57         for (int i = 0; i <studens .size(); i++) {
58              sheet.addMergedRegion(new Region(1+i+1, (short) 0, 1+i+1, (short)0));
59             HSSFRow rows= sheet.createRow(1+i+1);   ////創建第二列 標題
60             HSSFCell fens = rows.createCell((short)0);   //--->創建一個單元格  
61             fens.setCellValue(studens.get(i).getNo());
62             HSSFCell nos = rows.createCell((short)1);   //--->創建一個單元格  
63             nos.setCellValue(studens.get(i).getName());
64             HSSFCell ages = rows.createCell((short)2);   //--->創建一個單元格  
65             ages.setCellValue(studens.get(i).getAge());
66             HSSFCell grages = rows.createCell((short)3);   //--->創建一個單元格  
67             grages.setCellValue(studens.get(i).getGrage());
68         }
69         FileOutputStream fileOut = null;  
70         try{              
71             fileOut = new FileOutputStream("d:\\studens.xls");  
72             wb.write(fileOut);  
73             //fileOut.close();  
74             System.out.print("OK");  
75         }catch(Exception e){  
76             e.printStackTrace();  
77         }  
78         finally{  
79             if(fileOut != null){  
80                 try {  
81                     fileOut.close();  
82                 } catch (IOException e) {  
83                     // TODO Auto-generated catch block  
84                     e.printStackTrace();  
85                 }  
86             }  
87         }  
88     }
89 }

效果圖:

技術分享圖片

3實現導入的功能:

 1 package demo;
 2 
 3 import java.io.FileInputStream;
 4 import java.io.FileNotFoundException;
 5 import java.io.InputStream;
 6 import java.text.SimpleDateFormat;
 7 import java.util.ArrayList;
 8 import java.util.Date;
 9 import java.util.List;
10 
11 import org.apache.poi.hssf.usermodel.HSSFCell;
12 import org.apache.poi.hssf.usermodel.HSSFDateUtil;
13 import org.apache.poi.hssf.usermodel.HSSFRow;
14 import org.apache.poi.hssf.usermodel.HSSFSheet;
15 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
16 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
17 
18 import entity.Student;
19 
20 public class Import_demo {
21     private static POIFSFileSystem fs;//poi文件流
22     private static HSSFWorkbook wb;//獲得execl
23     private static HSSFRow row;//獲得行
24     private static HSSFSheet sheet;//獲得工作簿
25     
26     public static void main(String[] args) throws FileNotFoundException {
27         InputStream in= new FileInputStream("d:\\studens.xls");
28         imports(in);
29     }
30     
31     public static void imports(InputStream in ){
32         String str = "";
33         try {
34              fs = new POIFSFileSystem(in);
35              wb = new HSSFWorkbook(fs);
36              sheet=wb.getSheetAt(0);
37              //int rowfirst=sheet.getFirstRowNum();
38              int rowend=sheet.getLastRowNum();
39              for (int i = 2; i <=rowend; i++) {
40                 row=sheet.getRow(i);
41                 //System.out.println(row.get);
42                 int colNum = row.getPhysicalNumberOfCells();//一行總列數
43                  int j = 0;
44                     while (j < colNum) {
45                         str += getCellFormatValue(row.getCell((short) j)).trim() + "-";
46                         j++;
47                     }
48                     System.out.println(str);
49                     str="";
50             }
51         } catch (Exception e) {
52             // TODO: handle exception
53         }
54     }
55     
56     private static String getCellFormatValue(HSSFCell cell) {
57         String cellvalue = "";
58         if (cell != null) {
59             // 判斷當前Cell的Type
60             switch (cell.getCellType()) {
61                 // 如果當前Cell的Type為NUMERIC
62                 case HSSFCell.CELL_TYPE_NUMERIC:
63                 case HSSFCell.CELL_TYPE_FORMULA: {
64                     // 判斷當前的cell是否為Date
65                     if (HSSFDateUtil.isCellDateFormatted(cell)) {
66                         Date date = cell.getDateCellValue();
67                         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
68                         cellvalue = sdf.format(date);
69                     }
70                     // 如果是純數字
71                     else {
72                         // 取得當前Cell的數值
73                         cellvalue = String.valueOf(cell.getNumericCellValue());
74                     }
75                     break;
76                 }
77                 // 如果當前Cell的Type為STRIN
78                 case HSSFCell.CELL_TYPE_STRING:
79                     // 取得當前的Cell字符串
80                     cellvalue = cell.getRichStringCellValue().getString();
81                     break;
82                 // 默認的Cell值
83                 default:
84                     cellvalue = " ";
85             }
86         } else {
87             cellvalue = "";
88         }
89         return cellvalue;
90     }
91     
92 }

效果:

技術分享圖片

代碼和jar寶下載路徑

https://download.csdn.net/download/cengjianggh/10418815

POI實現Excel導入導出