本文将介绍如何使用spire.xls for java在excel文档中合并和取消合并单元格。
合并单元格
import com.spire.xls.fileformat;
import com.spire.xls.workbook;
import com.spire.xls.worksheet;
public class mergecells {
public static void main(string[] args){
//创建workbook实例
workbook workbook = new workbook();
//加载excel文档
workbook.loadfromfile("test1.xlsx");
//获取第一个工作表
worksheet sheet = workbook.getworksheets().get(0);
//合并单元格范围a1到c1
sheet.getrange().get("a1:c1").merge();
//保存结果文档
workbook.savetofile("mergecells.xlsx", fileformat.version2013);
}
}
取消合并单元格
import com.spire.xls.fileformat;
import com.spire.xls.workbook;
import com.spire.xls.worksheet;
public class unmergecells {
public static void main(string[] args){
//创建workbook实例
workbook workbook = new workbook();
//加载excel文档
workbook.loadfromfile("mergecells.xlsx");
//获取第一个工作表
worksheet sheet = workbook.getworksheets().get(0);
//取消合并单元格范围a1到c1
sheet.getrange().get("a1:c1").unmerge();
//保存结果文档
workbook.savetofile("unmergecells.xlsx", fileformat.version2013);
}
}