当您想在电子表格中创建或添加指定格式的单元格时,您需要创建一个 WritableCellFormat 对象并将其作为参数传递。
第 1 步:
您可以从另一个单元格读取格式(就像下面的代码或自己创建一个新的),然后创建一个 WritableCellFormat 对象[newFormat object in below code]
第 2 步:
添加您想要的所有格式,例如背景、边框、对齐方式等,如下面的代码所示。
WritableCellFormat newFormat = null;
WritableSheet sheet = workbook.getSheet(0);
Cell readCell = sheet.getCell(column, row); //read format from another cell(if you want to copy its existing properties otherwise you can ignore).
WritableCellFormat cellFormatObj = new WritableCellFormat(
noBoldFont);
CellFormat readFormat = readCell.getCellFormat() == null ? cellFormatObj
: readCell.getCellFormat();
newFormat = new WritableCellFormat(readFormat);
newFormat.setBackground(Colour.WHITE);
newFormat.setBorder(jxl.format.Border.BOTTOM,jxl.format.BorderLineStyle.THIN);
newFormat.setAlignment(Alignment.CENTRE);
第 3 步:
当您创建新单元格(或在 excel 中添加具有特定格式的单元格)时,将格式添加为参数。
newFormat(WritableCellFormat objectT) 将是您要设置的新格式。
WritableSheet s = workbook.getSheet(0);
//column, row , where you wan the new format , note newFormat is passed as parameter.
s.addCell(new Label(column, row, request.getRuleId(), copyCellFormat(s, column,
newFormat)));
上述操作所需的进口。
import jxl.Cell;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.CellFormat;
import jxl.format.Colour;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;