本身封裝的poi操做Excel工具類

       在上一篇文章使用poi讀寫Excel中分享了一下poi操做Excel的簡單示例,此次要分享一下我封裝的一個Excel操做的工具類。java


       該工具類主要完成的功能是:讀取Excel、彙總Excel的功能。在讀取時,能夠設定開始和結束讀取的位置、設定是否讀取多個sheet、設定讀取那個或者那些sheet等。在彙總時,如設定是否覆蓋目標文件、設定是否比較檢查重複內容、設定檢查重複的列索引等功能。具體來演示一下吧:apache


       工具類源碼:xss

package com.tgb.ccl.excel.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

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;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * Excel文件操做工具類,包括讀、寫、合併等功能
 * 
 * @author  : 龍軒
 * @group   : tgb8
 * @Version : 1.00
 * @Date    : 2014-10-29 上午12:40:44
 */
public class ExcelUtil {
	
	//%%%%%%%%-------常量部分 開始----------%%%%%%%%%
	/**
	 * 默認的開始讀取的行位置爲第一行(索引值爲0)
	 */
	private final static int READ_START_POS = 0;
	
	/**
	 * 默認結束讀取的行位置爲最後一行(索引值=0,用負數來表示倒數第n行)
	 */
	private final static int READ_END_POS = 0;
	
	/**
	 * 默認Excel內容的開始比較列位置爲第一列(索引值爲0)
	 */
	private final static int COMPARE_POS = 0;
	
	/**
	 * 默認多文件合併的時須要作內容比較(相同的內容不重複出現)
	 */
	private final static boolean NEED_COMPARE = true;
	
	/**
	 * 默認多文件合併的新文件遇到名稱重複時,進行覆蓋
	 */
	private final static boolean NEED_OVERWRITE = true;
	
	/**
	 * 默認只操做一個sheet
	 */
	private final static boolean ONLY_ONE_SHEET = true;
	
	/**
	 * 默認讀取第一個sheet中(只有當ONLY_ONE_SHEET = true時有效)
	 */
	private final static int SELECTED_SHEET = 0;
	
	/**
	 * 默認從第一個sheet開始讀取(索引值爲0)
	 */
	private final static int READ_START_SHEET= 0;
	
	/**
	 * 默認在最後一個sheet結束讀取(索引值=0,用負數來表示倒數第n行)
	 */
	private final static int READ_END_SHEET = 0;
	
	/**
	 * 默認打印各類信息
	 */
	private final static boolean PRINT_MSG = true;
	
	//%%%%%%%%-------常量部分 結束----------%%%%%%%%%
	

	//%%%%%%%%-------字段部分 開始----------%%%%%%%%%
	/**
	 * Excel文件路徑
	 */
	private String excelPath = "data.xlsx";

	/**
	 * 設定開始讀取的位置,默認爲0
	 */
	private int startReadPos = READ_START_POS;

	/**
	 * 設定結束讀取的位置,默認爲0,用負數來表示倒數第n行
	 */
	private int endReadPos = READ_END_POS;
	
	/**
	 * 設定開始比較的列位置,默認爲0
	 */
	private int comparePos = COMPARE_POS;

	/**
	 *  設定彙總的文件是否須要替換,默認爲true
	 */
	private boolean isOverWrite = NEED_OVERWRITE;
	
	/**
	 *  設定是否須要比較,默認爲true(僅當不覆寫目標內容是有效,即isOverWrite=false時有效)
	 */
	private boolean isNeedCompare = NEED_COMPARE;
	
	/**
	 * 設定是否只操做第一個sheet
	 */
	private boolean onlyReadOneSheet = ONLY_ONE_SHEET;
	
	/**
	 * 設定操做的sheet在索引值
	 */
	private int selectedSheetIdx =SELECTED_SHEET;
	
	/**
	 * 設定操做的sheet的名稱
	 */
	private String selectedSheetName = "";
	
	/**
	 * 設定開始讀取的sheet,默認爲0
	 */
	private int startSheetIdx = READ_START_SHEET;

	/**
	 * 設定結束讀取的sheet,默認爲0,用負數來表示倒數第n行	
	 */
	private int endSheetIdx = READ_END_SHEET;
	
	/**
	 * 設定是否打印消息
	 */
	private boolean printMsg = PRINT_MSG;
	
	
	//%%%%%%%%-------字段部分 結束----------%%%%%%%%%
	

	public static void main(String[] args) {
		ExcelUtil eu = new ExcelUtil();
		
		//從第一行開始讀取
		eu.setStartReadPos(1);
		
		String src_xlspath = "D:\\2.xls";
		String dist_xlsPath = "D:\\1.xls";
		List<Row> rowList;
		try {
			rowList = eu.readExcel(src_xlspath);
			//eu.writeExcel_xls(rowList, src_xlspath, dist_xlsPath);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public ExcelUtil(){
		
	}
	
	public ExcelUtil(String excelPath){
		this.excelPath = excelPath;
	}
	
	/**
	 * 還原設定(實際上是從新new一個新的對象並返回)
	 * @return
	 */
	public ExcelUtil RestoreSettings(){
		ExcelUtil instance = new  ExcelUtil(this.excelPath);
		return instance;
	}
	
	/**
	 * 自動根據文件擴展名,調用對應的讀取方法
	 * 
	 * @Title: writeExcel
	 * @Date : 2014-9-11 下午01:50:38
	 * @param xlsPath
	 * @throws IOException
	 */
	public List<Row> readExcel() throws IOException{
		return readExcel(this.excelPath);
	}

	/**
	 * 自動根據文件擴展名,調用對應的讀取方法
	 * 
	 * @Title: writeExcel
	 * @Date : 2014-9-11 下午01:50:38
	 * @param xlsPath
	 * @throws IOException
	 */
	public List<Row> readExcel(String xlsPath) throws IOException{
		
		//擴展名爲空時,
		if (xlsPath.equals("")){
			throw new IOException("文件路徑不能爲空!");
		}else{
			File file = new File(xlsPath);
			if(!file.exists()){
				throw new IOException("文件不存在!");
			}
		}
		
		//獲取擴展名
		String ext = xlsPath.substring(xlsPath.lastIndexOf(".")+1);
		
		try {
			
			if("xls".equals(ext)){				//使用xls方式讀取
				return readExcel_xls(xlsPath);
			}else if("xls".equals(ext)){		//使用xlsx方式讀取
				return readExcel_xlsx(xlsPath);
			}else{									//依次嘗試xls、xlsx方式讀取
				out("您要操做的文件沒有擴展名,正在嘗試以xls方式讀取...");
				try{
					return readExcel_xls(xlsPath);
				} catch (IOException e1) {
					out("嘗試以xls方式讀取,結果失敗!,正在嘗試以xlsx方式讀取...");
					try{
						return readExcel_xlsx(xlsPath);
					} catch (IOException e2) {
						out("嘗試以xls方式讀取,結果失敗!\n請您確保您的文件是Excel文件,而且無損,而後再試。");
						throw e2;
					}
				}
			}
		} catch (IOException e) {
			throw e;
		}
	}
	
	/**
	 * 自動根據文件擴展名,調用對應的寫入方法
	 * 
	 * @Title: writeExcel
	 * @Date : 2014-9-11 下午01:50:38
	 * @param rowList
	 * @throws IOException
	 */
	public void writeExcel(List<Row> rowList) throws IOException{
		writeExcel(rowList,excelPath);
	}
	
	/**
	 * 自動根據文件擴展名,調用對應的寫入方法
	 * 
	 * @Title: writeExcel
	 * @Date : 2014-9-11 下午01:50:38
	 * @param rowList
	 * @param xlsPath
	 * @throws IOException
	 */
	public void writeExcel(List<Row> rowList, String xlsPath) throws IOException {

		//擴展名爲空時,
		if (xlsPath.equals("")){
			throw new IOException("文件路徑不能爲空!");
		}
		
		//獲取擴展名
		String ext = xlsPath.substring(xlsPath.lastIndexOf(".")+1);
		
		try {
			
			if("xls".equals(ext)){				//使用xls方式寫入
				writeExcel_xls(rowList,xlsPath);
			}else if("xls".equals(ext)){		//使用xlsx方式寫入
				writeExcel_xlsx(rowList,xlsPath);
			}else{									//依次嘗試xls、xlsx方式寫入
				out("您要操做的文件沒有擴展名,正在嘗試以xls方式寫入...");
				try{
					writeExcel_xls(rowList,xlsPath);
				} catch (IOException e1) {
					out("嘗試以xls方式寫入,結果失敗!,正在嘗試以xlsx方式讀取...");
					try{
						writeExcel_xlsx(rowList,xlsPath);
					} catch (IOException e2) {
						out("嘗試以xls方式寫入,結果失敗!\n請您確保您的文件是Excel文件,而且無損,而後再試。");
						throw e2;
					}
				}
			}
		} catch (IOException e) {
			throw e;
		}
	}
	
	/**
	 * 修改Excel(97-03版,xls格式)
	 * 
	 * @Title: writeExcel_xls
	 * @Date : 2014-9-11 下午01:50:38
	 * @param rowList
	 * @param dist_xlsPath
	 * @throws IOException
	 */
	public void writeExcel_xls(List<Row> rowList, String dist_xlsPath) throws IOException {
		writeExcel_xls(rowList, excelPath,dist_xlsPath);
	}

	/**
	 * 修改Excel(97-03版,xls格式)
	 * 
	 * @Title: writeExcel_xls
	 * @Date : 2014-9-11 下午01:50:38
	 * @param rowList
	 * @param src_xlsPath
	 * @param dist_xlsPath
	 * @throws IOException
	 */
	public void writeExcel_xls(List<Row> rowList, String src_xlsPath, String dist_xlsPath) throws IOException {

		// 判斷文件路徑是否爲空
		if (dist_xlsPath == null || dist_xlsPath.equals("")) {
			out("文件路徑不能爲空");
			throw new IOException("文件路徑不能爲空");
		}
		// 判斷文件路徑是否爲空
		if (src_xlsPath == null || src_xlsPath.equals("")) {
			out("文件路徑不能爲空");
			throw new IOException("文件路徑不能爲空");
		}

		// 判斷列表是否有數據,若是沒有數據,則返回
		if (rowList == null || rowList.size() == 0) {
			out("文檔爲空");
			return;
		}

		try {
			HSSFWorkbook wb = null;

			// 判斷文件是否存在
			File file = new File(dist_xlsPath);
			if (file.exists()) {
				// 若是複寫,則刪除後
				if (isOverWrite) {
					file.delete();
					// 若是文件不存在,則建立一個新的Excel
					// wb = new HSSFWorkbook();
					// wb.createSheet("Sheet1");
					wb = new HSSFWorkbook(new FileInputStream(src_xlsPath));
				} else {
					// 若是文件存在,則讀取Excel
					wb = new HSSFWorkbook(new FileInputStream(file));
				}
			} else {
				// 若是文件不存在,則建立一個新的Excel
				// wb = new HSSFWorkbook();
				// wb.createSheet("Sheet1");
				wb = new HSSFWorkbook(new FileInputStream(src_xlsPath));
			}

			// 將rowlist的內容寫到Excel中
			writeExcel(wb, rowList, dist_xlsPath);

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 修改Excel(97-03版,xls格式)
	 * 
	 * @Title: writeExcel_xls
	 * @Date : 2014-9-11 下午01:50:38
	 * @param rowList
	 * @param dist_xlsPath
	 * @throws IOException
	 */
	public void writeExcel_xlsx(List<Row> rowList, String dist_xlsPath) throws IOException {
		writeExcel_xls(rowList, excelPath , dist_xlsPath);
	}

	/**
	 * 修改Excel(2007版,xlsx格式)
	 * 
	 * @Title: writeExcel_xlsx
	 * @Date : 2014-9-11 下午01:50:38
	 * @param rowList
	 * @param xlsPath
	 * @throws IOException
	 */
	public void writeExcel_xlsx(List<Row> rowList, String src_xlsPath, String dist_xlsPath) throws IOException {

		// 判斷文件路徑是否爲空
		if (dist_xlsPath == null || dist_xlsPath.equals("")) {
			out("文件路徑不能爲空");
			throw new IOException("文件路徑不能爲空");
		}
		// 判斷文件路徑是否爲空
		if (src_xlsPath == null || src_xlsPath.equals("")) {
			out("文件路徑不能爲空");
			throw new IOException("文件路徑不能爲空");
		}

		// 判斷列表是否有數據,若是沒有數據,則返回
		if (rowList == null || rowList.size() == 0) {
			out("文檔爲空");
			return;
		}

		try {
			// 讀取文檔
			XSSFWorkbook wb = null;

			// 判斷文件是否存在
			File file = new File(dist_xlsPath);
			if (file.exists()) {
				// 若是複寫,則刪除後
				if (isOverWrite) {
					file.delete();
					// 若是文件不存在,則建立一個新的Excel
					// wb = new XSSFWorkbook();
					// wb.createSheet("Sheet1");
					wb = new XSSFWorkbook(new FileInputStream(src_xlsPath));
				} else {
					// 若是文件存在,則讀取Excel
					wb = new XSSFWorkbook(new FileInputStream(file));
				}
			} else {
				// 若是文件不存在,則建立一個新的Excel
				// wb = new XSSFWorkbook();
				// wb.createSheet("Sheet1");
				wb = new XSSFWorkbook(new FileInputStream(src_xlsPath));
			}
			// 將rowlist的內容添加到Excel中
			writeExcel(wb, rowList, dist_xlsPath);

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * //讀取Excel 2007版,xlsx格式
	 * 
	 * @Title: readExcel_xlsx
	 * @Date : 2014-9-11 上午11:43:11
	 * @return
	 * @throws IOException
	 */
	public List<Row> readExcel_xlsx() throws IOException {
		return readExcel_xlsx(excelPath);
	}

	/**
	 * //讀取Excel 2007版,xlsx格式
	 * 
	 * @Title: readExcel_xlsx
	 * @Date : 2014-9-11 上午11:43:11
	 * @return
	 * @throws Exception
	 */
	public List<Row> readExcel_xlsx(String xlsPath) throws IOException {
		// 判斷文件是否存在
		File file = new File(xlsPath);
		if (!file.exists()) {
			throw new IOException("文件名爲" + file.getName() + "Excel文件不存在!");
		}

		XSSFWorkbook wb = null;
		List<Row> rowList = new ArrayList<Row>();
		try {
			FileInputStream fis = new FileInputStream(file);
			// 去讀Excel
			wb = new XSSFWorkbook(fis);

			// 讀取Excel 2007版,xlsx格式
			rowList = readExcel(wb);

		} catch (IOException e) {
			e.printStackTrace();
		}
		return rowList;
	}

	/***
	 * 讀取Excel(97-03版,xls格式)
	 * 
	 * @throws IOException
	 * 
	 * @Title: readExcel
	 * @Date : 2014-9-11 上午09:53:21
	 */
	public List<Row> readExcel_xls() throws IOException {
		return readExcel_xls(excelPath);
	}

	/***
	 * 讀取Excel(97-03版,xls格式)
	 * 
	 * @throws Exception
	 * 
	 * @Title: readExcel
	 * @Date : 2014-9-11 上午09:53:21
	 */
	public List<Row> readExcel_xls(String xlsPath) throws IOException {

		// 判斷文件是否存在
		File file = new File(xlsPath);
		if (!file.exists()) {
			throw new IOException("文件名爲" + file.getName() + "Excel文件不存在!");
		}

		HSSFWorkbook wb = null;// 用於Workbook級的操做,建立、刪除Excel
		List<Row> rowList = new ArrayList<Row>();

		try {
			// 讀取Excel
			wb = new HSSFWorkbook(new FileInputStream(file));

			// 讀取Excel 97-03版,xls格式
			rowList = readExcel(wb);

		} catch (IOException e) {
			e.printStackTrace();
		}
		return rowList;
	}

	/***
	 * 讀取單元格的值
	 * 
	 * @Title: getCellValue
	 * @Date : 2014-9-11 上午10:52:07
	 * @param cell
	 * @return
	 */
	private String getCellValue(Cell cell) {
		Object result = "";
		if (cell != null) {
			switch (cell.getCellType()) {
			case Cell.CELL_TYPE_STRING:
				result = cell.getStringCellValue();
				break;
			case Cell.CELL_TYPE_NUMERIC:
				result = cell.getNumericCellValue();
				break;
			case Cell.CELL_TYPE_BOOLEAN:
				result = cell.getBooleanCellValue();
				break;
			case Cell.CELL_TYPE_FORMULA:
				result = cell.getCellFormula();
				break;
			case Cell.CELL_TYPE_ERROR:
				result = cell.getErrorCellValue();
				break;
			case Cell.CELL_TYPE_BLANK:
				break;
			default:
				break;
			}
		}
		return result.toString();
	}

	/**
	 * 通用讀取Excel
	 * 
	 * @Title: readExcel
	 * @Date : 2014-9-11 上午11:26:53
	 * @param wb
	 * @return
	 */
	private List<Row> readExcel(Workbook wb) {
		List<Row> rowList = new ArrayList<Row>();
		
		int sheetCount = 1;//須要操做的sheet數量
		
		Sheet sheet = null;
		if(onlyReadOneSheet){	//只操做一個sheet
			// 獲取設定操做的sheet(若是設定了名稱,按名稱查,不然按索引值查)
			sheet =selectedSheetName.equals("")? wb.getSheetAt(selectedSheetIdx):wb.getSheet(selectedSheetName);
		}else{							//操做多個sheet
			sheetCount = wb.getNumberOfSheets();//獲取能夠操做的總數量
		}
		
		// 獲取sheet數目
		for(int t=startSheetIdx; t<sheetCount+endSheetIdx;t++){
			// 獲取設定操做的sheet
			if(!onlyReadOneSheet) {
				sheet =wb.getSheetAt(t);
			}
			
			//獲取最後行號
			int lastRowNum = sheet.getLastRowNum();

			if(lastRowNum>0){	//若是>0,表示有數據
				out("\n開始讀取名爲【"+sheet.getSheetName()+"】的內容:");
			}
			
			Row row = null;
			// 循環讀取
			for (int i = startReadPos; i <= lastRowNum + endReadPos; i++) {
				row = sheet.getRow(i);
				if (row != null) {
					rowList.add(row);
					out("第"+(i+1)+"行:",false);
					 // 獲取每一單元格的值
					 for (int j = 0; j < row.getLastCellNum(); j++) {
						 String value = getCellValue(row.getCell(j));
						 if (!value.equals("")) {
							 out(value + " | ",false);
						 }
					 }
					 out("");
				}
			}
		}
		return rowList;
	}

	/**
	 * 修改Excel,並另存爲
	 * 
	 * @Title: WriteExcel
	 * @Date : 2014-9-11 下午01:33:59
	 * @param wb
	 * @param rowList
	 * @param xlsPath
	 */
	private void writeExcel(Workbook wb, List<Row> rowList, String xlsPath) {

		if (wb == null) {
			out("操做文檔不能爲空!");
			return;
		}

		Sheet sheet = wb.getSheetAt(0);// 修改第一個sheet中的值

		// 若是每次重寫,那麼則從開始讀取的位置寫,不然果獲取源文件最新的行。
		int lastRowNum = isOverWrite ? startReadPos : sheet.getLastRowNum() + 1;
		int t = 0;//記錄最新添加的行數
		out("要添加的數據總條數爲:"+rowList.size());
		for (Row row : rowList) {
			if (row == null) continue;
			// 判斷是否已經存在該數據
			int pos = findInExcel(sheet, row);

			Row r = null;// 若是數據行已經存在,則獲取後重寫,不然自動建立新行。
			if (pos >= 0) {
				sheet.removeRow(sheet.getRow(pos));
				r = sheet.createRow(pos);
			} else {
				r = sheet.createRow(lastRowNum + t++);
			}
			
			//用於設定單元格樣式
			CellStyle newstyle = wb.createCellStyle();
			
			//循環爲新行建立單元格
			for (int i = row.getFirstCellNum(); i < row.getLastCellNum(); i++) {
				Cell cell = r.createCell(i);// 獲取數據類型
				cell.setCellValue(getCellValue(row.getCell(i)));// 複製單元格的值到新的單元格
				// cell.setCellStyle(row.getCell(i).getCellStyle());//出錯
				if (row.getCell(i) == null) continue;
				copyCellStyle(row.getCell(i).getCellStyle(), newstyle); // 獲取原來的單元格樣式
				cell.setCellStyle(newstyle);// 設置樣式
				// sheet.autoSizeColumn(i);//自動跳轉列寬度
			}
		}
		out("其中檢測到重複條數爲:" + (rowList.size() - t) + " ,追加條數爲:"+t);
		
		// 統一設定合併單元格
		setMergedRegion(sheet);
		
		try {
			// 從新將數據寫入Excel中
			FileOutputStream outputStream = new FileOutputStream(xlsPath);
			wb.write(outputStream);
			outputStream.flush();
			outputStream.close();
		} catch (Exception e) {
			out("寫入Excel時發生錯誤! ");
			e.printStackTrace();
		}
	}

	/**
	 * 查找某行數據是否在Excel表中存在,返回行數。
	 * 
	 * @Title: findInExcel
	 * @Date : 2014-9-11 下午02:23:12
	 * @param sheet
	 * @param row
	 * @return
	 */
	private int findInExcel(Sheet sheet, Row row) {
		int pos = -1;

		try {
			// 若是覆寫目標文件,或者不須要比較,則直接返回
			if (isOverWrite || !isNeedCompare) {
				return pos;
			}
			for (int i = startReadPos; i <= sheet.getLastRowNum() + endReadPos; i++) {
				Row r = sheet.getRow(i);
				if (r != null && row != null) {
					String v1 = getCellValue(r.getCell(comparePos));
					String v2 = getCellValue(row.getCell(comparePos));
					if (v1.equals(v2)) {
						pos = i;
						break;
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return pos;
	}

	/**
	 * 複製一個單元格樣式到目的單元格樣式
	 * 
	 * @param fromStyle
	 * @param toStyle
	 */
	public static void copyCellStyle(CellStyle fromStyle, CellStyle toStyle) {
		toStyle.setAlignment(fromStyle.getAlignment());
		// 邊框和邊框顏色
		toStyle.setBorderBottom(fromStyle.getBorderBottom());
		toStyle.setBorderLeft(fromStyle.getBorderLeft());
		toStyle.setBorderRight(fromStyle.getBorderRight());
		toStyle.setBorderTop(fromStyle.getBorderTop());
		toStyle.setTopBorderColor(fromStyle.getTopBorderColor());
		toStyle.setBottomBorderColor(fromStyle.getBottomBorderColor());
		toStyle.setRightBorderColor(fromStyle.getRightBorderColor());
		toStyle.setLeftBorderColor(fromStyle.getLeftBorderColor());

		// 背景和前景
		toStyle.setFillBackgroundColor(fromStyle.getFillBackgroundColor());
		toStyle.setFillForegroundColor(fromStyle.getFillForegroundColor());

		// 數據格式
		toStyle.setDataFormat(fromStyle.getDataFormat());
		toStyle.setFillPattern(fromStyle.getFillPattern());
		// toStyle.setFont(fromStyle.getFont(null));
		toStyle.setHidden(fromStyle.getHidden());
		toStyle.setIndention(fromStyle.getIndention());// 首行縮進
		toStyle.setLocked(fromStyle.getLocked());
		toStyle.setRotation(fromStyle.getRotation());// 旋轉
		toStyle.setVerticalAlignment(fromStyle.getVerticalAlignment());
		toStyle.setWrapText(fromStyle.getWrapText());

	}

	/**
	 * 獲取合併單元格的值
	 * 
	 * @param sheet
	 * @param row
	 * @param column
	 * @return
	 */
	public void setMergedRegion(Sheet sheet) {
		int sheetMergeCount = sheet.getNumMergedRegions();

		for (int i = 0; i < sheetMergeCount; i++) {
			// 獲取合併單元格位置
			CellRangeAddress ca = sheet.getMergedRegion(i);
			int firstRow = ca.getFirstRow();
			if (startReadPos - 1 > firstRow) {// 若是第一個合併單元格格式在正式數據的上面,則跳過。
				continue;
			}
			int lastRow = ca.getLastRow();
			int mergeRows = lastRow - firstRow;// 合併的行數
			int firstColumn = ca.getFirstColumn();
			int lastColumn = ca.getLastColumn();
			// 根據合併的單元格位置和大小,調整全部的數據行格式,
			for (int j = lastRow + 1; j <= sheet.getLastRowNum(); j++) {
				// 設定合併單元格
				sheet.addMergedRegion(new CellRangeAddress(j, j + mergeRows, firstColumn, lastColumn));
				j = j + mergeRows;// 跳過已合併的行
			}

		}
	}
	

	/**
	 * 打印消息,
	 * @param msg 消息內容
	 * @param tr 換行
	 */
	private void out(String msg){
		if(printMsg){
			out(msg,true);
		}
	}
	/**
	 * 打印消息,
	 * @param msg 消息內容
	 * @param tr 換行
	 */
	private void out(String msg,boolean tr){
		if(printMsg){
			System.out.print(msg+(tr?"\n":""));
		}
	}

	public String getExcelPath() {
		return this.excelPath;
	}

	public void setExcelPath(String excelPath) {
		this.excelPath = excelPath;
	}

	public boolean isNeedCompare() {
		return isNeedCompare;
	}

	public void setNeedCompare(boolean isNeedCompare) {
		this.isNeedCompare = isNeedCompare;
	}

	public int getComparePos() {
		return comparePos;
	}

	public void setComparePos(int comparePos) {
		this.comparePos = comparePos;
	}

	public int getStartReadPos() {
		return startReadPos;
	}

	public void setStartReadPos(int startReadPos) {
		this.startReadPos = startReadPos;
	}

	public int getEndReadPos() {
		return endReadPos;
	}

	public void setEndReadPos(int endReadPos) {
		this.endReadPos = endReadPos;
	}

	public boolean isOverWrite() {
		return isOverWrite;
	}

	public void setOverWrite(boolean isOverWrite) {
		this.isOverWrite = isOverWrite;
	}

	public boolean isOnlyReadOneSheet() {
		return onlyReadOneSheet;
	}

	public void setOnlyReadOneSheet(boolean onlyReadOneSheet) {
		this.onlyReadOneSheet = onlyReadOneSheet;
	}

	public int getSelectedSheetIdx() {
		return selectedSheetIdx;
	}

	public void setSelectedSheetIdx(int selectedSheetIdx) {
		this.selectedSheetIdx = selectedSheetIdx;
	}

	public String getSelectedSheetName() {
		return selectedSheetName;
	}

	public void setSelectedSheetName(String selectedSheetName) {
		this.selectedSheetName = selectedSheetName;
	}

	public int getStartSheetIdx() {
		return startSheetIdx;
	}

	public void setStartSheetIdx(int startSheetIdx) {
		this.startSheetIdx = startSheetIdx;
	}

	public int getEndSheetIdx() {
		return endSheetIdx;
	}

	public void setEndSheetIdx(int endSheetIdx) {
		this.endSheetIdx = endSheetIdx;
	}

	public boolean isPrintMsg() {
		return printMsg;
	}

	public void setPrintMsg(boolean printMsg) {
		this.printMsg = printMsg;
	}
}

       以上就是工具類的所有代碼,如今演示一下讀取的功能:工具

public void testRead() {
		try {
			
			ExcelUtil eu = new ExcelUtil();
			eu.setExcelPath("d:\\2.xls");
			
			System.out.println("=======測試Excel 默認 讀取========");
			eu.readExcel();
			
			System.out.println("\n=======測試Excel 從第四行讀取,倒數第二行結束========");
			eu = eu.RestoreSettings();//還原設定
			eu.setStartReadPos(3);
			eu.setEndReadPos(-1);
			eu.readExcel();
			
			System.out.println("\n=======測試Excel 讀取第二個sheet========");
			eu = eu.RestoreSettings();//還原設定
			eu.setSelectedSheetIdx(1);
			eu.readExcel();
			
			System.out.println("\n=======測試Excel 讀取全部的sheet========");
			eu = eu.RestoreSettings();//還原設定
			eu.setOnlyReadOneSheet(false);
			eu.readExcel();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
       操做的Excel文件內容以下:
執行結果以下:


       上面代碼的提示和結果已經解釋的很清楚了,我就不在這裏過多介紹。測試


       如今演示多個Excel的彙總合併功能,把上面的Excel內容合併到另外一個Excel表中,這個Excel內容以下:this


       測試代碼以下:spa

public void testMerge(){
		try {
			ExcelUtil eu1 = new ExcelUtil();//用來讀取源xls
			ExcelUtil eu2 = new ExcelUtil();//用來讀取目標xls,用於演示合併結果
			eu1.setExcelPath("d:\\2.xls");
			eu2.setExcelPath("d:\\1.xls");
			
			System.out.println("\n=======修改前,1.xls中的內容========");
			eu2.readExcel();
			
			System.out.println("\n=======讀取源文件2.xls中的內容========");
			eu1.setStartReadPos(3);
			//eu1.setOverWrite(false);//是否覆寫目標文件(默認覆寫)
			//eu1.setComparePos(1);//設定比較哪一列內容(默認爲0,比較第一列內容)
			//eu1.setNeedCompare(false);//設定是否比較(默認值是true)。只有當不覆蓋目標文件時,設置檢查重複纔有效。
			
			eu1.writeExcel(eu1.readExcel(), "d:\\1.xls");//將讀取到的2.xls中的數據合併到1.xls中
			System.out.println("\n=======修改後,1.xls中的內容========");
			eu2.readExcel();//讀取合併後的1.xls的數據
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
       執行結果以下:


       明明有重複的,爲何提示重複爲0呢?這是由於默認對目標文件進行了覆寫,直接把源文件的數據覆蓋到目標文件中。因此只會顯示源文件的內容。.net

       若是把上段測試代碼的1三、1四、15行換成下面的內容,手動還原1.xls,再測試一下:excel

eu1.setOverWrite(false);//是否覆寫目標文件(默認覆寫)
    //eu1.setComparePos(1);//設定比較哪一列內容(默認爲0,比較第一列內容)
    //eu1.setNeedCompare(false);//設定是否比較(默認值是true)。只有當不覆蓋目標文件時,設置檢查重複纔有效。

        執行結果以下:code


       此次把覆寫給取消了,在合併的時候,默認會比較第一列的數據,存在的則再也不追加,因此1.xls中的張三保留了,李四追加到了後面。對比上面的覆蓋,能夠看到,覆寫時跟2.xls的順序一致(李四在張三前面),而不覆寫,則在文檔的最後執行追加操做。


       咱們再次修改測試代碼:

eu1.setOverWrite(false);//是否覆寫目標文件(默認覆寫)
    eu1.setComparePos(1);//設定比較哪一列內容(默認爲0,比較第一列內容)
    //eu1.setNeedCompare(false);//設定是否比較(默認值是true)。只有當不覆蓋目標文件時,設置檢查重複纔有效。

       同時手動把1.xls中的內容修改以下:


       執行結果以下:


       你們能夠看到,比較的對象變到的第二列,因爲張三所在行的第二列與添加的內容不相同,因此張三被追加到了文檔的和麪。


       最後再次修改測試代碼,而且手動還原1.xls爲原始狀態。

eu1.setOverWrite(false);//是否覆寫目標文件(默認覆寫)
    //eu1.setComparePos(1);//設定比較哪一列內容(默認爲0,比較第一列內容)
    eu1.setNeedCompare(false);//設定是否比較(默認值是true)。只有當不覆蓋目標文件時,設置檢查重複纔有效。

        執行結果以下:


       此次把覆寫和自動比較都取消了,結果就是直接在目標文件的後面進行追加操做。


       代碼有點多,你們可能看的有點累了,不過寫這個工具類消耗了我n多腦細胞,仍是但願對你們能夠有所幫助。若是有什麼問題,歡迎你們批評指正。

相關文章
相關標籤/搜索