POI操作excel的匯入與匯出
阿新 • • 發佈:2018-12-24
趁著不忙,抽空寫了個用poi操作excel匯入匯出的功能的小例子。不多說,直接上程式碼!
poi的操作類:
package com.bao.excelUtil; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.text.DecimalFormat; import java.text.NumberFormat; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Row; /** * 操作excel的工具類 * @author Alex */ public class ExcelUtil { /** * 建立一個excel,返回一個excel物件 * @param sheetName * @param orderList * @return HSSFWorkbook * @throws Exception * @author Alex */ public static HSSFWorkbook createExcel(String filePath, String sheetName, List<String[]> orderList) throws Exception { FileOutputStream fileOutputStream = new FileOutputStream(filePath); HSSFWorkbook wb = new HSSFWorkbook(); //建立一個工作區 try { HSSFSheet sheet = wb.createSheet(sheetName); //建立一個sheet頁 //建立第一行,如果有多個sheet頁可以自己進行拓展 HSSFCell cell = null; if (orderList != null) { for (int i = 0; i < orderList.size(); i++) { HSSFRow row = sheet.createRow(i); String[] con = (String[]) orderList.get(i); for (int j = 0; j < con.length; j++) { if (i == 0) { //給第一行賦值,也就是標題行 cell = row.createCell(j); cell.setCellValue(con[j]); } else { //給其他行賦值,也就是資料 cell = row.createCell(j); cell.setCellValue(con[j]); } } } CellStyle cellStyle2 = wb.createCellStyle(); //建立一個樣式物件 cellStyle2.setAlignment(CellStyle.ALIGN_CENTER); //設定內容居中 } wb.write(fileOutputStream); } catch (Exception e) { throw e; }finally { fileOutputStream.flush(); fileOutputStream.close(); } return wb; } /** * 讀取excel資料的方法 * @param file * @return List<HashMap> * @throws Exception * @author Alex */ public static List<String[]> getExcel(String filePath) throws Exception { List<String[]> dataList = new ArrayList<String[]>(); File file = new File(filePath); FileInputStream fs = new FileInputStream(file); HSSFWorkbook wb = new HSSFWorkbook(fs); // 獲得工作 HSSFSheet sheet = wb.getSheetAt(0); // 拿到第一個sheet頁 Iterator<Row> rows = sheet.rowIterator(); // 拿到第一行 while (rows.hasNext()) { // 如果有值 HSSFRow row = (HSSFRow) rows.next(); // 拿到當前行 if(row.getRowNum() != 0) { //不讀取標題(可以根據自己需求進行修改) String[] data = new String[row.getLastCellNum()]; Iterator<Cell> cells = row.cellIterator(); // 拿到當前行所有列的集合 while (cells.hasNext()) { HSSFCell cell = (HSSFCell) cells.next(); // 拿到列值 String cellValue = ""; // 存放單元格的值 switch (cell.getCellType()) { // 判斷單元格的型別,取出單元格的值 case HSSFCell.CELL_TYPE_NUMERIC: // 處理數字型別 去掉科學計數法格式 double strCell = cell.getNumericCellValue(); DecimalFormat formatCell = (DecimalFormat) NumberFormat.getPercentInstance(); formatCell.applyPattern("0"); String value = formatCell.format(strCell); if (Double.parseDouble(value) != strCell) { formatCell.applyPattern(Double.toString(strCell)); value = formatCell.format(strCell); } cellValue = value; break; case HSSFCell.CELL_TYPE_STRING: cellValue = cell.getStringCellValue(); break; case HSSFCell.CELL_TYPE_BOOLEAN: cellValue = String.valueOf(cell.getBooleanCellValue()); break; case HSSFCell.CELL_TYPE_FORMULA: cellValue = cell.getCellFormula(); break; default: break; } data[cell.getColumnIndex()] = cellValue; } dataList.add(data); } } return dataList; } }
測試poi的實現類:
package com.bao.test; import java.util.ArrayList; import java.util.List; import com.bao.excelUtil.ExcelUtil; public class testMain { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub //模擬資料 String filePath = "file/testDate.xls"; String sheetName = "測試"; List<String[]> testList = new ArrayList<String[]>(); String[] title = {"姓名","年齡","性別"}; String[] data1 = {"張三","25","男"}; String[] data2 = {"趙四","22","男"}; String[] data3 = {"王武","12","男"}; testList.add(title); testList.add(data1); testList.add(data2); testList.add(data3); try { //匯出excel //ExcelUtil.createExcel(filePath, sheetName, testList); //讀取excel List<String[]> result = ExcelUtil.getExcel(filePath); for(String[] data : result){ for(int i = 0;i<data.length ;i++) { System.out.print(data[i] + " "); } System.out.println(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
用的jar包是poi-3.7-20101029.jar.如果大家想了解更多,直接下載api進行學習!寫的比較簡單,希望大家多多指點!