1.poi.jar 包
2.代码:
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("设备信息列表");
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
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);
productAssets.setDeviceType(java.net.URLDecoder.decode(productAssets.getDeviceType(), "UTF-8"));
productAssets.setSerialNumber(java.net.URLDecoder.decode(productAssets.getSerialNumber(), "UTF-8"));
productAssets.setFixedaAssetsNumber(java.net.URLDecoder.decode(productAssets.getFixedaAssetsNumber(), "UTF-8"));
productAssets.setStatus(java.net.URLDecoder.decode(productAssets.getStatus(), "UTF-8"));
productAssets.setCurrentDepartments(java.net.URLDecoder.decode(productAssets.getCurrentDepartments(), "UTF-8"));
productAssetsList = productAssetDAO.findProductAssetsList(productAssets,null);
// 第五步,写入实体数据 实际应用中这些数据从数据库得到,
if(productAssetsList !=null && productAssetsList.size()>0)
{
for (int i = 0; i < productAssetsList.size(); i++)
{
row = sheet.createRow((int) i + 1);
ProductAssets proAsset = (ProductAssets) productAssetsList.get(i);
// 第四步,创建单元格,并设置值
row.createCell((short)0).setCellValue(proAsset.getBrands());
row.createCell((short)1).setCellValue(proAsset.getModel());
row.createCell((short)2).setCellValue(proAsset.getSerialNumber());
row.createCell((short)3).setCellValue(proAsset.getDeviceType());
row.createCell((short)4).setCellValue(proAsset.getAccountingDate());
row.createCell((short)5).setCellValue(proAsset.getCurrentDepartments());
row.createCell((short)6).setCellValue(proAsset.getSourceWhere());
String st=null;
if(proAsset.getStatus().equals("0")){st="正常";}else{st="维修";}
row.createCell((short)7).setCellValue(st);
}
// 第六步,将文件存到指定位置
try
{
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String fileName = "设备信息列表"+sdf.format(new Date())+".xls";
fileName = new String(fileName.getBytes("GBK"),"iso8859-1");
response.reset();
response.setHeader("Content-Disposition","attachment;filename="+fileName);//指定下载的文件名
response.setContentType("application/vnd.ms-excel");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
OutputStream output = response.getOutputStream();
BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);
bufferedOutPut.flush();
wb.write(bufferedOutPut);
bufferedOutPut.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
productAssetsList.clear();