本文将介绍如何使用spire.xls for java给excel单元格设置边框。
原excel文档:
import com.spire.xls.*;
import java.awt.*;
public class addborders {
public static void main(string[] args){
//创建workbook实例
workbook workbook = new workbook();
//加载excel文档
workbook.loadfromfile("sample-setborder.xlsx");
//获取第一个工作表
worksheet sheet = workbook.getworksheets().get(0);
//获取需要设置边框的单元格范围
cellrange cr = sheet.getcellrange(sheet.getfirstrow(), sheet.getfirstcolumn(), sheet.getlastrow(), sheet.getlastcolumn());
//设置边框样式
cr.getborders().setlinestyle(linestyletype.double);
cr.getborders().getbyborderslinetype(borderslinetype.diagonaldown).setlinestyle(linestyletype.none);
cr.getborders().getbyborderslinetype(borderslinetype.diagonalup).setlinestyle(linestyletype.none);
cr.getborders().setcolor(color.blue);
//保存文档
workbook.savetofile("setborder.xlsx", excelversion.version2010);
}
}
结果文档: