jxl创建和设置excel
近日解决了一个jxl的问题,顺便整理了jxl的使用方法。
创建workbook(excel表格文件)
File file =
new File(filePath+ File.separator +fileName);
WritableWorkbook workbook = Workbook.createWorkbook(file);
创建sheet页(workbook中的单页)
WritableSheet sheet = workbook.createSheet(
"统计报表",
0);
通过 WritableFont对象设置单元格字体相关参数
WritableFont wfont =
new WritableFont(
WritableFont.ARIAL,
14,
WritableFont.BOLD,
false,
UnderlineStyle.NO_UNDERLINE,
jxl.format.Colour.BLACK
);
WritableCellFormat wcf =
new WritableCellFormat(wfont);
wcf.setBackground(jxl.format.Colour.IVORY);
wcf.setBorder(
jxl.format.Border.ALL,
jxl.format.BorderLineStyle.THIN,
jxl.format.Colour.BLACK
);
wcf.setVerticalAlignment(VerticalAlignment.CENTRE);
wcf.setAlignment(Alignment.CENTRE);
wcf.setWrap(
false);
创建标签对象(单元格内容及格式的载体)
Label label =
new Label(
0,
0,
contentString,
wcf
);
sheet.addCell(label);
通过 CellView对象,设置行、列的显示样式
CellView ccv =
new CellView();
ccv.setAutosize(
true);
ccv.setSize(
18);
CellView rcv =
new CellView();
rcv.setSize(
160);
rcv.setHidden(collapsed);
设置sheet页行列显示样式
sheet.setColumnView(
0,
ccv
);
sheet.setRowView(
0,
rcv
);
合并单元格
sheet.mergeCells(
0,
0,
9,
0);
设置自定义颜色
Color color = Color.decode(
"#0099cc");
workbook.setColourRGB(
Colour.ORANGE,
color.getRed(),
color.getGreen(),
color.getBlue()
);
自定义颜色可以用于WritableCellFormat对象设置单元格底色。
workbook资源关闭
workbook.write();
workbook.close();
jxl高级用法
TBD
转载请注明原文地址: https://ju.6miu.com/read-668900.html