项目中需要将未上传的数据导出到excel,供用户可见。不同的模块传入数据类型不一样。所以用了List<ArrayList>将数据封装成二维string类型。效率还挺高
build.gradle中引入jxl库。
// https://mvnrepository.com/artifact/net.sourceforge.jexcelapi/jxl
implementation 'net.sourceforge.jexcelapi:jxl:2.6.12'
先上效果图在这里插入图片描述
工具类如下
/**
* 创建时间:2020-12-25
* 编写人:kanghb
* 功能描述:将数据导出excel 经测试4万条学生数据一秒左右完成
*/
public class ExcelUtil {
private static WritableFont arial14font = null;
private static WritableCellFormat arial14format = null;
private static WritableFont arial10font = null;
private static WritableCellFormat arial10format = null;
private static WritableFont arial12font = null;
private static WritableCellFormat arial12format = null;
private final static String UTF8_ENCODING = "UTF-8";
/**
* 单元格的格式设置 字体大小 颜色 对齐方式、背景颜色等...
*/
private static void format() {
try {
arial14font = new WritableFont(WritableFont.ARIAL, 14, WritableFont.BOLD);
arial14font.setColour(jxl.format.Colour.LIGHT_BLUE);
arial14format = new WritableCellFormat(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 = 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);
arial12font = new WritableFont(WritableFont.ARIAL, 12);
arial12format = new WritableCellFormat(arial12font);
//对齐格式
arial12format.setAlignment(jxl.format.Alignment.CENTRE);
//设置边框
arial12format.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
} catch (WriteException e) {
e.printStackTrace();
}
}
/**
* 初始化Excel表格
*
* @param filePath 存放excel文件的路径(MPCMS/Export/demo.xls)
* @param sheetName Excel表格的表名
* @param colName excel中包含的列名(可以有多个)
*/
public static void initExcel(String filePath, String sheetName, String[] colName) {
format();
WritableWorkbook workbook = null;
try {
File file = new File(FileUtilsCore.SDPATH + "/Export");
makeDir(file);
File saveFile = new File(file, filePath);
if (!saveFile.exists()) {
saveFile.createNewFile();
}
workbook = Workbook.createWorkbook(saveFile);
//设置表格的名字
WritableSheet sheet = workbook.createSheet(sheetName, 0);
//创建标题栏
sheet.addCell((WritableCell) new Label(0, 0, filePath, arial14format));
for (int col = 0; col < colName.length; col++) {
sheet.addCell(new Label(col, 0, colName[col], arial10format));
}
//设置行高
sheet.setRowView(0, 340);
workbook.write();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (workbook != null) {
try {
workbook.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
* 将制定类型的List写入Excel中
*
* @param objList 待写入的list,
* @param fileName 文件名:用户名_模块_任务名_时间
* @param c
*/
@SuppressWarnings("unchecked")
public static void writeObjListToExcel(List<ArrayList<String>> objList, String fileName, Context c) {
if (objList != null && objList.size() > 0) {
WritableWorkbook writebook = null;
InputStream in = null;
try {
WorkbookSettings setEncode = new WorkbookSettings();
setEncode.setEncoding(UTF8_ENCODING);
File file = new File(FileUtilsCore.SDPATH + "/Export");
makeDir(file);
File saveFile = new File(file, fileName);
if (!saveFile.exists()) {
saveFile.createNewFile();
}
in = new FileInputStream(saveFile);
Workbook workbook = Workbook.getWorkbook(in);
writebook = Workbook.createWorkbook(saveFile, workbook);
WritableSheet sheet = writebook.getSheet(0);
for (int j = 0; j < objList.size(); j++) {
ArrayList<String> list = (ArrayList<String>) objList.get(j);
for (int i = 0; i < list.size(); i++) {
sheet.addCell(new Label(i, j + 1, list.get(i), arial12format));
if (list.get(i).length() <= 5) {
//设置列宽
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 (Exception e) {
e.printStackTrace();
} finally {
if (writebook != null) {
try {
writebook.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
public static void makeDir(File dir) {
if (!dir.getParentFile().exists()) {
makeDir(dir.getParentFile());
}
dir.mkdir();
}
}
调用代码
String[] title = {"母本名称", "母本小区", "父本名称", "父本小区", "母本珠号", "父本珠号", "授粉数量", "单位", "时间"};
String fileName = App.getInstance().getUserName() + "_" + "授粉" + "_" + crossTaskName + "_" + System.currentTimeMillis() + ".xls";
ExcelUtil.initExcel(fileName, "授粉记录", title);
ExcelUtil.writeObjListToExcel(recordList, fileName, this);