简单的poi导出
程序员文章站
2022-05-07 19:29:01
...
@Override
public void exportXlsNotBill(HttpServletResponse response, TSBaseUser user) throws UnsupportedEncodingException {
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("无审批单特殊物品信息");
sheet.setDefaultRowHeightInPoints(20);
sheet.setDefaultColumnWidth((short) 20);
// 第三步,在sheet中添加表头第0行
HSSFRow row = sheet.createRow((int) 0);
// 创建一个居中格式
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 第四步 设置值表头 设置表头
HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("上报人姓名 ");
cell.setCellStyle(style);
cell = row.createCell((short) 1);
cell.setCellValue("上报单位单位");
cell.setCellStyle(style);
cell = row.createCell((short) 2);
cell.setCellValue("联系电话 ");
cell.setCellStyle(style);
cell = row.createCell((short) 3);
cell.setCellValue("申请时间");
cell.setCellStyle(style);
cell = row.createCell((short) 4);
cell.setCellValue("出入境方式 ");
cell.setCellStyle(style);
cell = row.createCell((short) 5);
cell.setCellValue("物品类别 ");
cell.setCellStyle(style);
cell = row.createCell((short) 6);
cell.setCellValue("产品名称");
cell.setCellStyle(style);
cell = row.createCell((short) 7);
cell.setCellValue("数量 ");
cell.setCellStyle(style);
cell = row.createCell((short);
cell.setCellValue("重量");
cell.setCellStyle(style);
cell = row.createCell((short) 9);
cell.setCellValue("货值");
cell.setCellStyle(style);
cell = row.createCell((short) 10);
cell.setCellValue("处理方式");
cell.setCellStyle(style);
cell = row.createCell((short) 11);
cell.setCellValue("运输方式");
cell.setCellStyle(style);
cell = row.createCell((short) 12);
cell.setCellValue("备注");
cell.setCellStyle(style);
//第五步 设置数据集
StringBuffer hql = new StringBuffer();
hql.append("from NotBillProductEntity where 1=1 ");
if (ResourceUtil.getSessionUser().getManagerRole()== Manager_Role.SHENGJU_ADMIN) {
hql.append(" and depart like '" + user.getDepartid().substring(0, 2) + "%'");
} else if (ResourceUtil.getSessionUser().getManagerRole() == Manager_Role.FENZHIJU_ADMIN) {
hql.append(" and depart='" + user.getDepartid() + "'");
}
List<NotBillProductEntity> list = findHql(hql.toString());
for (int i = 0; i < list.size(); i++) {
row = sheet.createRow(i + 1);
NotBillProductEntity np = list.get(i);
HSSFCell dataCell = row.createCell((short) 0);
dataCell.setCellValue(np.getCreateUserName());
dataCell = row.createCell((short) 1);
dataCell.setCellValue(np.getCreateCorpId());
dataCell = row.createCell((short) 2);
dataCell.setCellValue(np.getPhone());
dataCell = row.createCell((short) 3);
dataCell.setCellValue(DateUtils.dateToStr(np.getCreateDate(),DateUtils.YYYY_MM_DD));
dataCell = row.createCell((short) 4);
dataCell.setCellValue(np.getExitWay() == 1 ? "入境" : "出境");
dataCell = row.createCell((short) 5);
ProductCategoryEntity pro = getEntity(ProductCategoryEntity.class, np.getPcategory());
dataCell.setCellValue(pro.getName()); //特殊物品类别
dataCell = row.createCell((short) 6);
dataCell.setCellValue(np.getProductName()); //特殊物品类别
dataCell = row.createCell((short) 7);
dataCell.setCellValue(np.getGrossNum() + np.getGrossUnit());
dataCell = row.createCell((short);
dataCell.setCellValue(np.getWeightNum() + np.getWeightUnit());
dataCell = row.createCell((short) 9);
dataCell.setCellValue(np.getValueNum() + "美元USD");
dataCell = row.createCell((short) 10);
String clfs = "其他";
if (np.getDisposeWay() == 1) {
clfs = "截留";
} else if (np.getDisposeWay() == 2) {
clfs = "销毁";
} else if (np.getDisposeWay() == 3) {
clfs = "退运";
}
dataCell.setCellValue(clfs);
dataCell = row.createCell((short) 11);
String ysfs = "货运";
if (np.getTransportWay() == 1) {
clfs = "旅客携带";
} else if (np.getTransportWay() == 2) {
clfs = "邮件";
} else if (np.getTransportWay() == 3) {
clfs = "快件";
}
dataCell.setCellValue(ysfs);
dataCell = row.createCell((short) 12);
dataCell.setCellValue(np.getRemark());
}
// 第六步,将文件存到指定位置
response.setHeader("Content-disposition", "attachment;filename=" + new String(("无审批的特殊物品.xls").getBytes("GBK"), "ISO8859-1"));
try {
wb.write(response.getOutputStream());
response.getOutputStream().close();
} catch (Exception e) {
logger.error("exportXlsNotBill(HttpServletResponse, TSBaseUser)", e); //$NON-NLS-1$
}
}
public void exportXlsNotBill(HttpServletResponse response, TSBaseUser user) throws UnsupportedEncodingException {
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("无审批单特殊物品信息");
sheet.setDefaultRowHeightInPoints(20);
sheet.setDefaultColumnWidth((short) 20);
// 第三步,在sheet中添加表头第0行
HSSFRow row = sheet.createRow((int) 0);
// 创建一个居中格式
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 第四步 设置值表头 设置表头
HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("上报人姓名 ");
cell.setCellStyle(style);
cell = row.createCell((short) 1);
cell.setCellValue("上报单位单位");
cell.setCellStyle(style);
cell = row.createCell((short) 2);
cell.setCellValue("联系电话 ");
cell.setCellStyle(style);
cell = row.createCell((short) 3);
cell.setCellValue("申请时间");
cell.setCellStyle(style);
cell = row.createCell((short) 4);
cell.setCellValue("出入境方式 ");
cell.setCellStyle(style);
cell = row.createCell((short) 5);
cell.setCellValue("物品类别 ");
cell.setCellStyle(style);
cell = row.createCell((short) 6);
cell.setCellValue("产品名称");
cell.setCellStyle(style);
cell = row.createCell((short) 7);
cell.setCellValue("数量 ");
cell.setCellStyle(style);
cell = row.createCell((short);
cell.setCellValue("重量");
cell.setCellStyle(style);
cell = row.createCell((short) 9);
cell.setCellValue("货值");
cell.setCellStyle(style);
cell = row.createCell((short) 10);
cell.setCellValue("处理方式");
cell.setCellStyle(style);
cell = row.createCell((short) 11);
cell.setCellValue("运输方式");
cell.setCellStyle(style);
cell = row.createCell((short) 12);
cell.setCellValue("备注");
cell.setCellStyle(style);
//第五步 设置数据集
StringBuffer hql = new StringBuffer();
hql.append("from NotBillProductEntity where 1=1 ");
if (ResourceUtil.getSessionUser().getManagerRole()== Manager_Role.SHENGJU_ADMIN) {
hql.append(" and depart like '" + user.getDepartid().substring(0, 2) + "%'");
} else if (ResourceUtil.getSessionUser().getManagerRole() == Manager_Role.FENZHIJU_ADMIN) {
hql.append(" and depart='" + user.getDepartid() + "'");
}
List<NotBillProductEntity> list = findHql(hql.toString());
for (int i = 0; i < list.size(); i++) {
row = sheet.createRow(i + 1);
NotBillProductEntity np = list.get(i);
HSSFCell dataCell = row.createCell((short) 0);
dataCell.setCellValue(np.getCreateUserName());
dataCell = row.createCell((short) 1);
dataCell.setCellValue(np.getCreateCorpId());
dataCell = row.createCell((short) 2);
dataCell.setCellValue(np.getPhone());
dataCell = row.createCell((short) 3);
dataCell.setCellValue(DateUtils.dateToStr(np.getCreateDate(),DateUtils.YYYY_MM_DD));
dataCell = row.createCell((short) 4);
dataCell.setCellValue(np.getExitWay() == 1 ? "入境" : "出境");
dataCell = row.createCell((short) 5);
ProductCategoryEntity pro = getEntity(ProductCategoryEntity.class, np.getPcategory());
dataCell.setCellValue(pro.getName()); //特殊物品类别
dataCell = row.createCell((short) 6);
dataCell.setCellValue(np.getProductName()); //特殊物品类别
dataCell = row.createCell((short) 7);
dataCell.setCellValue(np.getGrossNum() + np.getGrossUnit());
dataCell = row.createCell((short);
dataCell.setCellValue(np.getWeightNum() + np.getWeightUnit());
dataCell = row.createCell((short) 9);
dataCell.setCellValue(np.getValueNum() + "美元USD");
dataCell = row.createCell((short) 10);
String clfs = "其他";
if (np.getDisposeWay() == 1) {
clfs = "截留";
} else if (np.getDisposeWay() == 2) {
clfs = "销毁";
} else if (np.getDisposeWay() == 3) {
clfs = "退运";
}
dataCell.setCellValue(clfs);
dataCell = row.createCell((short) 11);
String ysfs = "货运";
if (np.getTransportWay() == 1) {
clfs = "旅客携带";
} else if (np.getTransportWay() == 2) {
clfs = "邮件";
} else if (np.getTransportWay() == 3) {
clfs = "快件";
}
dataCell.setCellValue(ysfs);
dataCell = row.createCell((short) 12);
dataCell.setCellValue(np.getRemark());
}
// 第六步,将文件存到指定位置
response.setHeader("Content-disposition", "attachment;filename=" + new String(("无审批的特殊物品.xls").getBytes("GBK"), "ISO8859-1"));
try {
wb.write(response.getOutputStream());
response.getOutputStream().close();
} catch (Exception e) {
logger.error("exportXlsNotBill(HttpServletResponse, TSBaseUser)", e); //$NON-NLS-1$
}
}