(1)读取excel,并入库
FileInputStream fin = new FileInputStream(file);
HSSFWorkbook wb = new HSSFWorkbook(fin);
HSSFSheet sheet = wb.getSheetAt(0);
Session session = sessionFactory.openSession();
Transaction ts = session.beginTransaction();
try {
ts.begin();
for (int index = 1; index < sheet.getLastRowNum(); index++) {
DiveShop shop = new DiveShop();
shop.setDiveShopName(sheet.getRow(index).getCell(0).getStringCellValue());
shop.setDiveShopDesc(sheet.getRow(index).getCell(1).getStringCellValue());
shop.setCountry(sheet.getRow(index).getCell(1) .getStringCellValue());
session.save(shop);
}
ts.commit();
} catch (Exception ex) {
ts.rollback();
} finally {
session.close();
}
(2)将内容写入到Excel中
Map<String,String> typeMap=new HashMap<String,String>();
typeMap.put("1", "达人奖");
typeMap.put("2", "超级达人奖");
typeMap.put("3", "鼓励奖");
typeMap.put("4", "业务体验奖");
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet();
HSSFRow headerRow = sheet1.createRow(0);
initHeader(headerRow);
int rownum=0;
for(ShakeAward sa:shakeList){
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String awardtime="";
if(sa.getAwardTime()!=null){
awardtime=format.format(sa.getAwardTime());
}
HSSFRow row=sheet1.createRow(++rownum);
row.createCell(0).setCellValue(sa.getMobileNo());
row.createCell(1).setCellValue(sa.getAwardName());
row.createCell(2).setCellValue(awardtime);
String awardTypeName=typeMap.get(sa.getAwardType());
row.createCell(3).setCellValue(awardTypeName);
row.createCell(4).setCellValue(sa.getRank());
}
try {
output.flush();
wb.write(output);
output.close();
}
catch (IOException e) {
}
}
private void initHeader(HSSFRow row0) {
row0.createCell(0).setCellValue("获奖号码");
row0.createCell(1).setCellValue("奖品名称");
row0.createCell(2).setCellValue("获奖时间");
row0.createCell(3).setCellValue("奖品类型");
row0.createCell(4).setCellValue("排名");
}
}