開發過程中經常需要用到數據的導入導出功能,之前用的是POI,這次使用JXL,JXL相對於POI來說要輕量簡潔許多,在數據量不大的情況下還是非常實用的。這里做一下使用JXL的學習記錄。首先需要導入相應的jar包,pom.xml中添加如下內容即可
<dependency> <groupId>net.sourceforge.jexcelapi</groupId> <artifactId>jxl</artifactId> <version>2.6.12</version> </dependency>
看圖說話:
0、數據實體類:
1、導出代碼:
2、導入代碼:
3、導出測試:
4、導出測試結果:
因為jxl不支持新本Excel格式,舊版本的表格最多只支持65536條數據;
這里測試導出最大65536條數據耗時1秒左右,速度還是非常快的,當然我這個跟我測試的實體字段比較少可能也有關系
5、導入測試:
6、導入測試結果:
以上就是Java實現Excel 導入導出的全部代碼了
下面附上代碼
數據實體類:
public class Customer { private String name; private Integer age; private String telephone; private String address; //這里get/set方法省略不貼 }
1、導出代碼:
public static void excelExport(List<Customer> list, String path) { WritableWorkbook book = null; try { // 創建一個Excel文件對象 book = Workbook.createWorkbook(new File(path)); // 創建Excel第一個選項卡對象 WritableSheet sheet = book.createSheet("第一頁", 0); // 設置表頭,第一行內容 // Label參數說明:第一個是列,第二個是行,第三個是要寫入的數據值,索引值都是從0開始 Label label1 = new Label(0, 0, "姓名");// 對應為第1列第1行的數據 Label label2 = new Label(1, 0, "年齡");// 對應為第2列第1行的數據 Label label3 = new Label(2, 0, "手機號碼");// 對應為第3列第1行的數據 Label label4 = new Label(3, 0, "住址");// 對應為第4列第1行的數據 // 添加單元格到選項卡中 sheet.addCell(label1); sheet.addCell(label2); sheet.addCell(label3); sheet.addCell(label4); // 遍歷集合並添加數據到行,每行對應一個對象 for (int i = 0; i < list.size(); i++) { Customer customer = list.get(i); // 表頭占據第一行,所以下面行數是索引值+1 // 跟上面添加表頭一樣添加單元格數據,這里為了方便直接使用鏈式編程 sheet.addCell(new Label(0, i + 1, customer.getName())); sheet.addCell(new Label(1, i + 1, customer.getAge().toString())); sheet.addCell(new Label(2, i + 1, customer.getTelephone())); sheet.addCell(new Label(3, i + 1, customer.getAddress())); } // 寫入數據到目標文件 book.write(); } catch (Exception e) { e.printStackTrace(); } finally { try { // 關閉 book.close(); } catch (Exception e) { e.printStackTrace(); } } }
2、導入代碼:
public static List<Customer> excelImport(String path) { List<Customer> list = new ArrayList<>(); Workbook book = null; try { // 獲取Excel對象 book = book.getWorkbook(new File(path)); // 獲取Excel第一個選項卡對象 Sheet sheet = book.getSheet(0); // 遍歷選項卡,第一行是表頭,所以索引數-1 for (int i = 0; i < sheet.getRows() - 1; i++) { Customer customer = new Customer(); // 獲取第一列第二行單元格對象 Cell cell = sheet.getCell(0, i + 1); customer.setName(cell.getContents()); // 獲取第二行其他數據 customer.setAge(Integer.parseInt(sheet.getCell(1, i + 1).getContents())); customer.setTelephone(sheet.getCell(2, i + 1).getContents()); customer.setAddress(sheet.getCell(3, i + 1).getContents()); list.add(customer); } // 返回導入的數據集合 return list; } catch (Exception e) { e.printStackTrace(); } finally { try { // 關閉 book.close(); } catch (Exception e) { e.printStackTrace(); } } return null; }
3、導出測試代碼:
public static void main(String[] args) { List<Customer> list = new ArrayList<>(); // 創建數據 for (int i = 0; i < 65535; i++) { Customer customer = new Customer(); customer.setName("隔壁老王_" + i); customer.setAge(i); customer.setTelephone("1301234" + i); customer.setAddress("浙江杭州西湖"); list.add(customer); } String path = "D:\\eclelTest\\xs.xls"; System.out.println("開始導出..."); long s1 = new Date().getTime(); // 開始導出 excelExport(list, path); long s2 = new Date().getTime(); long time = s2 - s1; System.out.println("導出完成!消耗時間:" + time + "毫秒"); }
4、導入測試代碼:
public static void main(String[] args) { String path = "D:\\eclelTest\\xs.xls"; List<Customer> list = excelImport(path); for (Customer customer : list) { System.out.println(customer); } }