// 7.数字的公式计算
Number n = new jxl.write.Number(0, 9, 4.5);// A10
sheet.addCell(n);
n = new Number(1, 9, 8);// B10
sheet.addCell(n);
NumberFormat dp3 = new NumberFormat("#.###"); // 设置单元格里面的数字格式
WritableCellFormat dp3cell = new WritableCellFormat(dp3);
dp3cell.setWrap(true);
Formula f = new Formula(2, 9, "(a10+b10)/2", dp3cell); // 设置C10公式
sheet.addCell(f);
f = new Formula(3, 9, "SUM(A10:B10)", dp3cell);// 设置D10公式
sheet.addCell(f);
// 8.设置sheet的样式
sheet.getSettings().setProtected(true); // 设置xls的保护,单元格为只读的
sheet.getSettings().setPassword("123"); // 设置xls的密码
sheet.getSettings().setDefaultColumnWidth(10); // 设置列的默认宽度,2cm左右
sheet.setRowView(3, 200);// 设置第4行高度
sheet.setRowView(2, false);// 这样可以自动把行高扩展
sheet.setColumnView(0, 300);// 设置第1列宽度,6cm左右
sheet.mergeCells(0, 5, 1, 7);// 合并单元格:合并A6B8也就是1列6行 与 2列7行之间的矩形
// 9.设置边框
drawRect(sheet, 5, 6, 7, 6, BorderLineStyle.THICK, Colour.BLACK, null);
sheet.mergeCells(1, 2, 3, 3);
wwb.write();
wwb.close();
os.close();
}
public static void drawRect(WritableSheet sheet, int x, int y, int width,
int height, BorderLineStyle style, Colour BorderColor,
Colour bgColor) throws WriteException {
for (int w = 0; w < width; w++) {
for (int h = 0; h < height; h++) {
WritableCellFormat alignStyle = new WritableCellFormat(); // 单元格样式
alignStyle.setAlignment(Alignment.CENTRE); // 设置对齐方式
alignStyle.setVerticalAlignment(VerticalAlignment.CENTRE);// 设置对齐方式
if (h == 0)// 画上
alignStyle.setBorder(Border.TOP, style, BorderColor);// 设置边框的颜色和样式
if (w == 0)// 画左
alignStyle.setBorder(Border.LEFT, style, BorderColor);// 设置边框的颜色和样式
if (w == width - 1)// 画右
alignStyle.setBorder(Border.RIGHT, style, BorderColor);// 设置边框的颜色和样式
if (h == height - 1)// 画下
alignStyle.setBorder(Border.BOTTOM, style, BorderColor);// 设置边框的颜色和样式
// drawLine(sheet, x, y, Border.BOTTOM);
if (bgColor != null)
alignStyle.setBackground(bgColor); // 背静色
Label mergelabel = new Label(x, y, "", alignStyle);
// topleftXIndex, topleftYIndex, bottomRightXIndex,
// bottomRightYIndex
// sheet.mergeCells(2, 5, 10, 10);
sheet.addCell(mergelabel);
y++;
}
y -= height;
x++;
}
}
责任编辑:小草