体温上报APP——打印

对于展示的信息要求可以实现按照样表自动从数据库中生成个人14天健康记录表。

EchartActivity.java文件:

 1 package com.example.application;
 2 
 3 import android.content.Intent;
 4 import android.net.Uri;
 5 import android.os.Bundle;
 6 import android.webkit.WebView;
 7 import android.webkit.WebViewClient;
 8 
 9 import androidx.appcompat.app.AppCompatActivity;
10 
11 public class EchartActivity extends AppCompatActivity {
12     private WebView chartshow_web;
13     protected void onCreate(Bundle savedInstanceState) {
14         super.onCreate(savedInstanceState);
15         setContentView(R.layout.activity_echart);
16 
17         chartshow_web=(WebView)findViewById(R.id.chartshow_web);
18         chartshow_web.getSettings().setAllowFileAccess(true);
19         chartshow_web.getSettings().setJavaScriptEnabled(true);
20         chartshow_web.loadUrl("file:///android_asset/echart.html");
21 
22 
23 
24         new Thread(new Runnable() {
25             @Override
26             public void run() {
27                 try{
28                     Thread.sleep(2000);
29                 } catch (InterruptedException e){
30                     e.printStackTrace();
31                 }
32             }
33         }).start();
34     }
35 
36 
37 }

OutActivity.java文件:

  1 package com.example.application;
  2 
  3 import android.content.Context;
  4 import android.content.Intent;
  5 import android.database.Cursor;
  6 import android.database.sqlite.SQLiteDatabase;
  7 import android.os.Build;
  8 import android.os.Environment;
  9 import androidx.appcompat.app.AppCompatActivity;
 10 import java.io.File;
 11 import java.util.ArrayList;
 12 import java.util.Calendar;
 13 import android.os.Bundle;
 14 
 15 import android.view.View;
 16 import android.widget.TextView;
 17 import android.widget.Toast;
 18 
 19 import jxl.Workbook;
 20 import jxl.write.Label;
 21 import jxl.write.WritableCellFormat;
 22 import jxl.write.WritableFont;
 23 import jxl.write.WritableSheet;
 24 import jxl.write.WritableWorkbook;
 25 import jxl.write.WriteException;
 26 
 27 
 28 public class OutActivity extends AppCompatActivity {
 29     public static WritableFont arial12font = null;
 30     public static WritableCellFormat arial12format = null;
 31     public static WritableFont arial12font_ = null;
 32     public static WritableCellFormat arial12format_ = null;
 33     public static WritableFont arial12font_1 = null;
 34     public static WritableCellFormat arial12format_1 = null;
 35     public static WritableFont arial18font = null;
 36     public static WritableCellFormat arial18format = null;
 37     public final static String UTF8_ENCODING = "UTF-8";
 38     public final static String GBK_ENCODING = "GBK";
 39 
 40 
 41     private ArrayList<ArrayList<String>> recordList;
 42     private DatabaseHelper myHelper;
 43     public SQLiteDatabase db;
 44     private String username;
 45     private TextView tv_pass;
 46     private String where="石家庄铁道大学";
 47     private Calendar cal;
 48     private String year;
 49     private String month;
 50     private String day;
 51     private String date;
 52     private String ID;
 53     private String phone;
 54 
 55     private static String[] title = { "日期","每日体温","健康状况","当日所在地","备注" };
 56     private File file;
 57     private String fileName;
 58 
 59     @Override
 60     protected void onCreate(Bundle savedInstanceState) {
 61         super.onCreate(savedInstanceState);
 62         setContentView(R.layout.activity_out);
 63         //模拟数据集合
 64         tv_pass=(TextView)findViewById(R.id.out_path) ;
 65         Intent intent=getIntent();
 66         username=intent.getStringExtra("user_name");
 67 
 68         myHelper=new DatabaseHelper(this);
 69         db=myHelper.getWritableDatabase();
 70         getTime();
 71 
 72         Cursor cursor = db.query("ALL_information", null, "name = ?", new String[]{username}, null, null, null);//读取数据库所有信息
 73         if(cursor.moveToFirst()){
 74                 ID=cursor.getString(cursor.getColumnIndex("ID"));
 75                 phone=cursor.getString(cursor.getColumnIndex("phone"));
 76         }
 77         cursor.close();
 78 
 79         recordList=new ArrayList<ArrayList<String>>();
 80     }
 81 
 82     private void getTime() {
 83         cal = Calendar.getInstance();
 84         year = String.valueOf(cal.get(Calendar.YEAR));
 85         month = String.valueOf(cal.get(Calendar.MONTH)+ 1) ;
 86         day = String.valueOf(cal.get(Calendar.DATE));
 87         date = year + "年" + month + "月" + day + "日";
 88     }
 89     /**
 90      * 导出excel
 91      * @param view
 92      */
 93     public void exportExcel(View view) {
 94         file = new File(getSDPath(this)+"/Record");
 95         makeDir(file);
 96         initExcel(file.toString() + "/体温登记表.xls");
 97         fileName = getSDPath(this) + "/Record/体温登记表.xls";
 98         tv_pass.setText("Excel表格已导出至:"+fileName);
 99     }
100     public static void format() {
101         try {
102             arial12font = new WritableFont(WritableFont.createFont("宋体"), 12);
103             arial12format = new WritableCellFormat(arial12font);
104             arial12format.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
105 
106             arial12font_ = new WritableFont(WritableFont.createFont("楷体"), 12,WritableFont.BOLD);
107             arial12format_ = new WritableCellFormat(arial12font_);
108             arial12format_.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
109 
110             arial12font_1 = new WritableFont(WritableFont.createFont("黑体"), 12);
111             arial12format_1 = new WritableCellFormat(arial12font_1);
112             arial12format_1.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
113             arial12format_1.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
114             arial12format_1.setWrap(true);
115 
116             arial18font = new WritableFont(WritableFont.createFont("宋体"), 18,WritableFont.BOLD);
117             arial18format = new WritableCellFormat(arial18font);
118             arial18format.setAlignment(jxl.format.Alignment.CENTRE);//对齐格式
119             arial18format.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN); //设置边框
120         }
121         catch (WriteException e) {
122             e.printStackTrace();
123         }
124 
125     }
126 
127     public void initExcel(String fileName) {
128             format();
129             WritableWorkbook workbook = null;
130 
131             try {
132                 File file = new File(fileName);
133                 if (!file.exists()) {
134                     file.createNewFile();
135                 }
136                 workbook = Workbook.createWorkbook(file);
137                 WritableSheet sheet = workbook.createSheet("体温登记表", 0);
138                 sheet.mergeCells(0,0,6,0);
139                 Label header = new Label(0,0,"学生14天健康情况登记表",arial18format);
140                 sheet.addCell(header);
141                 sheet.mergeCells(0,1,3,1);
142                 sheet.addCell(new Label(0,1,"单位名称:"+where,arial12format_));
143 
144                 sheet.mergeCells(4,1,6,1);
145                 sheet.addCell(new Label(4,1,"填表日期:"+date,arial12format_));
146 
147                 sheet.addCell(new Label(0,2,"姓名",arial12format));
148                 sheet.addCell(new Label(1,2,username,arial12format));
149                 sheet.mergeCells(1,2,3,2);
150                 sheet.addCell(new Label(4,2,"学号:",arial12format));
151                 sheet.addCell(new Label(5,2,ID,arial12format));
152                 sheet.mergeCells(5,2,6,2);
153                 sheet.addCell(new Label(0,3,"目前健康状况:",arial12format));
154                 sheet.mergeCells(1,3,3,3);
155                 sheet.addCell(new Label(1,3,"正常",arial12format));
156                 sheet.addCell(new Label(4,3,"手机号",arial12format));
157                 sheet.addCell(new Label(5,3,phone,arial12format));
158                 sheet.mergeCells(5,3,6,3);
159                 sheet.mergeCells(0,4,6,4);
160                 sheet.addCell(new Label(0,4,"每日体温、健康状况监测(周期14天)",arial18format));
161                 String [] colName = {"日期","每日体温℃","健康状况","当日所在地","备注"};
162                 sheet.mergeCells(3,5,4,5);
163                 sheet.mergeCells(5,5,6,5);
164                 for (int col=0;col<3;col++){
165                     sheet.addCell(new Label(col, 5, colName[col],arial12format));
166                 }
167                 sheet.addCell(new Label(3, 5, colName[3],arial12format));
168                 sheet.addCell(new Label(5, 5, colName[4],arial12format));
169 
170                 int j=6;
171 
172                 Cursor cursor2 = db.query("ALL_information", null, "name=?", new String[]{username}, null, null, null);//读取数据库所有信息
173                 // Cursor cursor=db.query("tt",null,"专业=?",new String[]{"软件工程"},
174                 //null,null,null);//读取数据库里面专业是软件工程的所有信息
175                 if(cursor2.moveToFirst()){
176                     do{
177                         String date=cursor2.getString(cursor2.getColumnIndex("date"));
178                         String temperature=cursor2.getString(cursor2.getColumnIndex("temperature"));
179                         String situation=cursor2.getString(cursor2.getColumnIndex("situation"));
180                         String location=cursor2.getString(cursor2.getColumnIndex("location"));
181                         String tip=cursor2.getString(cursor2.getColumnIndex("tip"));
182 
183 
184                         sheet.addCell(new Label(0,j,date,arial12format));
185                         sheet.addCell(new Label(1,j,temperature,arial12format));
186                         double k = Double.parseDouble(temperature);
187                         if (k < 37.3 &&k > 35) {
188                             sheet.addCell(new Label(2,j,"正常",arial12format));
189                         } else {
190                             sheet.addCell(new Label(2,j,"异常",arial12format));
191                         }
192                         sheet.mergeCells(3,j,4,j);
193                         sheet.addCell(new Label(3,j,location,arial12format));
194                         sheet.mergeCells(5,j,6,j);
195                         sheet.addCell(new Label(5,j,situation+tip,arial12format));
196                         j++;
197                     }while (cursor2.moveToNext());
198                 }
199                 cursor2.close();
200 
201                 sheet.mergeCells(0,20,6,23);
202                 sheet.addCell(new Label(0,20,"本人承诺:自觉履行疫情防控责任和义务,保证以上填报信息全部属实,如有隐瞒,自愿承担相应法律后果。",arial12format_1));
203                 sheet.mergeCells(0,24,3,24);
204                 sheet.addCell(new Label(0,24,"本人签字:",arial12format));
205                 sheet.mergeCells(4,24,6,24);
206                 sheet.addCell(new Label(4,24,"签字日期:",arial12format));
207                 //sheet.mergeCells(5,24,6,24);
208                 workbook.write();
209                 workbook.close();
210                 Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
211 
212             }
213             catch (Exception e) {
214                 e.printStackTrace();
215             }
216             finally {
217                 if (workbook != null) {
218                     try {
219                         workbook.close();
220                     }
221                     catch (Exception e) {
222                         // TODO Auto-generated catch block
223                         e.printStackTrace();
224                     }
225                 }
226             }
227         }
228 
229 
230     /**
231      * 将数据集合 转化成ArrayList<ArrayList<String>>
232      * @return
233      */
234 
235 
236     public static String getSDPath(Context context) {
237         File sdDir = null;
238         boolean sdCardExist = Environment.getExternalStorageState().equals(
239                 Environment.MEDIA_MOUNTED);// 判断sd卡是否存在
240 
241         if (sdCardExist) {
242             if (Build.VERSION.SDK_INT>=29){
243                 //Android10之后
244                 sdDir = context.getExternalFilesDir(null);
245             }else {
246                 sdDir = Environment.getExternalStorageDirectory();// 获取SD卡根目录
247 
248             }
249         } else {
250             sdDir = Environment.getRootDirectory();// 获取跟目录
251         }
252         return sdDir.toString();
253     }
254 
255     public  void makeDir(File dir) {
256         if (!dir.getParentFile().exists()) {
257             makeDir(dir.getParentFile());
258         }
259         dir.mkdir();
260     }
261 
262 }

activity_echart.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <WebView
        android:id="@+id/chartshow_web"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

activity_out.xml文件:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:background="@mipmap/bj3">
 7     <TextView
 8         android:id="@+id/out_path"
 9         android:layout_width="wrap_content"
10         android:layout_height="wrap_content"/>
11     <Button
12         android:layout_width="wrap_content"
13         android:layout_height="wrap_content"
14         android:text="导出到Excel"
15         android:textColor="#F2F2F2"
16         android:background="#328359"
17         android:onClick="exportExcel"
18         android:layout_gravity="center"
19         android:layout_marginTop="200dp"/>
20 </LinearLayout>

截图展示:

 下图是在手机上导出表格所截的图:

原文地址:https://www.cnblogs.com/znjy/p/14905828.html