zoukankan      html  css  js  c++  java
  • 个人作业阶段二 6

    将系统数据导出至,excel表格

    package com.example.myapplication;
    
    import android.content.Context;
    import android.os.Build;
    import android.os.Bundle;
    import android.os.Environment;
    import android.view.View;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import org.litepal.LitePal;
    
    import java.io.File;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    
    import jxl.Workbook;
    import jxl.write.Label;
    import jxl.write.WritableCellFormat;
    import jxl.write.WritableFont;
    import jxl.write.WritableSheet;
    import jxl.write.WritableWorkbook;
    import jxl.write.WriteException;
    
    import static com.example.myapplication.RegisterActivity.username;
    import static com.example.myapplication.RegisterActivity.userphone;
    import static com.example.myapplication.RegisterActivity.userxuehao;
    
    public class ExcelActivity extends AppCompatActivity {
    
        public static WritableFont arial12font = null;
        public static WritableCellFormat arial12format = null;
        public static WritableFont arial12font_ = null;
        public static WritableCellFormat arial12format_ = null;
        public static WritableFont arial12font_1 = null;
        public static WritableCellFormat arial12format_1 = null;
        public static WritableFont arial18font = null;
        public static WritableCellFormat arial18format = null;
        public final static String UTF8_ENCODING = "UTF-8";
        public final static String GBK_ENCODING = "GBK";
    
        private ArrayList<ArrayList<String>> recordList;
        private String excelname;
        private TextView tv_pass;
        private String excelxuehao;
        private String excelphone;
        private String exceldanwei = "石家庄铁道大学";
        private String exceldate;
        private static String[] title = {"日期", "每日体温", "健康状况", "当日所在地", "备注"};
        private File file;
        private String fileName;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_excel);
    
            tv_pass = findViewById(R.id.out_path);
    
            //填报日期
            SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
            Date date1 = new Date(System.currentTimeMillis());
            exceldate = simpleDateFormat1.format(date1);
    
            //姓名
            excelname = username;
    
            //学号
            excelxuehao = userxuehao;
    
    
            //手机号
            excelphone = userphone;
    
    
    
        }
    
        /**
         * 导出excel
         * @param view
         */
    
        public void exportExcel(View view) {
            file = new File(getSDPath(this) + "/Record");
            makeDir(file);
            initExcel(file.toString() + "/体温登记表.xls");
            fileName = getSDPath(this) + "/Record/体温登记表.xls";
            tv_pass.setText("Excel表格已导出至" + fileName);
        }
    
        public static void format() {
            try {
                arial12font = new WritableFont(WritableFont.createFont("宋体"), 12);
                arial12format = new WritableCellFormat(arial12font);
                arial12format.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
    
                arial12font_ = new WritableFont(WritableFont.createFont("楷体"), 12, WritableFont.BOLD);
                arial12format_ = new WritableCellFormat(arial12font_);
                arial12format_.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
    
                arial12font_1 = new WritableFont(WritableFont.createFont("黑体"), 12);
                arial12format_1 = new WritableCellFormat(arial12font_1);
                arial12format_1.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
                arial12format_1.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
                arial12format_1.setWrap(true);
    
                arial18font = new WritableFont(WritableFont.createFont("宋体"), 18, WritableFont.BOLD);
                arial18format = new WritableCellFormat(arial18font);
                arial18format.setAlignment(jxl.format.Alignment.CENTRE);//对齐格式
                arial18format.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN); //设置边框
            } catch (WriteException e) {
                e.printStackTrace();
            }
    
        }
    
        public void initExcel(String fileName) {
            format();
            WritableWorkbook workbook=null;
    
            try {
                File file = new File(fileName);
                if (!file.exists()) {
                    file.createNewFile();
                }
                workbook = Workbook.createWorkbook(file);
                WritableSheet sheet = workbook.createSheet("体温登记表", 0);
                sheet.mergeCells(0, 0, 6, 0);
    
                Label header = new Label(0, 0, "学生14天健康情况登记表", arial18format);
                sheet.addCell(header);
    
                sheet.mergeCells(0, 1, 3, 1);
                sheet.addCell(new Label(0, 1, "单位名称:" + exceldanwei, arial12format_));
    
                sheet.mergeCells(4, 1, 6, 1);
                sheet.addCell(new Label(4, 1, "填表日期:" + exceldate, arial12format_));
    
                sheet.addCell(new Label(0, 2, "姓名", arial12format));
                sheet.addCell(new Label(1, 2, excelname, arial12format));
    
                sheet.mergeCells(1, 2, 3, 2);
                sheet.addCell(new Label(4, 2, "学号:", arial12format));
                sheet.addCell(new Label(5, 2, excelxuehao, arial12format));
    
                sheet.mergeCells(5, 2, 6, 2);
                sheet.addCell(new Label(0, 3, "目前健康状况:", arial12format));
                sheet.mergeCells(1, 3, 3, 3);
                sheet.addCell(new Label(1, 3, "健康", arial12format));
    
                sheet.addCell(new Label(4, 3, "手机号", arial12format));
                sheet.addCell(new Label(5, 3, excelphone, arial12format));
                sheet.mergeCells(5, 3, 6, 3);
                sheet.mergeCells(0, 4, 6, 4);
                sheet.addCell(new Label(0, 4, "每日体温、健康状况监测(周期14天)", arial18format));
                String[] colName = {"日期", "每日体温℃", "健康状况", "当日所在地", "备注"};
                sheet.mergeCells(3, 5, 4, 5);
                sheet.mergeCells(5, 5, 6, 5);
    
                for (int col = 0; col < 3; col++) {
                    sheet.addCell(new Label(col, 5, colName[col], arial12format));
                }
                sheet.addCell(new Label(3, 5, colName[3], arial12format));
                sheet.addCell(new Label(5, 5, colName[4], arial12format));
    
                int j =6;
    
                List<Infomation> infomations = LitePal.where("zhu_name=?", excelname).find(Infomation.class);
                for(Infomation infomation:infomations) {
    
                        String ZK = "健康";
                        String DATE = infomation.getZhu_date().toString();
                        String ADD = infomation.getZhu_add().toString();
                        String BEIZHU = infomation.getZhu_shuoming().toString();
                        String TIWEN = infomation.getZhu_tiwen().toString();
    
                        sheet.addCell(new Label(0, j, DATE, arial12format));
                        sheet.addCell(new Label(1, j, TIWEN, arial12format));
                        sheet.addCell(new Label(2, j, ZK, arial12format));
    
                        sheet.mergeCells(3, j, 4, j);
                        sheet.addCell(new Label(3, j, ADD, arial12format));
                        sheet.mergeCells(5, j, 6, j);
                        sheet.addCell(new Label(5, j,BEIZHU, arial12format));
                        j++;
                }
    
                sheet.mergeCells(0,20,6,23);
                sheet.addCell(new Label(0,20,"本人承诺:自觉履行疫情防控责任和义务,保证以上填报信息全部属实,如有隐瞒,自愿承担相应法律后果。",arial12format_1));
                sheet.mergeCells(0,24,3,24);
                sheet.addCell(new Label(0,24,"本人签字:",arial12format));
                sheet.mergeCells(4,24,6,24);
                sheet.addCell(new Label(4,24,"签字日期:",arial12format));
                //sheet.mergeCells(5,24,6,24);
                workbook.write();
                workbook.close();
                Toast.makeText(ExcelActivity.this, "生成成功", Toast.LENGTH_SHORT).show();
            }catch (Exception e) {
                e.printStackTrace();
            }finally {
                if (workbook != null) {
                    try {
                        workbook.close();
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }
    
        /**
         * 将数据集合,转化成ArrayList<ArrayList<String>></String>
         * @param context
         * @return
         */
        public String getSDPath(Context context) {
            File sdDir = null;
            boolean sdCardExist = Environment.getExternalStorageState().equals(
                    Environment.MEDIA_MOUNTED);// 判断sd卡是否存在
    
            if (sdCardExist) {
                if (Build.VERSION.SDK_INT>=29){
                    //Android10之后
                    sdDir = context.getExternalFilesDir(null);
                }else {
                    sdDir =getExternalFilesDir(Environment.DIRECTORY_PICTURES);;// 获取SD卡根目录
    
                }
            } else {
                sdDir = Environment.getRootDirectory();// 获取跟目录
            }
            return sdDir.toString();
        }
    
        public  void makeDir(File dir) {
            if (!dir.getParentFile().exists()) {
                makeDir(dir.getParentFile());
            }
            dir.mkdir();
        }
    
    }
    

      

  • 相关阅读:
    electron 显示对话框 showMessageBoxSync showMessageBox
    c++ 随机数 取值范围 多线程
    c++ 字符串转换为数字
    VS2019 C++动态链接库的创建使用(1)
    js mutationobserver property vs attribute
    Chromium base 基础库概览
    Git:合并分支----git merge命令应用的三种情景
    chromium 处理 addEventListener 事件
    JavaScript监听属性改变
    chrome 启动开关参数
  • 原文地址:https://www.cnblogs.com/ltw222/p/14913269.html
Copyright © 2011-2022 走看看