1. 程式人生 > >jxl 操作excle 修改 新增 追加批註 java

jxl 操作excle 修改 新增 追加批註 java

import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import jxl.Sheet;
import jxl.Workbook;
import jxl.format.CellFormat;
import jxl.write.Label;
import jxl.write.WritableCell;
import jxl.write.WritableCellFeatures;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class ExcelTools {
		
	/**
	 * 
	 * 設定excel修改批註資訊
	 * 
	 * @param File file 檔案
	 * @param Workbook book 
	 * @param int col 列座標
	 * @param int row 行座標
	 * @param String text 批註資訊
	 * @return boolean true|false
	 */
	public synchronized static boolean updateCmment(File file, Workbook book, int col, int row, String text) {
		WritableWorkbook wbe = null;
		WritableSheet sheet = null;
		WritableCell cell = null;
		int height;
		try {
			wbe = Workbook.createWorkbook(file, book);						//建立workbook的副本  
			sheet = wbe.getSheet(0); 				 						//獲取第一個sheet  
	        cell =sheet.getWritableCell(col, row);       					//獲取第一個單元格  
	        WritableCellFeatures cellFeatures = new WritableCellFeatures(); //建立空的features
			if(null != cell.getCellFeatures()){
				cell.getCellFeatures().removeComment();
			}else{
				cell.setCellFeatures(cellFeatures);
			}
			height = getCommentRows(text, "\r\n");
			cellFeatures.setComment(text, 3, height+2);
			cell.setCellFeatures(cellFeatures);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}finally{
			try {
				wbe.write();												//將修改儲存到workbook
				wbe.close();												//關閉workbook,釋放記憶體
			} catch (Exception e) {
				e.printStackTrace();
			}														    												
		}
	}
	/**
	 * 
	 * 設定excel追加批註資訊,保留原來批註
	 * 
	 * @param File file 檔案
	 * @param Workbook book 
	 * @param int col 列座標
	 * @param int row 行座標
	 * @param String text 批註資訊
	 * @return boolean true|false
	 */
	public synchronized static boolean addCmment(File file, Workbook book, int col, int row, String text) {
		WritableWorkbook wbe = null;
		WritableSheet sheet = null;
		WritableCell cell = null;
		String oldmark = null;
		int height;
		try {
			wbe = Workbook.createWorkbook(file, book);						//建立workbook的副本  
			sheet = wbe.getSheet(0); 				 						//獲取第一個sheet  
	        cell =sheet.getWritableCell(col, row);       					//獲取第一個單元格  
	        WritableCellFeatures cellFeatures = new WritableCellFeatures(); //建立空的features
			oldmark = cell.getCellFeatures() == null ? null : cell.getCellFeatures().getComment();//原批註
			if(null != oldmark){
				cell.getCellFeatures().removeComment();
				text = oldmark + "\r\n---------------\r\n" + text;
			}else{
				cell.setCellFeatures(cellFeatures);
			}
			height = getCommentRows(text, "\r\n");
			cellFeatures.setComment(text, 3, height+3);
			cell.setCellFeatures(cellFeatures);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}finally{
			try {
				wbe.write();												//將修改儲存到workbook
				wbe.close();												//關閉workbook,釋放記憶體
			} catch (Exception e) {
				e.printStackTrace();
			}														    												
		}
	}
	/**
	 * 
	 * 設定刪除批註資訊
	 * 
	 * @param File file 檔案
	 * @param Workbook book 
	 * @param int col 列座標
	 * @param int row 行座標
	 * @return boolean true|false
	 */
	public synchronized static boolean removeCmment(File file, Workbook book, int col, int row) {
		WritableWorkbook wbe = null;
		WritableSheet sheet = null;
		WritableCell cell = null;
		try {
			wbe = Workbook.createWorkbook(file, book);						//建立workbook的副本  
			sheet = wbe.getSheet(0); 				 						//獲取第一個sheet  
	        cell =sheet.getWritableCell(col, row);       					//獲取第一個單元格  
	        WritableCellFeatures cellFeatures = new WritableCellFeatures(); //建立空的features
			if(null != cell.getCellFeatures()){
				cell.getCellFeatures().removeComment();
			}
			cell.setCellFeatures(cellFeatures);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}finally{
			try {
				wbe.write();												//將修改儲存到workbook
				wbe.close();												//關閉workbook,釋放記憶體
			} catch (Exception e) {
				e.printStackTrace();
			}														    												
		}
	}
	public static int getCommentRows(String str, String findstr){
		String regEx = findstr; //要匹配的子串,可以用正則表示式 
		Pattern p = Pattern.compile(regEx); 
		Matcher m = p.matcher(str); 
		int i = 0;
		while(m.find()) {
			i++;
		}
		return i;
	}
	/**
	 * 測試
	 */
	public static void main(String[] args) throws Exception {
		//新增批註start
		File file =  new File("D://001311600.xls");
		//File file =  new File("D://3333.xls");
		Workbook book = Workbook.getWorkbook(file);
		Sheet sheet = book.getSheet(0);
		System.out.println(sheet.getRow(0)[0].getContents());//列印座標 0,0 文字資訊
		
		ExcelTools.addCmment(file, book, 0, 0, "測試787123456789012");
		//ExcelTools.updateCmment(file, book, 0, 0, "測試787123456789012\r\n");
		//ExcelTools.removeCmment(file, book, 0, 0);
		
        book.close();
        
	}
}