java实现excel表格导出数据

时间:2023-03-09 01:19:51
java实现excel表格导出数据
/**
* 导出清单 eb中 firstRow(EntityBean) 列表第一行数据,键值对(不包含序号)例:("name","姓名")
* data(EntityBean[]) 列表数据
*
* @author zhaojq
*/
public PR exportData(EntityBean eb)
{
if (eb == null)
{
return new PR(0, "参数为空", null);
}
try
{
// 创建表格文件
String rootpath = NameedPathUtil.getDefaultSavePath();
String filepath = "/LSIP/excel/";
File f = new File(rootpath + filepath);// 创建文件夹路径
// 如果文件夹不存在则创建
if (!f.exists() && !f.isDirectory())
{
f.mkdirs();
}
String filename = System.currentTimeMillis() + ".xls";
File file = new File(f, filename);
if (!file.exists())
{
file.createNewFile();
}
EntityBean firstRow = eb.getBean("firstRow");
EntityBean[] data = eb.getBeans("data");
// 创建工作薄
WritableWorkbook workbook = Workbook.createWorkbook(file);
// 创建新的一页
WritableSheet sheet = workbook.createSheet("第1页", 0);
// Label(列,行,数据) // 第一行
String[] keys = firstRow.getBeanFieldNames();// 获取表头key
for (int i = 0; i < keys.length; i++)
{
Label firstrow = new Label(i, 0, firstRow.getString(keys[i]));
sheet.addCell(firstrow);
} // 创建数据行
for (int i = 0; i < data.length; i++)
{
for (int j = 0; j < keys.length; j++)
{
Label datarow = new Label(j, 1 + i, data[i].getString(keys[j]));
sheet.addCell(datarow);
}
}
workbook.write();
workbook.close();
String contex = LSIPConfigUtil.getDownloadConfig();
return new PR(1, "", String.format("%sLEAP/Download/default%s%s", contex, filepath, filename));
}
catch (Exception e)
{
Global.getInstance().LogError(e);
return new PR(0, e.toString(), null);
}
}

  

/**
* 考试清单导出
*/
public PR exportExamQD(SearchParameters spar)
{
try
{
EntityBean par = new EntityBean();
spar.setOrder("seatno");
spar.setPageSize(10000);
EntityBean[] list = examsearch4eb(spar); if(StringUtil.IsNullOrEmpty(list))return new PR(1,"查询结果为空",null);
//第一行数据
EntityBean firstBean = new EntityBean();
firstBean.set("areaname", "地区");
firstBean.set("examroomid", "考试中心");
firstBean.set("examsubject", "考试科目");
firstBean.set("examtime", "考试时间");
firstBean.set("cardno", "证件号码");
firstBean.set("personname", "姓名");
firstBean.set("mobile", "手机号");
firstBean.set("examstate", "考试报名状态");
firstBean.set("examscore", "成绩");
firstBean.set("seatno", "座位号");
firstBean.set("address", "通讯地址");
for(int i = 0;i < list.length;i++)
{
EntityBean _data = list[i];
String areaname = zonepro.getInstance().getAreaName(_data.getString("areaid"));
_data.set("areaname", areaname); if(_data.getString("examroomid") != null)
{
leapcodevalue _examroomid= CodeTypeCache.getInstance().getCodeValue("LSIP_examroom",_data.getString("examroomid"));
if(_examroomid != null)
_data.set("examroomid", _examroomid.getcodevalue());
}
if(_data.getString("examsubject") != null)
{
leapcodevalue _examsubject= CodeTypeCache.getInstance().getCodeValue("LSIP_examsubject",_data.getString("examsubject"));
if(_examsubject != null)
_data.set("examsubject", _examsubject.getcodevalue());
}
if(_data.getString("examtype") != null)
{
leapcodevalue _examtype= CodeTypeCache.getInstance().getCodeValue("LSIP_examtype",_data.getString("examtype"));
if(_examtype != null)
_data.set("examtype", _examtype.getcodevalue());
}
if(_data.getString("examstate") != null)
{
leapcodevalue _examstate= CodeTypeCache.getInstance().getCodeValue("LSIP_examstate",_data.getString("examstate"));
if(_examstate != null)
_data.set("examstate", _examstate.getcodevalue());
}
if(_data.getString("examtime") != null)
{
_data.set("examtime", _data.getString("examtime").substring(0, 16));
}
}
par.set("firstRow", firstBean);
par.set("data", list);
return new BaseDataService().exportData(par);
}
catch (Exception e)
{
Global.getInstance().LogError(e);
return new PR(0,e.getMessage(),null);
}
}