本文介绍如何使用spire.xls for java在excel文档中冻结第一行、冻结第一列、同时冻结多行多列,以及如何解除冻结。
冻结第一行
//创建workbook对象
workbook workbook = new workbook();
//加载一个示例文档
workbook.loadfromfile("c:\\users\\administrator\\desktop\\sample.xlsx");
//获取第一个工作表
worksheet sheet = workbook.getworksheets().get(0);
//冻结第一行
sheet.freezepanes(2,1);
//保存文档
workbook.savetofile("freezefirstrow.xlsx", excelversion.version2016);
冻结首列
//创建workbook对象
workbook workbook = new workbook();
//加载一个示例文档
workbook.loadfromfile("c:\\users\\administrator\\desktop\\sample.xlsx");
//获取第一个工作表
worksheet sheet = workbook.getworksheets().get(0);
//冻结首列
sheet.freezepanes(1,2);
//保存文档
workbook.savetofile("freezefirstcolumn.xlsx", excelversion.version2016);
同时冻结多行多列
//创建workbook对象
workbook workbook = new workbook();
//加载一个示例文档
workbook.loadfromfile("c:\\users\\administrator\\desktop\\sample.xlsx");
//获取第一个工作表
worksheet sheet = workbook.getworksheets().get(0);
//冻结前两行两列
sheet.freezepanes(3,3);
//保存文档
workbook.savetofile("freezefewrowsandcolumns.xlsx", excelversion.version2016);
解除冻结
//创建workbook对象
workbook workbook = new workbook();
//加载一个示例文档
workbook.loadfromfile("c:\\users\\administrator\\desktop\\freeze.xlsx");
//获取第一个工作表
worksheet sheet = workbook.getworksheets().get(0);
//取消冻结窗口
sheet.removepanes();
//保存文档
workbook.savetofile("unfreezepanes.xlsx", excelversion.version2016);