今天須要對比2個excel表的內容找出相同;因爲要學的還不少上手很慢因此在這作個分享但願對初學的有幫助;java
先是pom的配置:apache
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-scratchpad</artifactId> <version>3.11-beta2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.11-beta2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> <version>3.11-beta2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-excelant</artifactId> <version>3.11-beta2</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>1.0.1</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.8.1</version> </dependency> </dependencies>
下邊是我網上找到的工具類可是無法在簡單的java項目中實現導出因此只用了導入功能網絡
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; import java.util.Map; 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.Font; import org.apache.poi.ss.usermodel.IndexedColors; 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 ExcelUtil { private ExcelUtil() { } private final String excel2003L = ".xls";//2003- 版本的excel private final String excel2007U = ".xlsx";//2007+ 版本的excel public static final ExcelUtil getInstance() { return ExcelUtilHolder.instance; } private static final class ExcelUtilHolder { private static final ExcelUtil instance = new ExcelUtil(); } private Workbook getWorkbook(InputStream inStr, String filename) throws Exception { Workbook workbook; String fileType = filename.substring(filename.lastIndexOf(".")); if (excel2003L.equals(fileType)) { workbook = new HSSFWorkbook(inStr);//2003- } else if (excel2007U.equals(fileType)) { workbook = new XSSFWorkbook(inStr);//2007+ } else { throw new Exception("解析的文件格式有誤"); } return workbook; } /** * 獲取excel中的數據 */ public List<List<Object>> readExcelData(InputStream in, String filename) throws Exception { List<List<Object>> list; //建立Excel工做薄 Workbook work = getWorkbook(in, filename); if (null == work) { throw new Exception("建立Excel工做薄爲空!"); } Sheet sheet = null; Row row = null; Cell cell = null; list = new ArrayList<List<Object>>(); //遍歷Excel中全部的sheet for (int i = 0; i < work.getNumberOfSheets(); i++) { sheet = work.getSheetAt(i); if (sheet == null) { continue; } //遍歷當前sheet中的全部行 for (int j = sheet.getFirstRowNum(); j <= sheet.getLastRowNum(); j++) { row = sheet.getRow(j); if (row == null || row.getFirstCellNum() == j) { continue; } //遍歷全部的列 List<Object> li = new ArrayList<Object>(); for (int y = row.getFirstCellNum(); y < row.getLastCellNum(); y++) { cell = row.getCell(y); //經過getCellValue方法獲取當前行每一列中的數據 li.add(getCellValue(cell)); } //將每一行的數據添加到list list.add(li); } } in.close(); return list; } /** * 獲取每一個單元格的內容 */ private Object getCellValue(Cell cell) { Object value = null; DecimalFormat df = new DecimalFormat("0");//格式化number String字符串 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//日期格式化 switch (cell.getCellType()) { case Cell.CELL_TYPE_STRING: value = cell.getRichStringCellValue().getString(); break; case Cell.CELL_TYPE_NUMERIC: if ("General".equals(cell.getCellStyle().getDataFormatString())) { value = df.format(cell.getNumericCellValue()); } else if ("m/d/yy".equals(cell.getCellStyle().getDataFormatString())) { value = sdf.format(cell.getDateCellValue()); } else { value = cell.getNumericCellValue(); } break; case Cell.CELL_TYPE_BOOLEAN: value = cell.getBooleanCellValue(); break; case Cell.CELL_TYPE_BLANK: value = ""; break; default: break; } return value; } /** * 數據轉成excel * * @param dataList 須要轉換的數據 * @param sheetName 生成的excel表名 * @param columnName 每一列的名字(key) * @return */ public Workbook dataToExcel(List<Map<String, String>> dataList, String sheetName, String[] columnName) { int columnNum = columnName.length; Workbook workbook = new HSSFWorkbook(); //建立第一頁,並命名 Sheet sheet = workbook.createSheet(sheetName); //建立第一行 Row row = sheet.createRow(0); // 建立兩種單元格格式 CellStyle cs = workbook.createCellStyle(); CellStyle cs2 = workbook.createCellStyle(); // 建立兩種字體 Font f = workbook.createFont(); Font f2 = workbook.createFont(); // 建立第一種字體樣式(用於列名) f.setFontHeightInPoints((short) 10); f.setColor(IndexedColors.BLACK.getIndex()); // f.setBold(Font.COLOR_NORMAL); // f.setBoldweight(Font.COLOR_RED); // 建立第二種字體樣式(用於值) f2.setFontHeightInPoints((short) 10); f2.setColor(IndexedColors.BLACK.getIndex()); //設置列名 for (int i = 0; i < columnNum; i++) { Cell cell = row.createCell(i); cell.setCellValue(columnName[i]); cell.setCellStyle(cs); } //設置每行每列的值 int rowNum = dataList.size(); for (int j = 1; j <= rowNum; j++) { //建立一行 Row r = sheet.createRow(j); for (int k = 0; k < columnNum; k++) { Cell cell = r.createCell(k); cell.setCellValue(dataList.get(j - 1).get(columnName[k])); cell.setCellStyle(cs2); } } return workbook; } static HSSFWorkbook workbook; public static void writeToExcel(String fileDir,String sheetName,List<Map> mapList) throws Exception{ //建立workbook File file = new File(fileDir); try { workbook = new HSSFWorkbook(new FileInputStream(file)); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } //流 FileOutputStream out = null; HSSFSheet sheet = workbook.getSheet(sheetName); // 獲取表格的總行數 // int rowCount = sheet.getLastRowNum() + 1; // 須要加一 // 獲取表頭的列數 int columnCount = sheet.getRow(0).getLastCellNum()+1; try { // 得到表頭行對象 HSSFRow titleRow = sheet.getRow(0); if(titleRow!=null){ for(int rowId=0;rowId<mapList.size();rowId++){ Map map = mapList.get(rowId); HSSFRow newRow=sheet.createRow(rowId+1); for (short columnIndex = 0; columnIndex < columnCount; columnIndex++) { //遍歷表頭 String mapKey = titleRow.getCell(columnIndex).toString().trim().toString().trim(); HSSFCell cell = newRow.createCell(columnIndex); cell.setCellValue(map.get(mapKey)==null ? null : map.get(mapKey).toString()); } } } out = new FileOutputStream(fileDir); workbook.write(out); } catch (Exception e) { throw e; } finally { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } private static final String EXCEL_XLS = "xls"; private static final String EXCEL_XLSX = "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("名字" + j).toString(); for (int k = 0; k <= columnNumCount; k++) { // 在一行內循環 Cell first = row.createCell(0); first.setCellValue(name); } } // 建立文件輸出流,準備輸出電子表格:這個必須有,不然你在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; } }
接下來是另外一個地方找到的導出工具類xss
import java.io.IOException; import java.io.OutputStream; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.text.SimpleDateFormat; import java.util.Collection; import java.util.Date; import java.util.Iterator; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.commons.lang3.StringUtils; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFRichTextString; 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.hssf.util.HSSFColor; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFColor; import org.apache.poi.xssf.usermodel.XSSFFont; import org.apache.poi.xssf.usermodel.XSSFRichTextString; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; /** * 導出Excel * @author liuyazhuang * * @param <T> */ public class ExportExcelUtil<T>{ // 2007 版本以上 最大支持1048576行 public final static String EXCEl_FILE_2007 = "2007"; // 2003 版本 最大支持65536 行 public final static String EXCEL_FILE_2003 = "2003"; /** * <p> * 導出無頭部標題行Excel <br> * 時間格式默認:yyyy-MM-dd hh:mm:ss <br> * </p> * * @param title 表格標題 * @param dataset 數據集合 * @param out 輸出流 * @param version 2003 或者 2007,不傳時默認生成2003版本 */ public void exportExcel(String title, Collection<T> dataset, OutputStream out, String version) { if(StringUtils.isEmpty(version) || EXCEL_FILE_2003.equals(version.trim())){ exportExcel2003(title, null, dataset, out, "yyyy-MM-dd HH:mm:ss"); }else{ exportExcel2007(title, null, dataset, out, "yyyy-MM-dd HH:mm:ss"); } } /** * <p> * 導出帶有頭部標題行的Excel <br> * 時間格式默認:yyyy-MM-dd hh:mm:ss <br> * </p> * * @param title 表格標題 * @param headers 頭部標題集合 * @param dataset 數據集合 * @param out 輸出流 * @param version 2003 或者 2007,不傳時默認生成2003版本 */ public void exportExcel(String title,String[] headers, Collection<T> dataset, OutputStream out,String version) { if(StringUtils.isBlank(version) || EXCEL_FILE_2003.equals(version.trim())){ exportExcel2003(title, headers, dataset, out, "yyyy-MM-dd HH:mm:ss"); }else{ exportExcel2007(title, headers, dataset, out, "yyyy-MM-dd HH:mm:ss"); } } /** * <p> * 通用Excel導出方法,利用反射機制遍歷對象的全部字段,將數據寫入Excel文件中 <br> * 此版本生成2007以上版本的文件 (文件後綴:xlsx) * </p> * * @param title * 表格標題名 * @param headers * 表格頭部標題集合 * @param dataset * 須要顯示的數據集合,集合中必定要放置符合JavaBean風格的類的對象。此方法支持的 * JavaBean屬性的數據類型有基本數據類型及String,Date * @param out * 與輸出設備關聯的流對象,能夠將EXCEL文檔導出到本地文件或者網絡中 * @param pattern * 若是有時間數據,設定輸出格式。默認爲"yyyy-MM-dd hh:mm:ss" */ @SuppressWarnings({ "unchecked", "rawtypes" }) public void exportExcel2007(String title, String[] headers, Collection<T> dataset, OutputStream out, String pattern) { // 聲明一個工做薄 XSSFWorkbook workbook = new XSSFWorkbook(); // 生成一個表格 XSSFSheet sheet = workbook.createSheet(title); // 設置表格默認列寬度爲15個字節 sheet.setDefaultColumnWidth(20); // 生成一個樣式 XSSFCellStyle style = workbook.createCellStyle(); // 設置這些樣式 style.setFillForegroundColor(new XSSFColor(java.awt.Color.gray)); style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); style.setBorderBottom(XSSFCellStyle.BORDER_THIN); style.setBorderLeft(XSSFCellStyle.BORDER_THIN); style.setBorderRight(XSSFCellStyle.BORDER_THIN); style.setBorderTop(XSSFCellStyle.BORDER_THIN); style.setAlignment(XSSFCellStyle.ALIGN_CENTER); // 生成一個字體 XSSFFont font = workbook.createFont(); font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD); font.setFontName("宋體"); font.setColor(new XSSFColor(java.awt.Color.BLACK)); font.setFontHeightInPoints((short) 11); // 把字體應用到當前的樣式 style.setFont(font); // 生成並設置另外一個樣式 XSSFCellStyle style2 = workbook.createCellStyle(); style2.setFillForegroundColor(new XSSFColor(java.awt.Color.WHITE)); style2.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); style2.setBorderBottom(XSSFCellStyle.BORDER_THIN); style2.setBorderLeft(XSSFCellStyle.BORDER_THIN); style2.setBorderRight(XSSFCellStyle.BORDER_THIN); style2.setBorderTop(XSSFCellStyle.BORDER_THIN); style2.setAlignment(XSSFCellStyle.ALIGN_CENTER); style2.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER); // 生成另外一個字體 XSSFFont font2 = workbook.createFont(); font2.setBoldweight(XSSFFont.BOLDWEIGHT_NORMAL); // 把字體應用到當前的樣式 style2.setFont(font2); // 產生表格標題行 XSSFRow row = sheet.createRow(0); XSSFCell cellHeader; for (int i = 0; i < headers.length; i++) { cellHeader = row.createCell(i); cellHeader.setCellStyle(style); cellHeader.setCellValue(new XSSFRichTextString(headers[i])); } // 遍歷集合數據,產生數據行 Iterator<T> it = dataset.iterator(); int index = 0; T t; Field[] fields; Field field; XSSFRichTextString richString; Pattern p = Pattern.compile("^//d+(//.//d+)?$"); Matcher matcher; String fieldName; String getMethodName; XSSFCell cell; Class tCls; Method getMethod; Object value; String textValue; SimpleDateFormat sdf = new SimpleDateFormat(pattern); while (it.hasNext()) { index++; row = sheet.createRow(index); t = (T) it.next(); // 利用反射,根據JavaBean屬性的前後順序,動態調用getXxx()方法獲得屬性值 fields = t.getClass().getDeclaredFields(); for (int i = 0; i < fields.length; i++) { cell = row.createCell(i); cell.setCellStyle(style2); field = fields[i]; fieldName = field.getName(); getMethodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); try { tCls = t.getClass(); getMethod = tCls.getMethod(getMethodName, new Class[] {}); value = getMethod.invoke(t, new Object[] {}); // 判斷值的類型後進行強制類型轉換 textValue = null; if (value instanceof Integer) { cell.setCellValue((Integer) value); } else if (value instanceof Float) { textValue = String.valueOf((Float) value); cell.setCellValue(textValue); } else if (value instanceof Double) { textValue = String.valueOf((Double) value); cell.setCellValue(textValue); } else if (value instanceof Long) { cell.setCellValue((Long) value); } if (value instanceof Boolean) { textValue = "是"; if (!(Boolean) value) { textValue = "否"; } } else if (value instanceof Date) { textValue = sdf.format((Date) value); } else { // 其它數據類型都看成字符串簡單處理 if (value != null) { textValue = value.toString(); } } if (textValue != null) { matcher = p.matcher(textValue); if (matcher.matches()) { // 是數字看成double處理 cell.setCellValue(Double.parseDouble(textValue)); } else { richString = new XSSFRichTextString(textValue); cell.setCellValue(richString); } } } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } finally { // 清理資源 } } } try { workbook.write(out); } catch (IOException e) { e.printStackTrace(); } } /** * <p> * 通用Excel導出方法,利用反射機制遍歷對象的全部字段,將數據寫入Excel文件中 <br> * 此方法生成2003版本的excel,文件名後綴:xls <br> * </p> * * @param title * 表格標題名 * @param headers * 表格頭部標題集合 * @param dataset * 須要顯示的數據集合,集合中必定要放置符合JavaBean風格的類的對象。此方法支持的 * JavaBean屬性的數據類型有基本數據類型及String,Date * @param out * 與輸出設備關聯的流對象,能夠將EXCEL文檔導出到本地文件或者網絡中 * @param pattern * 若是有時間數據,設定輸出格式。默認爲"yyyy-MM-dd hh:mm:ss" */ @SuppressWarnings({ "unchecked", "rawtypes" }) public void exportExcel2003(String title, String[] headers, Collection<T> dataset, OutputStream out, String pattern) { // 聲明一個工做薄 HSSFWorkbook workbook = new HSSFWorkbook(); // 生成一個表格 HSSFSheet sheet = workbook.createSheet(title); // 設置表格默認列寬度爲15個字節 sheet.setDefaultColumnWidth(20); // 生成一個樣式 HSSFCellStyle style = workbook.createCellStyle(); // 設置這些樣式 style.setFillForegroundColor(HSSFColor.GREY_50_PERCENT.index); style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); style.setBorderBottom(HSSFCellStyle.BORDER_THIN); style.setBorderLeft(HSSFCellStyle.BORDER_THIN); style.setBorderRight(HSSFCellStyle.BORDER_THIN); style.setBorderTop(HSSFCellStyle.BORDER_THIN); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 生成一個字體 HSSFFont font = workbook.createFont(); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); font.setFontName("宋體"); font.setColor(HSSFColor.WHITE.index); font.setFontHeightInPoints((short) 11); // 把字體應用到當前的樣式 style.setFont(font); // 生成並設置另外一個樣式 HSSFCellStyle style2 = workbook.createCellStyle(); style2.setFillForegroundColor(HSSFColor.WHITE.index); style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); style2.setBorderBottom(HSSFCellStyle.BORDER_THIN); style2.setBorderLeft(HSSFCellStyle.BORDER_THIN); style2.setBorderRight(HSSFCellStyle.BORDER_THIN); style2.setBorderTop(HSSFCellStyle.BORDER_THIN); style2.setAlignment(HSSFCellStyle.ALIGN_CENTER); style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 生成另外一個字體 HSSFFont font2 = workbook.createFont(); font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); // 把字體應用到當前的樣式 style2.setFont(font2); // 產生表格標題行 HSSFRow row = sheet.createRow(0); HSSFCell cellHeader; for (int i = 0; i < headers.length; i++) { cellHeader = row.createCell(i); cellHeader.setCellStyle(style); cellHeader.setCellValue(new HSSFRichTextString(headers[i])); } // 遍歷集合數據,產生數據行 Iterator<T> it = dataset.iterator(); int index = 0; T t; Field[] fields; Field field; HSSFRichTextString richString; Pattern p = Pattern.compile("^//d+(//.//d+)?$"); Matcher matcher; String fieldName; String getMethodName; HSSFCell cell; Class tCls; Method getMethod; Object value; String textValue; SimpleDateFormat sdf = new SimpleDateFormat(pattern); while (it.hasNext()) { index++; row = sheet.createRow(index); t = (T) it.next(); // 利用反射,根據JavaBean屬性的前後順序,動態調用getXxx()方法獲得屬性值 fields = t.getClass().getDeclaredFields(); for (int i = 0; i < fields.length; i++) { cell = row.createCell(i); cell.setCellStyle(style2); field = fields[i]; fieldName = field.getName(); getMethodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); try { tCls = t.getClass(); getMethod = tCls.getMethod(getMethodName, new Class[] {}); value = getMethod.invoke(t, new Object[] {}); // 判斷值的類型後進行強制類型轉換 textValue = null; if (value instanceof Integer) { cell.setCellValue((Integer) value); } else if (value instanceof Float) { textValue = String.valueOf((Float) value); cell.setCellValue(textValue); } else if (value instanceof Double) { textValue = String.valueOf((Double) value); cell.setCellValue(textValue); } else if (value instanceof Long) { cell.setCellValue((Long) value); } if (value instanceof Boolean) { textValue = "是"; if (!(Boolean) value) { textValue = "否"; } } else if (value instanceof Date) { textValue = sdf.format((Date) value); } else { // 其它數據類型都看成字符串簡單處理 if (value != null) { textValue = value.toString(); } } if (textValue != null) { matcher = p.matcher(textValue); if (matcher.matches()) { // 是數字看成double處理 cell.setCellValue(Double.parseDouble(textValue)); } else { richString = new HSSFRichTextString(textValue); cell.setCellValue(richString); } } } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } finally { // 清理資源 } } } try { workbook.write(out); } catch (IOException e) { e.printStackTrace(); } } }
導出 須要一個實體類:工具
public class Hu { public Hu() { super(); // TODO Auto-generated constructor stub } private String name; public void setName(String name) { this.name = name; } public Hu(String name) { super(); this.name = name; } public String getName() { return name; } }
準備工做就弄好了 接下來 實踐字體
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import entity.Hu; import util.ExcelUtil; import util.ExportExcelUtil; public class Poitest { public static void main(String[] args) throws Exception { InputStream in = new FileInputStream(new File("C:\\Users\\Administrator\\Desktop\\進件滿.xlsx")); InputStream in1 = new FileInputStream(new File("C:\\Users\\Administrator\\Desktop\\test.xlsx")); List<List<Object>> list = ExcelUtil.getInstance().readExcelData(in, "進件滿.xlsx"); List<List<Object>> list1 = ExcelUtil.getInstance().readExcelData(in1, "test.xlsx"); List<String> li1 = new ArrayList<String>(); System.out.println(list1.size()); String n = ""; for(int i = 0; i < list.size(); i++) { if(list1.contains(list.get(i))) { // list1.remove(list.get(i)); n = list.get(i).toString(); // l.add(String.valueOf(s)); n = n.replace("[", ""); n = n.replace("]", ""); li1.add(n); } } ExportExcelUtil<Hu> util = new ExportExcelUtil<Hu>(); // 準備數據 List<Hu> list5 = new ArrayList(); Hu h = new Hu(); String a = ""; for(Object s:li1) { a = String.valueOf(s); // l.add(String.valueOf(s)); a = a.replace("[", ""); a = a.replace("]", ""); list5.add(new Hu(a)); ; } String[] columnNames = {"姓名"}; util.exportExcel("用戶導出", columnNames, list5, new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test1.xlsx"), ExportExcelUtil.EXCEl_FILE_2007); } }
而後會在規定的excel文件裏寫入你比較成功的內容this
這樣就實現了excel的導入導出spa