今日总结

2021年3月9日:

今天写了将数据导进excel的代码,具体如下:

首先是权限申请:

if (ContextCompat.checkSelfPermission(Tijiao.this,Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(Tijiao.this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST);
}
else
{
printer();
}
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == MY_PERMISSIONS_REQUEST) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
printer();
} else {
Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show();
}
return;
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
保存文件:
public void saveFile(String fileName, Context context,int rawid) throws IOException {
// 首先判断该目录下的文件夹是否存在
File dir = new File(Environment.getExternalStorageDirectory().getPath() + "/inspection/");
if (!dir.exists()) {
// 文件夹不存在 , 则创建文件夹
dir.mkdirs();
}
// 判断目标文件是否存在
File file1 = new File(Environment.getExternalStorageDirectory().getPath() + "/inspection/"+fileName);
if(!file1.exists()){
try {
file1.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
InputStream input = context.getResources().openRawResource(rawid); // 获取资源文件raw
try {
FileOutputStream out = new FileOutputStream(file1); // 文件输出流、用于将文件写到SD卡中
// -- 从内存出去
byte[] buffer = new byte[1024];
int len = 0;
while ((len = (input.read(buffer))) != -1) { // 读取文件,-- 进到内存
out.write(buffer, 0, len); // 写入数据 ,-- 从内存出
}
input.close();
out.close(); // 关闭流
} catch (Exception e) {
e.printStackTrace();
}
}
private void printer() {
try {
Cursor cursor = db.rawQuery("select id,app.name,banji,tiwen,date,address,phone,special from student left join app on app.name=student.name", null);
if (cursor.moveToFirst()) {
students = new ArrayList<>();
do {
studenttiwen student = new studenttiwen();
student.id = cursor.getString(cursor.getColumnIndex("id"));
student.name = cursor.getString(cursor.getColumnIndex("name"));
student.banji = cursor.getString(cursor.getColumnIndex("banji"));
student.tiwen = cursor.getString(cursor.getColumnIndex("tiwen"));
student.date = cursor.getString(cursor.getColumnIndex("date"));
student.address = cursor.getString(cursor.getColumnIndex("address"));
student.phone = cursor.getString(cursor.getColumnIndex("phone"));
student.special = cursor.getString(cursor.getColumnIndex("special"));
students.add(student);
} while (cursor.moveToNext());
}
cursor.close();
List<studenttiwen> students1 = new ArrayList<>();
List<studenttiwen> students2 = new ArrayList<>();
for (studenttiwen stuall : students) {
if (stuall.banji.equals("信1905-1")) {
students1.add(stuall);
} else {
students2.add(stuall);
}
}
try {
saveFile("radiate1.xls", this, R.raw.radiate1);//文件目录res/raw
} catch (IOException e) {
e.printStackTrace();
}
String aafileurl = Environment.getExternalStorageDirectory().getPath() + "/inspection/radiate1.xls";
String bbfileurl = Environment.getExternalStorageDirectory().getPath() + "/inspection/信1905-2学生个人登记表.xls";
String ccfileurl = Environment.getExternalStorageDirectory().getPath() + "/inspection/信1905-1学生个人登记表.xls";
Workbook rwb = null;
try {
rwb = Workbook.getWorkbook(new File(aafileurl));
} catch (IOException e) {
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
}
WritableWorkbook wwb = null;// copy
try {
wwb = Workbook.createWorkbook(new File(bbfileurl), rwb);
} catch (IOException e) {
e.printStackTrace();
}
WritableWorkbook wwa = null;
try {
wwa = Workbook.createWorkbook(new File(ccfileurl), rwb);
} catch (IOException e) {
e.printStackTrace();
}
WritableSheet sheet = wwb.getSheet(0);
WritableSheet sheet1 = wwa.getSheet(0);
if(students1!=null){
WritableCell cell1 = sheet1.getWritableCell(1, 1);
WritableCell cell3 = sheet1.getWritableCell(1, 2);
WritableCell cell4 = sheet1.getWritableCell(3, 2);
WritableCell cell6 = sheet1.getWritableCell(3, 3);
Label label1 = (Label) cell1;
Label label3 = (Label) cell3;
Label label4 = (Label) cell4;
Label label6 = (Label) cell6;
label1.setString(students.get(0).banji);
label3.setString(students.get(0).name);
label4.setString(students.get(0).id);
label6.setString(students.get(0).phone);
for (studenttiwen stu : students) {
WritableCell cell2 = sheet1.getWritableCell(5, 1);
WritableCell cell5 = sheet1.getWritableCell(1, 3);
WritableCell a = sheet1.getWritableCell(1, startRowIndex);
WritableCell b = sheet1.getWritableCell(2, startRowIndex);
WritableCell c = sheet1.getWritableCell(3, startRowIndex);
WritableCell d = sheet1.getWritableCell(4, startRowIndex);
Label label2 = (Label) cell2;
Label label5 = (Label) cell5;
Label a1 = (Label) a;
Label b1 = (Label) b;
Label c1 = (Label) c;
Label d1 = (Label) d;
label2.setString(stu.date);
if (stu.special.equals("无")) {
label5.setString("正常");
b1.setString("正常");
} else {
label5.setString("有异常");
b1.setString("有异常");
}
a1.setString(stu.tiwen);
c1.setString(stu.address);
d1.setString(stu.special);
startRowIndex++;
}
try {
wwb.write();
} catch (IOException e) {
e.printStackTrace();
}
try {
wwb.close();
} catch (IOException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
rwb.close();
}
if (students2 != null) {
WritableCell cell1 = sheet.getWritableCell(1, 1);
WritableCell cell3 = sheet.getWritableCell(1, 2);
WritableCell cell4 = sheet.getWritableCell(3, 2);
WritableCell cell6 = sheet.getWritableCell(3, 3);
Label label1 = (Label) cell1;
Label label3 = (Label) cell3;
Label label4 = (Label) cell4;
Label label6 = (Label) cell6;
label1.setString(students.get(0).banji);
label3.setString(students.get(0).name);
label4.setString(students.get(0).id);
label6.setString(students.get(0).phone);
for (studenttiwen stu : students) {
WritableCell cell2 = sheet.getWritableCell(5, 1);
WritableCell cell5 = sheet.getWritableCell(1, 3);
WritableCell a = sheet.getWritableCell(1, startRowIndex);
WritableCell b = sheet.getWritableCell(2, startRowIndex);
WritableCell c = sheet.getWritableCell(3, startRowIndex);
WritableCell d = sheet.getWritableCell(4, startRowIndex);
Label label2 = (Label) cell2;
Label label5 = (Label) cell5;
Label a1 = (Label) a;
Label b1 = (Label) b;
Label c1 = (Label) c;
Label d1 = (Label) d;
label2.setString(stu.date);
if (stu.special.equals("无")) {
label5.setString("正常");
b1.setString("正常");
} else {
label5.setString("有异常");
b1.setString("有异常");
}
a1.setString(stu.tiwen);
c1.setString(stu.address);
d1.setString(stu.special);
startRowIndex++;
}
try {
wwb.write();
} catch (IOException e) {
e.printStackTrace();
}
try {
wwb.close();
} catch (IOException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
rwb.close();
}
} finally {
}
}
上一篇:oracle批量入库解决方案


下一篇:关于JDBC实例