在开源世界中,jExcelAPI对中文支持非常好,API是纯Java的, 并不依赖Windows系统,即使运行在Linux下,它同样能够正确的处理Excel文件。
一、各个功能点介绍
1、创建一个Excel文件
//创建关联磁盘文件
File excel = new File("D:/testIO/" + "表文档.xls");
//创建一个excel
WritableWorkbook workbook = Workbook.createWorkbook(excel);
2.1、文字设置–WritableFont
WritableFont bold = new WritableFont(WritableFont.createFont("微软雅黑"), 12, WritableFont.BOLD);
WritableFont noBold = new WritableFont(WritableFont.createFont("微软雅黑"), 12, WritableFont.NO_BOLD);
2.2、设置正文内容样式–WritableCellFormat
// 设置正文,单元格样式控制对象
WritableCellFormat textFormat = new WritableCellFormat(noBold);
// 单元格中的内容水平方向居中、垂直方向居中、设置边框
textFormat.setAlignment(Alignment.CENTRE);
textFormat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
textFormat.setBorder(Border.ALL,BorderLineStyle.THIN);
2.3设置行高
// 设置行高,第一行,设置500高度;
sheet.setRowView(0,500);
2.4、对数据进行分组
sheet.setRowGroup(0, 2, true);
sheet.setRowGroup(3, 5, true);
sheet.setRowGroup(6, 8, true);
二、构建表头数据
创建表数据
//参数依次表示:(0列,0行,填充数据:”表名称“,以titleFormate定义的样式创建)并加入工作表中
Label label_00 = new Label(0,0, "表名称", titleFormate);
sheet.addCell(label_00);
//参数依次表示:(1列,0行,填充数据:”表名称“,以titleFormate定义的样式创建)并加入工作表中
Label label_00 = new Label(0,0, "表名称", titleFormate);
sheet.addCell(label_00);
合并的单元格
sheet.mergeCells(0,0,1,0);
构建数据
此时一般是for循环构造
for (int i = 0; i < 9; i++, startRow++) {
Label label_02 = new Label(0, startRow, "属性名称", textFormat);
sheet.addCell(label_02);
}