您的位置:首页 > 其它

使用POI导入Excel数据到系统

2012-12-04 09:34 447 查看

1.首先通过页面的上传组件上传一个excel类型的文件后,在java段能得到一个File类型的对象,如果你使用的是struts1,得到的File对象可能是FormFile类型的,如下:

 

FormFile file;



2.得到File对象后,就可以进行处理,一行一行的读取数据

 

try {
POIFSFileSystem fs = new POIFSFileSystem(new BufferedInputStream(file.getInputStream()));
HSSFWorkbook wb = new HSSFWorkbook(fs);

// 全局验证.
HSSFSheet sheet = wb.getSheetAt(0);
int rowsnum = sheet.getLastRowNum();
if (rowsnum < 1) {
//如果没有数据,进行处理,如提示用户
resStr = LocaleMessage.getValue(request, "file.data.is.empty");
file.getInputStream().close();
} else {
HSSFRow row = sheet.getRow(0);

//				HSSFCell cell0 = null;
String celval = "";
List codeList = new ArrayList();
for (int i = sheet.getFirstRowNum() + 1; i <= sheet.getLastRowNum(); i++) {
row = sheet.getRow(i);
HSSFCell cell0 = row.getCell((short) 0);    	//	订单号
HSSFCell cell1  = row.getCell((short) 1 );  	//	订单名称
HSSFCell cell2  = row.getCell((short) 2 );  	//	员工号
HSSFCell cell3  = row.getCell((short) 3 );  	//	约定到期日
HSSFCell cell4  = row.getCell((short) 4 );  	//	实际到账日
HSSFCell cell5  = row.getCell((short) 5 );  	//	提前回款金额
HSSFCell cell6  = row.getCell((short) 6 );  	//	提前回款天数
HSSFCell cell7  = row.getCell((short) 7 );  	//	节省资金成本奖励
HSSFCell cell8  = row.getCell((short) 8 );  	//	奖励工资
HSSFCell cell9  = row.getCell((short) 9 );  	//	奖励信息费
HSSFCell cell10 = row.getCell((short) 10);	//	奖励费用

//将获取到的数据保存到业务对象
BusinessSanctionTqhk tqhk = new BusinessSanctionTqhk();

tqhk.setLastUpdateUser(userName);
tqhk.setIsuse("T");
tqhk.setYf(paraseDate(sjyf));

tqhk.setOrderNumber(getCellValue(getCellValue(cell0)));
tqhk.setCustomerFullName(getCellValue(getCellValue(cell1)));
tqhk.setBusiness(getCellValue(getCellValue(cell2)));
tqhk.setYdqr(getCellValue(getCellValue(cell3)));
tqhk.setSjdzrq(getCellValue(getCellValue(cell4)));
tqhk.setTjhkje(getCellValue(getCellValue(cell5)));
tqhk.setTjhkts(getCellValue(getCellValue(cell6)));
tqhk.setJszjcbjl(getCellValue(getCellValue(cell7)));
tqhk.setJlgzFifteen(getCellValue(getCellValue(cell8)));
tqhk.setJlxxfSeventeen(getCellValue(getCellValue(cell9)));
tqhk.setJlfyTen(getCellValue(getCellValue(cell10)));

codeList.add(tqhk);
}
if (codeList.size() > 0) {
//删除数据库中相同月份的数据,根据实际处理
this.deleteByFullStatementName("com.yutong.crm.sanction.model.SanctionReportImport.deleteTqhkjl", sjyf);
//循环写入数据库
for(int i=0;i<codeList.size();i++){
BusinessSanctionTqhk tqhk = (BusinessSanctionTqhk)codeList.get(i);
this.insertByFullStatementName("com.yutong.crm.sanction.model.SanctionReportImport.insertTqhkjl", tqhk);
}
}
}
resStr = resStr+"导入 提前还款奖励表 数据成功";
} catch (Exception e) {
// TODO: handle exception
resStr = "导入 提前还款奖励表 数据失败"+resStr +"\n"+e.getMessage()+"\n";
}



3.下面是对Excel表格中不同类型内容的处理

public final static DateFormat DEFAULT_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");

/**
* 对excel表格的值进行处理.
*
* @param cell
*            cell
* @return str
*/
private String getCellValue(HSSFCell cell) {
//先判断是不是日期
int dataFormat = cell.getCellStyle().getDataFormat();
if (dataFormat == 14 || dataFormat == 178
|| dataFormat == 180 || dataFormat == 181
|| dataFormat == 182)
{
return getDateValue(cell);
}
String str = "";
// System.err.println("***************TYPE is "+cell.getCellType());
if (cell != null && cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
//也可以在这里先判断是不是日期
//if(HSSFDateUtil.isCellDateFormatted(cell)){
//	return getDateValue(cell);
//}
double dd = cell.getNumericCellValue();

DecimalFormat df = new DecimalFormat("0.##");
str = String.valueOf(df.format(dd)); // .trim();
}
if (cell != null && cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
str = cell.getStringCellValue(); // .trim();
}
if (cell != null && cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {
str = String.valueOf(cell.getBooleanCellValue()); // .trim();
}
if (cell != null && cell.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
str = ""; // .trim();
}

return str.trim();
}

private String getCellValue(String cellValue) {
if (cellValue != null && cellValue.length() > 0) {
return cellValue.trim();
//			if (cellValue.indexOf(".0") > 0)
//			return cellValue.substring(0, cellValue.length() - 2);//根据实际情况处理
//			else
//			return cellValue;
} else
return "";
}

/**
*  返回时间内的特殊时间格式 OFFICE2003
*  @param cell
* @return
*/
private static String getDateValue(HSSFCell cell){
return DEFAULT_DATE_FORMAT.format(cell.getDateCellValue());
}

public Date paraseDate(String sjyf){
SimpleDateFormat sFormat = new SimpleDateFormat("yyyy-MM");
try {
Date date = sFormat.parse(sjyf);
return date;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//出错插入当前日期
return new Date();
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: