将应用内的数据导出为excel表格。
在app的build.gradle里面添加依赖包:
implementationgroup: 'net.sourceforge.jexcelapi', name: 'jxl', version: '2.6.12'有很多读者提到该依赖有问题,笔者推测是网络代理的问题,遇到这种问题大家可以到这个地址下载对应的jar包手动导入即可:
如果还是不能成功笔者已经写好了jar包放在了这个地址,大家可以自由下载,下载到jar包之后将其放在app/libs目录下,然后右键jar包文件,然后Add as Library即可。
packagecn.xiaojii.cashgift.util.io;
importjxl.Workbook;
importjxl.WorkbookSettings;
importjxl.format.Colour;
importjxl.write.Label;
importjxl.write.WritableCell;
importjxl.write.WritableCellFormat;
importjxl.write.WritableFont;
importjxl.write.WritableSheet;
importjxl.write.WritableWorkbook;
importjxl.write.WriteException;
/** * @author dmrfcoder * @date 2018/8/9 */publicclassExcelUtil {
privatestaticWritableFontarial14font = null;
privatestaticWritableCellFormatarial14format = null;
privatestaticWritableFontarial10font = null;
privatestaticWritableCellFormatarial10format = null;
privatestaticWritableFontarial12font = null;
privatestaticWritableCellFormatarial12format = null;
privatefinalstaticStringUTF8_ENCODING = "UTF-8";
/** * 单元格的格式设置 字体大小 颜色 对齐方式、背景颜色等... */privatestaticvoidformat() {
try {
arial14font = newWritableFont(WritableFont.ARIAL, 14, WritableFont.BOLD);
arial14font.setColour(jxl.format.Colour.LIGHT_BLUE);
arial14format = newWritableCellFormat(arial14font);
arial14format.setAlignment(jxl.format.Alignment.CENTRE);
arial14format.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
arial14format.setBackground(jxl.format.Colour.VERY_LIGHT_YELLOW);
arial10font = newWritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD);
arial10format = newWritableCellFormat(arial10font);
arial10format.setAlignment(jxl.format.Alignment.CENTRE);
arial10format.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
arial10format.setBackground(Colour.GRAY_25);
arial12font = newWritableFont(WritableFont.ARIAL, 10);
arial12format = newWritableCellFormat(arial12font);
//对齐格式arial10format.setAlignment(jxl.format.Alignment.CENTRE);
//设置边框arial12format.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
} catch (WriteExceptione) {
e.printStackTrace();
}
}
/** * 初始化Excel * * @param fileName 导出excel存放的地址(目录) * @param colName excel中包含的列名(可以有多个) */publicstaticvoidinitExcel(StringfileName, String[] colName) {
format();
WritableWorkbookworkbook = null;
try {
Filefile = newFile(fileName);
if (!file.exists()) {
file.createNewFile();
}
workbook = Workbook.createWorkbook(file);
//设置表格的名字WritableSheetsheet = workbook.createSheet("账单", 0);
//创建标题栏sheet.addCell((WritableCell) newLabel(0, 0, fileName, arial14format));
for (intcol = 0; col < colName.length; col++) {
sheet.addCell(newLabel(col, 0, colName[col], arial10format));
}
//设置行高sheet.setRowView(0, 340);
workbook.write();
} catch (Exceptione) {
e.printStackTrace();
} finally {
if (workbook != null) {
try {
workbook.close();
} catch (Exceptione) {
e.printStackTrace();
}
}
}
}
@SuppressWarnings("unchecked")
publicstatic <T> voidwriteObjListToExcel(List<T> objList, StringfileName, Contextc) {
if (objList != null && objList.size() > 0) {
WritableWorkbookwritebook = null;
InputStreamin = null;
try {
WorkbookSettingssetEncode = newWorkbookSettings();
setEncode.setEncoding(UTF8_ENCODING);
in = newFileInputStream(newFile(fileName));
Workbookworkbook = Workbook.getWorkbook(in);
writebook = Workbook.createWorkbook(newFile(fileName), workbook);
WritableSheetsheet = writebook.getSheet(0);
for (intj = 0; j < objList.size(); j++) {
ProjectBeanprojectBean = (ProjectBean) objList.get(j);
List<String> list = newArrayList<>();
list.add(projectBean.getName());
list.add(projectBean.getProject());
list.add(projectBean.getMoney());
list.add(projectBean.getYear() + " " + projectBean.getMonth()+" "+projectBean.getDay());
list.add(projectBean.getBeizhu());
for (inti = 0; i < list.size(); i++) {
sheet.addCell(newLabel(i, j + 1, list.get(i), arial12format));
if (list.get(i).length() <= 4) {
//设置列宽sheet.setColumnView(i, list.get(i).length() + 8);
} else {
//设置列宽sheet.setColumnView(i, list.get(i).length() + 5);
}
}
//设置行高sheet.setRowView(j + 1, 350);
}
writebook.write();
Toast.makeText(c, "导出Excel成功", Toast.LENGTH_SHORT).show();
} catch (Exceptione) {
e.printStackTrace();
} finally {
if (writebook != null) {
try {
writebook.close();
} catch (Exceptione) {
e.printStackTrace();
}
}
if (in != null) {
try {
in.close();
} catch (IOExceptione) {
e.printStackTrace();
}
}
}
}
}
}
StringfilePath = Environment.getExternalStorageDirectory() + "/AndroidExcelDemo";
Filefile = newFile(filePath);
if (!file.exists()) {
file.mkdirs();
}
StringexcelFileName = "/demo.xls";
String[] title = {"姓名", "年龄", "男孩"};
StringsheetName = "demoSheetName";
List<DemoBean> demoBeanList = newArrayList<>();
DemoBeandemoBean1 = newDemoBean("张三", 10, true);
DemoBeandemoBean2 = newDemoBean("小红", 12, false);
DemoBeandemoBean3 = newDemoBean("李四", 18, true);
DemoBeandemoBean4 = newDemoBean("王香", 13, false);
demoBeanList.add(demoBean1);
demoBeanList.add(demoBean2);
demoBeanList.add(demoBean3);
demoBeanList.add(demoBean4);
filePath = filePath + excelFileName;
ExcelUtil.initExcel(filePath, sheetName, title);
ExcelUtil.writeObjListToExcel(demoBeanList, filePath, context);
textView.setText("excel已导出至:" + filePath);最终生成的excel效果如下:
注意一定要处理权限申请的相关工作。

