java中将从数据库查询的信息输出到excel文件中
package com.cn.peitest.excel;
import java.io.File;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List; import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook; public class ExcelBook {
public static void main(String[] args) { ArrayList<EmployeeBasicInformation> arrayList=new
ArrayList<EmployeeBasicInformation>(); EmployeeBasicInformation bean0 =new
EmployeeBasicInformation(); bean0.setId("主表id"); bean0.setName("姓名");
bean0.setCreatorCode("用户号"); EmployeeBasicInformation bean =new
EmployeeBasicInformation(); bean.setId("000220"); bean.setName("裴");
bean.setCreatorCode("001223456"); EmployeeBasicInformation bean1 =new
EmployeeBasicInformation(); bean1.setId("000220"); bean1.setName("裴");
bean1.setCreatorCode("001223456"); arrayList.add(bean0); arrayList.add(bean);
arrayList.add(bean1); System.out.println("arrayList============="+arrayList);
excelOut(arrayList); reflect(bean1); } //将数据导出到Excel
public static void excelOut(ArrayList<EmployeeBasicInformation> arrayList) {
WritableWorkbook bWorkbook = null;
try {
// 创建Excel对象
bWorkbook = Workbook.createWorkbook(new File("D:/book.xls"));
// 通过Excel对象创建一个选项卡对象
WritableSheet sheet = bWorkbook.createSheet("sheet1", 0);
//使用循环将数据读出
for (int i =0; i < arrayList.size(); i++) {
EmployeeBasicInformation book=arrayList.get(i);
Label label=new Label(0,i,String.valueOf(book.getId()));
Label label1=new Label(1,i,String.valueOf(book.getName()));
Label label2=new Label(2,i,String.valueOf(book.getCreatorCode()));
sheet.addCell(label);
sheet.addCell(label1);
sheet.addCell(label2); } // 创建一个单元格对象,第一个为列,第二个为行,第三个为值
Label label = new Label(0, 2, "test");
// 将创建好的单元格放入选项卡中
//sheet.addCell(label);
// 写如目标路径
bWorkbook.write(); } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
bWorkbook.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
//循环获取实体中的值
public static List reflect(EmployeeBasicInformation e){
List list=new ArrayList();
try {
Class cls = e.getClass();
Field[] fields = cls.getDeclaredFields();
for(int i=0; i<fields.length; i++){
Field f = fields[i];
f.setAccessible(true);
System.out.println("属性名:" + f.getName() + " 属性值:" + f.get(e));
list.add(f.get(e));
}
} catch (Exception e2) {
e2.printStackTrace();
// TODO: handle exception
}
return list; } }
//实体类 package com.cn.peitest.excel;
import java.io.Serializable; /**
* 员工基本信息
*
* @author pei
*/
public class EmployeeBasicInformation implements Serializable { private static final long serialVersionUID = 1L;
/**
* 创建人名称
*/
private String creatorName; /**
* 创建人编号
*/
private String creatorCode; /**
* 姓名
*/
private String name; /**
* 工号
*/
private String employeeNumber; /**
* 身份证号码
*/
private String idCardNumber; /**
* 入职日期
*/
private String entryDate; /**
* 转正日期
*/
private String becomeFullMemberDate; /**
* 性别
*/
private String sex; /**
* 部门名称
*/
private String department; /**
* 部门编号
*/
private String departmentCode; /**
* 组别
*/
private String group; /**
* 组编号
*/
private String groupCode; /**
* 职位
*/
private String position; /**
* 职能
*/
private String function; /**
* 职级
*/
private String positionRank; /**
* 婚否
*/
private String isMarry; /**
* 合同开始日期
*/
private String contractStartDate; /**
* 合同结束日期
*/
private String contractEndDate; /**
* 专业
*/
private String profession; /**
* 学历
*/
private String educationBackground; /**
* 政治面貌
*/
private String politicsStatus; /**
* 毕业院校
*/
private String schoolOfGraduation; /**
* 户籍地址
*/
private String censusRegisterAddress; /**
* 户口类型
*/
private String registeredResidenceType; /**
* 暂住地址
*/
private String temporaryAddress; /**
* 联系电话
*/
private String contactPhone; /**
* 紧急联系人
*/
private String emergencyContact; /**
* 电话
*/
private String phone; /**
* 在职状态
*/
private String workingState; /**
* 表id
*/
private String id;
/**
* 租户id
*/
private String tenantId;
/**
* 入职月数
*/
private Double entryMonths;
/**
* 是否删除 0 未删除 1删除
*/
private String isDelete;
public String getCreatorName() {
return creatorName;
}
public void setCreatorName(String creatorName) {
this.creatorName = creatorName;
}
public String getCreatorCode() {
return creatorCode;
}
public void setCreatorCode(String creatorCode) {
this.creatorCode = creatorCode;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmployeeNumber() {
return employeeNumber;
}
public void setEmployeeNumber(String employeeNumber) {
this.employeeNumber = employeeNumber;
}
public String getIdCardNumber() {
return idCardNumber;
}
public void setIdCardNumber(String idCardNumber) {
this.idCardNumber = idCardNumber;
}
public String getEntryDate() {
return entryDate;
}
public void setEntryDate(String entryDate) {
this.entryDate = entryDate;
}
public String getBecomeFullMemberDate() {
return becomeFullMemberDate;
}
public void setBecomeFullMemberDate(String becomeFullMemberDate) {
this.becomeFullMemberDate = becomeFullMemberDate;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public String getDepartmentCode() {
return departmentCode;
}
public void setDepartmentCode(String departmentCode) {
this.departmentCode = departmentCode;
}
public String getGroup() {
return group;
}
public void setGroup(String group) {
this.group = group;
}
public String getGroupCode() {
return groupCode;
}
public void setGroupCode(String groupCode) {
this.groupCode = groupCode;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public String getFunction() {
return function;
}
public void setFunction(String function) {
this.function = function;
}
public String getPositionRank() {
return positionRank;
}
public void setPositionRank(String positionRank) {
this.positionRank = positionRank;
}
public String getIsMarry() {
return isMarry;
}
public void setIsMarry(String isMarry) {
this.isMarry = isMarry;
}
public String getContractStartDate() {
return contractStartDate;
}
public void setContractStartDate(String contractStartDate) {
this.contractStartDate = contractStartDate;
}
public String getContractEndDate() {
return contractEndDate;
}
public void setContractEndDate(String contractEndDate) {
this.contractEndDate = contractEndDate;
}
public String getProfession() {
return profession;
}
public void setProfession(String profession) {
this.profession = profession;
}
public String getEducationBackground() {
return educationBackground;
}
public void setEducationBackground(String educationBackground) {
this.educationBackground = educationBackground;
}
public String getPoliticsStatus() {
return politicsStatus;
}
public void setPoliticsStatus(String politicsStatus) {
this.politicsStatus = politicsStatus;
}
public String getSchoolOfGraduation() {
return schoolOfGraduation;
}
public void setSchoolOfGraduation(String schoolOfGraduation) {
this.schoolOfGraduation = schoolOfGraduation;
}
public String getCensusRegisterAddress() {
return censusRegisterAddress;
}
public void setCensusRegisterAddress(String censusRegisterAddress) {
this.censusRegisterAddress = censusRegisterAddress;
}
public String getRegisteredResidenceType() {
return registeredResidenceType;
}
public void setRegisteredResidenceType(String registeredResidenceType) {
this.registeredResidenceType = registeredResidenceType;
}
public String getTemporaryAddress() {
return temporaryAddress;
}
public void setTemporaryAddress(String temporaryAddress) {
this.temporaryAddress = temporaryAddress;
}
public String getContactPhone() {
return contactPhone;
}
public void setContactPhone(String contactPhone) {
this.contactPhone = contactPhone;
}
public String getEmergencyContact() {
return emergencyContact;
}
public void setEmergencyContact(String emergencyContact) {
this.emergencyContact = emergencyContact;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getWorkingState() {
return workingState;
}
public void setWorkingState(String workingState) {
this.workingState = workingState;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTenantId() {
return tenantId;
}
public void setTenantId(String tenantId) {
this.tenantId = tenantId;
}
public Double getEntryMonths() {
return entryMonths;
}
public void setEntryMonths(Double entryMonths) {
this.entryMonths = entryMonths;
}
public String getIsDelete() {
return isDelete;
}
public void setIsDelete(String isDelete) {
this.isDelete = isDelete;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
@Override
public String toString() {
return "EmployeeBasicInformation [creatorName=" + creatorName + ", creatorCode=" + creatorCode + ", name="
+ name + ", employeeNumber=" + employeeNumber + ", idCardNumber=" + idCardNumber + ", entryDate="
+ entryDate + ", becomeFullMemberDate=" + becomeFullMemberDate + ", sex=" + sex + ", department="
+ department + ", departmentCode=" + departmentCode + ", group=" + group + ", groupCode=" + groupCode
+ ", position=" + position + ", function=" + function + ", positionRank=" + positionRank + ", isMarry="
+ isMarry + ", contractStartDate=" + contractStartDate + ", contractEndDate=" + contractEndDate
+ ", profession=" + profession + ", educationBackground=" + educationBackground + ", politicsStatus="
+ politicsStatus + ", schoolOfGraduation=" + schoolOfGraduation + ", censusRegisterAddress="
+ censusRegisterAddress + ", registeredResidenceType=" + registeredResidenceType + ", temporaryAddress="
+ temporaryAddress + ", contactPhone=" + contactPhone + ", emergencyContact=" + emergencyContact
+ ", phone=" + phone + ", workingState=" + workingState + ", id=" + id + ", tenantId=" + tenantId
+ ", entryMonths=" + entryMonths + ", isDelete=" + isDelete + "]";
} }
java中将从数据库查询的信息输出到excel文件中的更多相关文章
- arcgis api for javascript 学习(三) 调用发布地图信息,并将地图属性信息输出到Excel表中
吐血推荐:网上搜了很久关于webgis地图属性表输出到Excel表,并没能找到相关有价值的信息,在小白面前,这就是一脸懵x啊!网上要么是关于前端如何在页面上直接导出excel,和webgis半毛钱关系 ...
- 把页面的Table直接输出到Excel文件中
有个需求是统计的时候,为生成的html表格提供导出功能,但是这样导出Excel不会显示自身的表格 影响美观,但是excel会显示html的css样式,这里可以通过处理行对象的方式进行导出,但是处理起纵 ...
- 【Java/JDBC】借助ResultSetMetaData,从数据库表中抽取字段信息存成Excel文件
本例工程下载:https://files.cnblogs.com/files/xiandedanteng/FindNotnullColumns20191102-3.rar 工作中曾有个为42张表建立测 ...
- 将从mysql数据库查询的信息,遍历到List<>以及一些随机数的生成
将从mysql数据库查询的信息,遍历到List<>以及一些随机数的生成. 代码比较乱,但是方法还是对的,大家又需要的选择看,希望对博友 有帮助,欢迎留言分享! public class s ...
- java代码将excel文件中的内容列表转换成JS文件输出
思路分析 我们想要把excel文件中的内容转为其他形式的文件输出,肯定需要分两步走: 1.把excel文件中的内容读出来: 2.将内容写到新的文件中. 举例 一张excel表中有一个表格: 我们需要将 ...
- Java读取、写入、处理Excel文件中的数据(转载)
原文链接 在日常工作中,我们常常会进行文件读写操作,除去我们最常用的纯文本文件读写,更多时候我们需要对Excel中的数据进行读取操作,本文将介绍Excel读写的常用方法,希望对大家学习Java读写Ex ...
- 效率最高的Excel数据导入---(c#调用SSIS Package将数据库数据导入到Excel文件中【附源代码下载】) 转
效率最高的Excel数据导入---(c#调用SSIS Package将数据库数据导入到Excel文件中[附源代码下载]) 本文目录: (一)背景 (二)数据库数据导入到Excel的方法比较 ...
- log4j根据包名 日志输出到不同文件中 , service层无法输出日志问题
1. service 层因为要配置事务,使用了代理 <aop:config proxy-target-calss=''true"> <aop:pointcut id=&qu ...
- java 写入数据到Excel文件中_Demo
=======第一版:基本功能实现======= import com.google.common.collect.Maps; import org.apache.log4j.Logger; impo ...
随机推荐
- 心跳event
在通过sessionID和passwd获取会话的时候会使原会话断开,后续的事件都变为disconnected,且zk会不断发送disconnected给原连接,connected给新连接
- SpringCloud 源码系列(1)—— 注册中心 Eureka(上)
Eureka 是 Netflix 公司开源的一个服务注册与发现的组件,和其他 Netflix 公司的服务组件(例如负载均衡.熔断器.网关等)一起,被 Spring Cloud 整合为 Spring C ...
- ASP.Net Core 3.1 使用gRPC入门指南
主要参考文章微软官方文档: https://docs.microsoft.com/zh-cn/aspnet/core/grpc/client?view=aspnetcore-3.1 此外还参考了文章 ...
- IDEA2020.2.4最新激活教程,有效期到2089
前言 昨天又有好多粉丝反馈Idea失效过期,也有群里的小伙伴私聊问我,最新的Idea2020.2.4 版本要如何激活? 于是自己在网上搜罗了各种注册码.激活码,均以失败告终,有的虽然当时成功了,当时很 ...
- 20200523_mysql中文乱码
查看字符集: 方法一:show variables like '%character%'; 方法二:show variables like 'collation%';设置字符集: /*建立连接使用的编 ...
- Fiddler 4 (过滤器的使用)
1.先找到过滤器并且勾选 2.勾选 并填写要过滤的地址 3.运行 最终效果如下
- 手写koa-static源码,深入理解静态服务器原理
这篇文章继续前面的Koa源码系列,这个系列已经有两篇文章了: 第一篇讲解了Koa的核心架构和源码:手写Koa.js源码 第二篇讲解了@koa/router的架构和源码:手写@koa/router源码 ...
- moviepy音视频开发专栏文章目录
☞ ░ 前往老猿Python博文目录 ░ <moviepy音视频开发专栏>为收费专栏,基于老猿阅读moviepy1.03版本的源代码以及大量测试验证的基础上,详细介绍moviepy主要音视 ...
- Python链式赋值执行顺序及执行方式的证明
专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 老猿学5G博文目录 在<关于Python链式赋值的赋值顺序问题& ...
- PyQt(Python+Qt)学习随笔:QMainWindow的addDockWidget方法增加QDockWidget停靠窗到主窗口
专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 DockWidget除了放在QMainWindow窗口内外,也可以放在 ...