码迷,mamicode.com
首页 > 其他好文 > 详细

团队博客4

时间:2021-05-24 07:16:57      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:directory   zoom   心得   nbsp   package   sap   就是   size   form   

之前在做javaIO操作的时候,稍微也看了一点关于jxl.jar操作Excel的知识——当然说实话也就是知道有这么个东西而已的程度,实际上也确实是第一次用,所以发生了一些令人意想不到的错误也在情理之中……

先上代码(我将整个表的建立写成了一整个函数,其实分开更好理解,不过我还是有点懒惰了……):

技术图片
package com.example.helloworld;

import android.content.Context;
import android.content.ContextWrapper;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.RequiresApi;

import java.io.File;
import java.util.ArrayList;

import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import static android.content.Context.MODE_PRIVATE;

public class Excel {

    private static final String TAG = "SEL";

    @RequiresApi(api = Build.VERSION_CODES.N)
    public void excelCreate(String name, Context con) {

//获取待填写的数据 SharedPreferences sp = con.getSharedPreferences("temper", MODE_PRIVATE); String code=sp.getString("code","未设定"); String cla=sp.getString("class","未设定"); String phone=sp.getString("phone","未设定"); String safe="良好"; dao poi=new dao(con); ArrayList<info> pop=poi.serme(code); try {
//按路径创建xls ContextWrapper cw = new ContextWrapper(con); File directory = cw.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS); File file = new File(directory, name + "体温报表.xls"); Toast.makeText(con, "文件储存路径为" + file.getAbsolutePath(), Toast.LENGTH_SHORT).show(); Log.d(TAG, file.getAbsolutePath()); WritableWorkbook wwb = Workbook.createWorkbook(file); WritableSheet sheet = wwb.createSheet("sheet1", 0);
//文字对齐格式 WritableCellFormat headerFormat = new WritableCellFormat(); //水平居中对齐 headerFormat.setAlignment(Alignment.CENTRE); headerFormat.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN); //竖直方向居中对齐 headerFormat.setVerticalAlignment(VerticalAlignment.CENTRE); sheet.mergeCells(0, 0, 6, 1); //合并单元格 Label label = new Label(0, 0, "学生体温信息登记表", headerFormat); //插入数据 sheet.addCell(label); label = new Label(0, 2, "班级", headerFormat); sheet.addCell(label); sheet.mergeCells(1, 2, 3, 2); label = new Label(1, 2, cla, headerFormat); sheet.addCell(label); label = new Label(4, 2, "填表日期", headerFormat); sheet.addCell(label); sheet.mergeCells(5, 2, 6, 2); label = new Label(5, 2, poi.getTime(), headerFormat); sheet.addCell(label); label = new Label(0, 3, "姓名", headerFormat); sheet.addCell(label); sheet.mergeCells(1, 3, 3, 3); label = new Label(1, 3, name, headerFormat); sheet.addCell(label); label = new Label(4, 3, "学号", headerFormat); sheet.addCell(label); sheet.mergeCells(5, 3, 6, 3); label = new Label(5, 3, code, headerFormat); sheet.addCell(label); label = new Label(0, 4, "健康状况", headerFormat); sheet.addCell(label); sheet.mergeCells(1, 4, 3, 4); label = new Label(1, 4, "良好", headerFormat); sheet.addCell(label); label = new Label(4, 4, "手机号", headerFormat); sheet.addCell(label); sheet.mergeCells(5, 4, 6, 4); label = new Label(5, 4, phone, headerFormat); sheet.addCell(label); sheet.mergeCells(0, 5, 6, 6); label = new Label(0, 5, "每日体温状况监测", headerFormat); sheet.addCell(label); label = new Label(0, 7, "日期", headerFormat); sheet.addCell(label); label = new Label(1, 7, "体温", headerFormat); sheet.addCell(label); label = new Label(2, 7, "健康状况", headerFormat); sheet.addCell(label); sheet.mergeCells(3, 7, 4, 7); sheet.mergeCells(5, 7, 6, 7); label = new Label(3, 7, "填报时位置", headerFormat); sheet.addCell(label); label = new Label(5, 7, "备注", headerFormat); sheet.addCell(label); for (int i = 8; i <= 21; i++) { sheet.mergeCells(3, i, 4, i); sheet.mergeCells(5, i, 6, i); if((i-8)<pop.size()){ label = new Label(0, i, pop.get(i-8).getTime(), headerFormat); sheet.addCell(label); label = new Label(1, i, pop.get(i-8).getTem(), headerFormat); sheet.addCell(label); label = new Label(2, i, "良好", headerFormat); sheet.addCell(label); label = new Label(3, i, pop.get(i-8).getPlace(), headerFormat); sheet.addCell(label); label = new Label(5, i, pop.get(i-8).getSide(), headerFormat); sheet.addCell(label); } } sheet.mergeCells(0, 22, 6, 23); label = new Label(0, 22, " 本人承诺:自觉履行疫情防控责任和义务,保证以上填报信息全部属实,如有隐瞒,自愿承担相应法律后果。"); sheet.addCell(label); sheet.mergeCells(0, 24, 1, 25); label = new Label(0, 24, "本人签字", headerFormat); sheet.addCell(label); sheet.mergeCells(2, 24, 3, 25); sheet.mergeCells(4, 24, 4, 25); label = new Label(4, 24, "签字日期", headerFormat); sheet.addCell(label); sheet.mergeCells(5, 24, 6, 25); label = new Label(5, 24, poi.getsTime(), headerFormat); sheet.addCell(label); wwb.write(); wwb.close(); } catch (Exception e) { e.printStackTrace(); } } }
技术图片

虽然看上去繁琐,但实际上就是不断地合并单元格插入数据,实际上花不了多久就能理顺思路

但真正恶心的是Android中的文件操作,恶心得要死

本来我的设想是直接把表输出到根目录(storage/emulated/0/)下,但无论怎么尝试都会报错,开始为还以为是代码本身的问题,改了n遍后才发现Android 7.0以后不能随便操作根目录……

所以就改到了文件包本身的路径了

最后贴一下运行效果:

技术图片

 

技术图片

团队博客4

标签:directory   zoom   心得   nbsp   package   sap   就是   size   form   

原文地址:https://www.cnblogs.com/ldy2396/p/14762639.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!