将制定类型的List写入Excel中
@param objList 待写入的list
* @param fileName
* @param c
* @param <T>
public static <T> void writeObjListToExcel(List<T> objList, String fileName, Context c) {
System.err.println(fileName);
int flag=1;
String name="雷锟";
if (objList != null && objList.size() > 0) {
WritableWorkbook writebook = null;
InputStream in = null;
try {
WorkbookSettings setEncode = new WorkbookSettings();
setEncode.setEncoding(UTF8_ENCODING);
in = new FileInputStream(new File(fileName));
Workbook workbook = Workbook.getWorkbook(in);
writebook = Workbook.createWorkbook(new File(fileName), workbook);
WritableSheet sheet = writebook.getSheet(0);
System.err.println(objList.size());
for (int j = 5; j < (objList.size()+5); j++) {
System.err.println("99999999999999999");
people people = (people) objList.get(j-5);
List<String> list = new ArrayList<>();
/*list.add(demoBean.getName());
list.add(String.valueOf(demoBean.getId()));
list.add(String.valueOf(demoBean.getBanji()));
list.add(String.valueOf(demoBean.getWendu()));
list.add(String.valueOf(demoBean.getData()));
list.add(String.valueOf(demoBean.getSpace()));
*/
list.add(String.valueOf(people.getTime()));
list.add(String.valueOf(people.getPrice()));
list.add("正常");
list.add(String.valueOf(MyLocationListener.locat));
list.add(String.valueOf(people.getSituation()));
for (int i = 0; i < list.size(); i++) {
sheet.addCell(new Label(i, j + 1, list.get(i), arial12format));
if(i+1==5){
sheet.addCell(new Label(i+1,j+1,list.get(i)));
}
if(flag==1){
sheet.addCell(new Label(1,1,"信1905-2"));
sheet.addCell(new Label(1,2,name));
sheet.addCell(new Label(1,3,"良好"));
sheet.addCell(new Label(5,1,MainActivity.timenum1));
sheet.addCell(new Label(5,2,"20193981"));
sheet.addCell(new Label(5,3,"15095948187"));
flag++;}
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();
workbook.close();
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();
}
}
}
}
}