首先要确保有apache poi包然后利用上传附件控件发请求到后台 然后后台处理好保存到数据库中,注意的是apache poi包读取的excel只能读取excel2003以xls结尾的excel ,excel2007会发生识别错误!
以下是请求方法
@RequestMapping(params={"method=upLoad"})
@Action
public String upLoad(HttpServletRequest request,
HttpServletResponse response, ModelMap modelMap)
throws WafException {
MultipartFile file = ((MultipartHttpServletRequest)request).getFile("files[]");
ServletContext servletContext = this.getServletContext();
if(!checkFileFormat(file, servletContext)){
servletContext.setAttribute(getConversationID(), "文件格式不对");//文件格式不对
}
try
{
byte[] bytes = file.getBytes();
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
String list = importDayPrice(in);
if(list==null)
return null;
servletContext.setAttribute(getConversationID(), list);
}
catch (IOException e)
{
JSONUtils.ERROR(e.getMessage(), e);
e.printStackTrace();
}
return null;
}
以下是读取excel方法
/**
*
* 版本:EAS7.0
* 作者(修改人):xu chang
* 修改时间:2014-10-10
* <p>
* 描 述:人天单价导入
* @param in
* @return
*/
public String importDayPrice(InputStream in){
String [] colNames={"工号","员工姓名","人天单价"};
if(in!=null){
try
{
POIFSFileSystem pfs = new POIFSFileSystem(in);
HSSFWorkbook workbook = new HSSFWorkbook(pfs);
int sheetCount = workbook.getNumberOfSheets();// 获得当前Excel表共有几个sheet
if (sheetCount < 1) {
return "导入Excel文件为空。";
}
HSSFSheet sheet = workbook.getSheetAt(0);// 得到第一个sheet
int rowCount = sheet.getPhysicalNumberOfRows();// 总行数
if (rowCount < 2) {
return "导入Excel文件数据为空,请检查模版。";
}
HSSFRow titleName=sheet.getRow(0);
for(int i=0;i<colNames.length;i++){
if(!colNames[i].equals(titleName.getCell(i).getStringCellValue())){
return "模版格式不对,请确认模版列名是否正确,错误列:"+titleName.getCell(i).getStringCellValue();
}
}
IPerson iPerson =PersonFactory.getRemoteInstance();
PersonCollection psCol=new PersonCollection();
Set<String> check=new HashSet<String>();
for(int i=1;i<rowCount;i++){
HSSFRow row=sheet.getRow(i);
//只有三列
PersonInfo person=new PersonInfo();
for(int j=0;j<colNames.length;j++){
HSSFCell cell=row.getCell(j);
if(j==0){
//工号
if(ExcelUtils.formatStr(cell)==null){
return "第"+(i+1)+"行工号不能为空";
}else{
PersonCollection col=
iPerson.getPersonCollection(
"select id,name,number where number ='"+ExcelUtils.formatStr(cell)+"' ");
if(col.size()>0){
check.add(ExcelUtils.formatStr(cell));
person.setNumber(ExcelUtils.formatStr(cell));
}else{
return "第"+(i+1)+"行工号在系统中找不到对应的人!请检查系统是否存在此人!";
}
}
}else if(j==2){
//人天单价
if(ExcelUtils.formatStr(cell)==null){
return "第"+(i+1)+"行人天单价不能为空";
}else{
person.setCell(ExcelUtils.formatStr(cell));
}
}
}
psCol.add(person);
}
if(check.size()!=(rowCount-1)){
return "总共数据为"+(rowCount-1)+"存在重复工号!去除重复工号后数据为"+check.size()+"条!";
}
//此处放在事务中执行
ProjectBillFactory.getRemoteInstance().importDayPrice(psCol);
}
catch (IOException e)
{
e.printStackTrace();
return "发生系统错误!"+e.getMessage();
}
catch (BOSException e)
{
e.printStackTrace();
return "发生查询错误!"+e.getMessage();
}
catch (EASBizException e)
{
e.printStackTrace();
return "发生导入数据业务异常!"+e.getMessage();
}
}
return "人天单价导入成功!";
}
读取过程中往往会发生各种转译错误因此提炼了一个Utils工具类转换各种数据
/**
*
* 简述:
* <p>
* 详细描述:excel工具数据格式转换类
* <p>
* @author xu chang
* @version 2014-10-9
*/
public class ExcelUtils
{
/**
*
* 版本:EAS7.0
* 作者(修改人):xu chang
* 修改时间:2014-10-9
* <p>
* 描 述:poi excel字符串转换
* @param cell
* @return
*/
public static String formatStr(HSSFCell cell){
if(cell==null){
return null;
}else if((cell+"").equalsIgnoreCase("null")){
return null;
}else{
return (cell+"").trim();
}
}
/**
*
* 版本:EAS7.0
* 作者(修改人):xu chang
* 修改时间:2014-10-9
* <p>
* 描 述:poi excel日期转换
* @param cell
* @return
*/
public static Date formatDate(HSSFCell cell){
if(cell==null){
return null;
}else if((cell+"").equalsIgnoreCase("null")){
return null;
}else{
return cell.getDateCellValue();
}
}
/**
*
* 版本:EAS7.0
* 作者(修改人):xu chang
* 修改时间:2014-10-9
* <p>
* 描 述:poi excel金额转换
* @param cell
* @return
*/
public static BigDecimal formatAmount(HSSFCell cell){
if(cell==null){
return BigDecimal.ZERO;
}else if((cell+"").equalsIgnoreCase("null")){
return BigDecimal.ZERO;
}else{
return new BigDecimal(cell+"");
}
}
}
最后我们用控件的结束事件抛出校验提示
/**
*
* 版本:EAS7.0
* 作者(修改人):xu chang
* 修改时间:2014-10-6
* <p>
* 描 述:导入完成后将校验数据传出
* @return
* @throws WafException
* @throws WafBizException
*/
@RequestMapping(params={"method=fileupload1_onStop"})
@Action
public String fileupload1_onStop(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws WafException,WafBizException{
ServletContext servletContext = this.getServletContext();
String list = servletContext.getAttribute(getConversationID()).toString();
servletContext.removeAttribute(getConversationID());
if(list!=null){
JSONUtils.SUCCESS(list);
}
return null;
}
由于时间仓促就介绍到这了Eas waf2利用poi导入数据方法,如有更好的实现可以交流