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();
}
}
jxl 操作excle 修改 添加 追加批註 java
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.