JAVA日报

从零开始的体温app开发(excelutil)

package Excelutil;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import jxl.Workbook;
import jxl.WorkbookSettings;
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 android.content.Context;
import android.content.SharedPreferences;
import android.widget.Toast;
public class util {
public static WritableFont arial14font = null;
public static WritableCellFormat arial14format = null;
public static WritableFont arial10font = null;
public static WritableCellFormat arial10format = null;
public static WritableFont arial12font = null;
public static WritableCellFormat arial12format = null;
public final static String UTF8_ENCODING = "UTF-8";
public final static String GBK_ENCODING = "GBK";
private static WritableWorkbook workbook = null;
private static File file;
public static void format() {
try {
arial14font = new WritableFont(WritableFont.ARIAL, 14, WritableFont.BOLD);
arial14font.setColour(jxl.format.Colour.LIGHT_BLUE);
arial14format = new WritableCellFormat(arial14font);
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.setBackground(jxl.format.Colour.LIGHT_BLUE);
arial12font = new WritableFont(WritableFont.ARIAL, 12);
arial12format = new WritableCellFormat(arial12font);
arial12format.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
}
catch (WriteException e) {
e.printStackTrace();
}
}
public static void initData(Context context) throws WriteException {
// Label label;
// workbook = Workbook.createWorkbook(file);
// WritableSheet sheet = workbook.getSheet(0);
// SharedPreferences sharedPreferences=null;
// if(sharedPreferences==null){
// sharedPreferences=context.getSharedPreferences("data", Context.MODE_PRIVATE);}
// label = new Label(1,2 ,);
// sheet.addCell(label);
// label = new Label(5,2 ,sharedPreferences.getString("num",""));
// sheet.addCell(label);
// label = new Label(5,3 ,"19948100929");
// sheet.addCell(label);
// for(int i=6;i<20;i++){
// label = new Label(3,i ,sharedPreferences.getString("area",""));
// sheet.addCell(label);}
// for(int i=6;i<20;i++){
// label = new Label(5,i ,sharedPreferences.getString("qw",""));
// sheet.addCell(label);}

}
public static void initExcel(String fileName, String[] colName,Context context) {
format();
WritableWorkbook workbook = null;
try {
file = new File(fileName,"tem.xls");
if(!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
if (!file.exists()) {
file.createNewFile();
}
workbook = Workbook.createWorkbook(file);//
WritableSheet sheet = workbook.createSheet("体温表", 0);//第一页
// sheet.addCell((WritableCell) new Label(0, 0, fileName, arial14format));
// for (int col = 0; col < colName.length; col++) {
// sheet.addCell(new Label(col, 0, colName[col], arial10format));
// }
Label label;
sheet.mergeCells(0,0,6,0);
sheet.mergeCells(5,1,6,1);

sheet.mergeCells(1,1,3,1);
sheet.mergeCells(1,2,3,2);
sheet.mergeCells(1,3,3,3);

sheet.mergeCells(0,4,6,4);

sheet.mergeCells(0,20,6,24);

sheet.mergeCells(0,25,1,25);
sheet.mergeCells(2,25,3,25);
sheet.mergeCells(5,25,6,25);
for(int i=1;i<20;i++){
sheet.mergeCells(5,i,6,i);
}
for(int i=5;i<20;i++){
sheet.mergeCells(3,i,4,i);
}
label = new Label(0, 0,"学生14天健康情况登记表",DaBiaoTi());
sheet.addCell(label);
label = new Label(0,1 ,"单位名称");
sheet.addCell(label);
label = new Label(4,1 ,"填表日期");
sheet.addCell(label);
label = new Label(0,2 ,"姓名");
sheet.addCell(label);
label = new Label(4,2 ,"学号");
sheet.addCell(label);
label = new Label(0,3 ,"目前健康状况");
sheet.addCell(label);
label = new Label(4,3 ,"手机号");
sheet.addCell(label);
label = new Label(0,4 ,"每日体温、健康状况监测(周期14天)",DaBiaoTi());
sheet.addCell(label);
label = new Label(0,5 ,"日期");
sheet.addCell(label);
label = new Label(1,5 ,"每日体温℃");
sheet.addCell(label);
label = new Label(2,5 ,"健康状况");
sheet.addCell(label);
label = new Label(3,5 ,"当日所在地");
sheet.addCell(label);
label = new Label(5,5 ,"备注");
sheet.addCell(label);

for(int i=6;i<20;i++){
String str="3月"+(i-5)+"日";
label = new Label(0,i ,str);
sheet.addCell(label);}

label = new Label(0,20 ,"本人承诺:自觉履行疫情防控责任和义务,保证以上填报信息全部属实,如有隐瞒,自愿承担相应法律后果。",DaBiaoTi());
sheet.addCell(label);

label = new Label(0,25 ,"本人签字:");
sheet.addCell(label);
label = new Label(4,25 ,"签字日期:");
sheet.addCell(label);

label = new Label(1,1 ,"石家庄铁道大学");
sheet.addCell(label);

label = new Label(5,1 ,dat());
sheet.addCell(label);//设置日期

label = new Label(1,3 ,"正常");
sheet.addCell(label);
for(int i=6;i<20;i++){
label = new Label(2,i ,"正常");
sheet.addCell(label);//设置日期
}

for(int i=6;i<20;i++){
label = new Label(1,i ,wendu()+"℃");
sheet.addCell(label);//设置温度
}
SharedPreferences sharedPreferences=null;
if(sharedPreferences==null){
sharedPreferences=context.getSharedPreferences("data", Context.MODE_PRIVATE);}
label = new Label(1,2 ,sharedPreferences.getString("Name",""));
sheet.addCell(label);
label = new Label(5,2 ,sharedPreferences.getString("num",""));
sheet.addCell(label);
label = new Label(5,3 ,"19948100929");
sheet.addCell(label);
for(int i=6;i<20;i++){
label = new Label(3,i ,sharedPreferences.getString("area",""));
sheet.addCell(label);}
for(int i=6;i<20;i++){
label = new Label(5,i ,sharedPreferences.getString("qw",""));
sheet.addCell(label);}
workbook.write();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (workbook != null) {
try {
workbook.close();
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
@SuppressWarnings("unchecked")
public static <T> void writeObjListToExcel(List<T> objList, String fileName, Context c) {
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);
for (int j = 0; j < objList.size(); j++) {
ArrayList<String> list=(ArrayList<String>) objList.get(j);
for (int i = 0; i < list.size(); i++) {
sheet.addCell(new Label(i, j+1, list.get(i), arial12format));
}
}
writebook.write();
Toast.makeText(c, "保存成功", 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();
}
}
}
}
}
public static Object getValueByRef(Class cls, String fieldName) {
Object value = null;
fieldName = fieldName.replaceFirst(fieldName.substring(0, 1), fieldName.substring(0, 1).toUpperCase());
String getMethodName = "get" + fieldName;
try {
Method method = cls.getMethod(getMethodName);
value = method.invoke(cls);
}
catch (Exception e) {
e.printStackTrace();
}
return value;
}
public static WritableCellFormat DaBiaoTi() {
WritableFont font = new WritableFont(WritableFont.createFont("黑体"), 10,
WritableFont.BOLD, false);// 定义字体

WritableCellFormat format = new WritableCellFormat(font);
try {
format.setAlignment(jxl.format.Alignment.CENTRE);// 左右居中
format.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);// 上下居中
// format.setBorder(Border.ALL, BorderLineStyle.THIN,
// Colour.BLACK);// 黑色边框
// format.setBackground(Colour.YELLOW);// 黄色背景
} catch (WriteException e) {
e.printStackTrace();
}
//sheet.mergeCells(0,0,6,0);
return format;
}
public static String dat() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
String str = df.format(new Date());
//System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
return str;
}
public static double wendu() {
double n;
Random rand = new Random();
double number = rand.nextInt(20) + 350;
n = number / 10;
return n;
}

}
原文地址:https://www.cnblogs.com/mumulailai/p/14909464.html