1:添加處理excel的依賴jar包
<!-- 引入poi,解析workbook視圖 --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.16</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.14</version> </dependency> <!-- 處理excel和上面功能是一樣的--> <dependency> <groupId>net.sourceforge.jexcelapi</groupId> <artifactId>jxl</artifactId> <version>2.6.10</version> </dependency>
2:向excel中寫入內容的類
WriteExcel.java
package com.li.controller; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class WriteExcel { private static final String EXCEL_XLS = "xls"; private static final String EXCEL_XLSX = "xlsx"; public static void main(String[] args) { Map<String, String> dataMap=new HashMap<String, String>(); dataMap.put("BankName", "BankName"); dataMap.put("Addr", "Addr"); dataMap.put("Phone", "Phone"); List<Map> list=new ArrayList<Map>(); list.add(dataMap); writeExcel(list, 3, "D:/writeExcel.xlsx"); } public static void writeExcel(List<Map> dataList, int cloumnCount,String finalXlsxPath){ OutputStream out = null; try { // 獲取總列數 int columnNumCount = cloumnCount; // 讀取Excel文檔 File finalXlsxFile = new File(finalXlsxPath); Workbook workBook = getWorkbok(finalXlsxFile); // sheet 對應一個工作頁 Sheet sheet = workBook.getSheetAt(0); /** * 刪除原有數據,除了屬性列 */ int rowNumber = sheet.getLastRowNum(); // 第一行從0開始算 System.out.println("原始數據總行數,除屬性列:" + rowNumber); for (int i = 1; i <= rowNumber; i++) { Row row = sheet.getRow(i); sheet.removeRow(row); } // 創建文件輸出流,輸出電子表格:這個必須有,否則你在sheet上做的任何操作都不會有效 out = new FileOutputStream(finalXlsxPath); workBook.write(out); /** * 往Excel中寫新數據 */ for (int j = 0; j < dataList.size(); j++) { // 創建一行:從第二行開始,跳過屬性列 Row row = sheet.createRow(j + 1); // 得到要插入的每一條記錄 Map dataMap = dataList.get(j); String name = dataMap.get("BankName").toString(); String address = dataMap.get("Addr").toString(); String phone = dataMap.get("Phone").toString(); for (int k = 0; k <= columnNumCount; k++) { // 在一行內循環 Cell first = row.createCell(0); first.setCellValue(name); Cell second = row.createCell(1); second.setCellValue(address); Cell third = row.createCell(2); third.setCellValue(phone); } } // 創建文件輸出流,准備輸出電子表格:這個必須有,否則你在sheet上做的任何操作都不會有效 out = new FileOutputStream(finalXlsxPath); workBook.write(out); } catch (Exception e) { e.printStackTrace(); } finally{ try { if(out != null){ out.flush(); out.close(); } } catch (IOException e) { e.printStackTrace(); } } System.out.println("數據導出成功"); } /** * 判斷Excel的版本,獲取Workbook * @param in * @param filename * @return * @throws IOException */ public static Workbook getWorkbok(File file) throws IOException{ Workbook wb = null; FileInputStream in = new FileInputStream(file); if(file.getName().endsWith(EXCEL_XLS)){ //Excel 2003 wb = new HSSFWorkbook(in); }else if(file.getName().endsWith(EXCEL_XLSX)){ // Excel 2007/2010 wb = new XSSFWorkbook(in); } return wb; } }
3:讀取Excel中的數據,並寫入list中
package com.li.controller; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import jxl.Sheet; import jxl.Workbook; import jxl.read.biff.BiffException; public class ReadExcel { public static void main(String[] args) { ReadExcel obj = new ReadExcel(); // 此處為我創建Excel路徑:E:/zhanhj/studysrc/jxl下 File file = new File("D:/readExcel.xls"); List excelList = obj.readExcel(file); System.out.println("list中的數據打印出來"); for (int i = 0; i < excelList.size(); i++) { List list = (List) excelList.get(i); for (int j = 0; j < list.size(); j++) { System.out.print(list.get(j)); } System.out.println(); } } // 去讀Excel的方法readExcel,該方法的入口參數為一個File對象 public List readExcel(File file) { try { // 創建輸入流,讀取Excel InputStream is = new FileInputStream(file.getAbsolutePath()); // jxl提供的Workbook類 Workbook wb = Workbook.getWorkbook(is); // Excel的頁簽數量 int sheet_size = wb.getNumberOfSheets(); for (int index = 0; index < sheet_size; index++) { List<List> outerList=new ArrayList<List>(); // 每個頁簽創建一個Sheet對象 Sheet sheet = wb.getSheet(index); // sheet.getRows()返回該頁的總行數 for (int i = 0; i < sheet.getRows(); i++) { List innerList=new ArrayList(); // sheet.getColumns()返回該頁的總列數 for (int j = 0; j < sheet.getColumns(); j++) { String cellinfo = sheet.getCell(j, i).getContents(); if(cellinfo.isEmpty()){ continue; } innerList.add(cellinfo); System.out.print(cellinfo); } outerList.add(i, innerList); System.out.println(); } return outerList; } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (BiffException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } }
4:在D盤下面創建readExcel.xls(有內容) 和writeExcel.xlsx即可