今日学习总计

今天学习将数据导出为excel文件

代码尝试

public class ExpportDataBeExcel {
public void exportData(List<BeanExportData> datas){
//需要导出的excel文件的文件名
String fileName ="考情统计.xls";
//操作excel的对象
WritableWorkbook wwb = null;
try {
//根据当前的文件路径创建统计的文件并且实例化出一个操作excel的对象
wwb = Workbook.createWorkbook(new File(Environment.getExternalStorageDirectory()+"/"+fileName));
} catch (IOException e) {
e.printStackTrace();
}
if (wwb != null ){
//创建底部的选项卡 传参是选项卡的名称 和 选型卡的索引
WritableSheet writableSheet = wwb.createSheet("2017年3月7日考勤",0);
//创建excel的表头的信息
String [] topic ={"序号","姓名","年龄","日期"};
for (int i = 0 ; i<topic.length ; i++ ){
//横向的在单元格中填写数据
Label labelC = new Label(i,0,topic[i]);
try {
writableSheet.addCell(labelC);
} catch (WriteException e) {
e.printStackTrace();
}
}
//从实体中遍历数据并将数据写入excel文件中
BeanExportData account;
ArrayList<String> li;
for ( int j = 0 ; j < datas.size() ; j++ ){
//将数据源列表中的数据整合成 一个个的字符串列表
account = datas.get(j);
li = new ArrayList<>();
li.add(account.getNumber());
li.add(account.getName());
li.add(account.getAge());
li.add(account.getData());
int k = 0;
for (String l:li){
//将单个的字符串列表横向的填入到excel表中
Label labelC = new Label(k,j+1,l);
k++;
try {
writableSheet.addCell(labelC);
} catch (WriteException e) {
e.printStackTrace();
}
}
li = null;
}
}
//将文件从内存写入到文件当中
try {
wwb.write();
wwb.close();
} catch (IOException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
}
}

posted @ 2021-03-10 23:46  禁小呆  阅读(38)  评论(0)    收藏  举报