• 最后完整的excel.java文件


    package com.example.temperature;
    import android.content.Context;
    import android.widget.Toast;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.ArrayList;
    import java.util.List;
    
    import jxl.Workbook;
    import jxl.WorkbookSettings;
    import jxl.format.Alignment;
    import jxl.format.Colour;
    import jxl.format.UnderlineStyle;
    import jxl.write.Label;
    import jxl.write.WritableCell;
    import jxl.write.WritableCellFormat;
    import jxl.write.WritableFont;
    import jxl.write.WritableSheet;
    import jxl.write.WritableWorkbook;
    import jxl.write.WriteException;
    
    import static com.baidu.location.h.n.m;
    
    /**
     * @author dmrfcoder
     * @date 2019/2/14
     */
    
    public class Excel {
    
        private static WritableFont arial14font = null;
    
        private static WritableCellFormat arial14format = null;
        private static WritableFont arial10font = null;
        private static WritableCellFormat arial10format = null;
        private static WritableFont arial12font = null;
        private static WritableCellFormat arial12format = null;
        private final static String UTF8_ENCODING = "UTF-8";
    
    
        /**
         * 单元格的格式设置 字体大小 颜色 对齐方式、背景颜色等...
         */
        private static void format() {
            try {
                arial14font = new WritableFont(WritableFont.ARIAL, 14, WritableFont.BOLD);
                arial14font.setColour(jxl.format.Colour.LIGHT_BLUE);
                arial14format = new WritableCellFormat(arial14font);
                arial14format.setWrap(true);
                arial14format.setAlignment(jxl.format.Alignment.CENTRE);
                arial14format.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
                arial14format.setBackground(jxl.format.Colour.VERY_LIGHT_YELLOW);
    
                arial10font = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD);
                arial10format = new WritableCellFormat(arial10font);
                arial10format.setAlignment(jxl.format.Alignment.CENTRE);
                arial10format.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
                arial10format.setWrap(true);
    
                arial12font = new WritableFont(WritableFont.ARIAL, 10);
                arial12format = new WritableCellFormat(arial12font);
                //对齐格式
                arial10format.setAlignment(jxl.format.Alignment.CENTRE);
                //设置边框
                arial12format.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
    
                arial12format.setWrap(true);
            } catch (WriteException e) {
                e.printStackTrace();
            }
        }
    
    
        /**
         * 初始化Excel表格
         *
         * @param filePath  存放excel文件的路径(path/demo.xls)
         * @param sheetName Excel表格的表名
         * @param colName   excel中包含的列名(可以有多个)
         */
        public static void initExcel(String filePath, String sheetName, String[] colName) {
            format();
            WritableWorkbook workbook = null;
            System.out.println("是否运行excel文件");
            try {
                File file = new File(filePath);
                if (!file.exists()) {
    
                    workbook = Workbook.createWorkbook(file);
                    //设置表格的名字
                    WritableSheet sheet = workbook.createSheet(sheetName,0);
                    //m.addMergedRegion(new CellRangeAddress(1,1,1,2));
                    sheet.addCell((WritableCell) new Label(0, 0, filePath, arial14format));
                    sheet.setRowView(0, 800);
                    sheet.setRowView(20, 600);
                    sheet.setRowView(3, 600);
                    sheet.mergeCells(0,0,6,0);
                    sheet.mergeCells(5,1,6,1);
                    sheet.mergeCells(1,2,3,2);
                    sheet.mergeCells(1,3,3,3);
                    sheet.mergeCells(5,2,6,2);
                    sheet.mergeCells(5,3,6,3);
                    sheet.mergeCells(1,1,3,1);
                    sheet.mergeCells(0,4,6,4);
    
                    sheet.mergeCells(5,5,6,5);
                    sheet.mergeCells(5,6,6,6);
                    sheet.mergeCells(5,7,6,7);
                    sheet.mergeCells(5,8,6,8);
                    sheet.mergeCells(5,9,6,9);
                    sheet.mergeCells(5,10,6,10);
                    sheet.mergeCells(5,11,6,11);
                    sheet.mergeCells(5,12,6,12);
                    sheet.mergeCells(5,13,6,13);
                    sheet.mergeCells(5,14,6,14);
                    sheet.mergeCells(5,15,6,15);
                    sheet.mergeCells(5,16,6,16);
                    sheet.mergeCells(5,17,6,17);
                    sheet.mergeCells(5,18,6,18);
                    sheet.mergeCells(5,19,6,19);
    
                    sheet.mergeCells(3,5,4,5);
                    sheet.mergeCells(3,6,4,6);
                    sheet.mergeCells(3,7,4,7);
                    sheet.mergeCells(3,8,4,8);
                    sheet.mergeCells(3,9,4,9);
                    sheet.mergeCells(3,10,4,10);
                    sheet.mergeCells(3,11,4,11);
                    sheet.mergeCells(3,12,4,12);
                    sheet.mergeCells(3,13,4,13);
                    sheet.mergeCells(3,14,4,14);
                    sheet.mergeCells(3,15,4,15);
                    sheet.mergeCells(3,16,4,16);
                    sheet.mergeCells(3,17,4,17);
                    sheet.mergeCells(3,18,4,18);
                    sheet.mergeCells(3,19,4,19);
    
                    sheet.mergeCells(0,21,1,21);
                    sheet.mergeCells(2,21,3,21);
                    sheet.mergeCells(5,21,6,21);
                    sheet.mergeCells(0,20,6,20);
                    sheet.addCell(new Label(0,0 ,"学生十四天健康情况登记表" , arial10format));
                    sheet.addCell(new Label(0,1 ,"单位名称" , arial10format));
                    sheet.addCell(new Label(0,2 ,"姓名" , arial10format));
                    sheet.addCell(new Label(0,3 ,"目前健康状态" , arial10format));
                    sheet.addCell(new Label(0,4 ,"每日体温,健康状况监测(周期14天)" , arial10format));
                    sheet.addCell(new Label(0,5 ,"日期" , arial10format));
    
                /*sheet.addCell(new Label(0,6 ,"2021-3-5" , arial10format));
                sheet.addCell(new Label(0,7 ,"2021-3-6" , arial10format));
                sheet.addCell(new Label(0,8 ,"2021-3-7" , arial10format));
                sheet.addCell(new Label(0,9 ,"2021-3-8" , arial10format));
                sheet.addCell(new Label(0,10 ,"2021-3-9" , arial10format));
                sheet.addCell(new Label(0,11 ,"2021-3-10" , arial10format));
                sheet.addCell(new Label(0,12 ,"2021-3-11" , arial10format));
                sheet.addCell(new Label(0,13 ,"2021-3-12" , arial10format));
                sheet.addCell(new Label(0,14 ,"2021-3-13" , arial10format));
                sheet.addCell(new Label(0,15 ,"2021-3-14" , arial10format));
                sheet.addCell(new Label(0,16 ,"2021-3-15" , arial10format));
                sheet.addCell(new Label(0,17 ,"2021-3-16" , arial10format));
                sheet.addCell(new Label(0,18 ,"2021-3-17" , arial10format));
                sheet.addCell(new Label(0,19 ,"2021-3-18" , arial10format));*/
    
                    sheet.addCell(new Label(0,20 ,"本人承诺:自觉履行疫情防控责任和义务,保证以上填报信息全部属实," +
                            "如有隐瞒,自愿承担相应法律后果。" , arial10format));
                    new WritableCellFormat().setWrap(true);//通过调整宽度和高度自动换行
                    sheet.addCell(new Label(0,21,"本人签字:" , arial10format));
                    sheet.addCell(new Label(1,5 ,"每日体温°C" , arial10format));
                    sheet.addCell(new Label(2,5 ,"健康状况" , arial10format));
                    sheet.addCell(new Label(3,5 ,"当日所在地" , arial10format));
                    sheet.addCell(new Label(4,1 ,"填表日期:" , arial10format));
                    sheet.addCell(new Label(4,2 ,"学号" , arial10format));
                    sheet.addCell(new Label(4,3 ,"手机号" , arial10format));
                    sheet.addCell(new Label(4,21 ,"签字日期:" , arial10format));
                    sheet.addCell(new Label(5,5 ,"备注" , arial10format));
    
    
    
    
    
                /*
                //创建标题栏
                sheet.addCell((WritableCell) new Label(0, 0, filePath, arial14format));
                for (int col = 0; col < colName.length; col++) {
                    sheet.addCell(new Label(col, 0, colName[col], arial10format));
                }
    
                 */
                    //设置行高
                    sheet.setRowView(0, 340);
                    workbook.write();
                    workbook.close();
                    System.out.println("已创建excel文件");
                } else {
                    System.out.println("已存在excel文件");
                    return;
                }
    
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (workbook != null) {
                    try {
                        workbook.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
        /**
         * 将制定类型的List写入Excel中
         *
         * @param objList  待写入的list
         * @param fileName
         * @param c
         * @param <T>
         */
        @SuppressWarnings("unchecked")
    
    
    
        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();
                        }
                    }
                }
    
            }
        }
    
    
    }
  • 相关阅读:
    2440中断
    2440内存管理
    printf不定参数
    时钟体系
    Uart串口
    链接脚本与重定位
    指令速记
    OpenOCD-JTAG调试
    ATPCS规则
    ARM三级流水线
  • 原文地址:https://www.cnblogs.com/lkwkk/p/14908807.html
Copyright © 2020-2023  润新知