智行码上说,分享您需要的干货
前言
根据客户的需要,需要将应用的财务数据导出到 Excel 表格中导出,使用 java 库 jxl 生成 Excel
表格
Jxl 库使用
1. 导入依赖包
在 gradle 中添加
implementation 'net.sourceforge.jexcelapi:jxl:2.6.12'
也可以导入 jar 包
implementation files('libs/jxl-2.6.12.jar')
2. 设置单元格格式
设置单元格字体和字体大小、对齐方式、边框大小、背景颜色
// 设置字体和字体大小
arial10font = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD);
arial10format = new WritableCellFormat(arial10font);
// 对齐格式
arial10format.setAlignment(jxl.format.Alignment.CENTRE);
// 设置边框
arial10format.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
// 设置背景色
arial10format.setBackground(Colour.GRAY_25);
3. 初始化表格
初始化设置表格存储路径和文件名称,创建 Excel 表格并且设置 sheet、设置标题栏
WritableWorkbook workbook = null;
try {
File file = new File(context.getExternalCacheDir() + "/Export");
makeDir(file);
File saveFile = new File(file, filePath);
if (!saveFile.exists()) {
saveFile.createNewFile();
}
workbook = Workbook.createWorkbook(saveFile);
// 设置表格的 sheet 名字
WritableSheet sheet = workbook.createSheet(sheetName, 0);
// 创建标题栏
sheet.addCell(new Label(0, 0, filePath, arial14format));
评论0