`

java 读取excel

阅读更多
package com.longtuo.client.action.admin.job;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;


//加入自己的类
import *.*;


/**
* 媒体数据导入
*
* @author xjmfc
*
*/
public class ImportMediaAction extends BaseAction {

/**
*
*/
private static final long serialVersionUID = 1L;

private File newsFile = null;

private String savePath = null;

private String contentType = null;

private String fileName = null;

private JobService jobService = null;

private UserInfoService userInfoService = null;

private String type = null;

public JobService getJobService() {
return jobService;
}

public void setJobService(JobService jobService) {
this.jobService = jobService;
}

public String getNewsFileContentType() {
return contentType;
}

public void setNewsFileContentType(String contentType) {
this.contentType = contentType;
}

public String getNewsFileFileName() {
return fileName;
}

public void setNewsFileFileName(String fileName) {
this.fileName = fileName;
}

public File getNewsFile() {
return newsFile;
}

public void setNewsFile(File newsFile) {
this.newsFile = newsFile;
}

public String getSavePath() {
return savePath;
}

public void setSavePath(String savePath) {
this.savePath = savePath;
}

private List<MediaMeta> getList(Sheet sheet, int rowCount, int columnCount) throws Exception {

UserInfo user = null;
Media media=null;

MediaMeta  mmeta=null;
List<MediaMeta> lst = new ArrayList<MediaMeta>();


for (int i = 0; i < rowCount; i++) {
if (i == 0) {
continue;
}

user = new UserInfo();
mmeta=new MediaMeta();
media=new Media();
user.setLoginTimes(ATTMedia.DEFAULT_LOGINTIME);
            user.setStatus(ATTUserInfo.STATUS_USER_DEFAULT);
            user.setCate("m");
            user.setRoleId("200");
            user.setCreateDate(new Date());
            media.setCreateDate(new Date());
media.setDownloadTimes(ATTMedia.DEFAULT_DOWNLOADTIME);
media.setLev(ATTMedia.LEVEL_DEFAULT_MEDIA);
media.setCreateDate(new Date());
user.setStatus("0");
user.setPasswd(new MD5Util().getMD5ofStr("000000"));
// 1用户名 2.邮箱 3.名称 4.媒体类型 5,6 省市 7.真实姓名 8. 9.电话
String province=null;
for (int j = 0; j < columnCount; j++) {
Cell cell = sheet.getCell(j, i);
switch (j) {
case 0:
user.setUserName(cell.getContents());

                     break;

case 1:
user.setEmail(cell.getContents());
break;

case 2:
                  media.setMName(cell.getContents());
break;
case 3:
if (cell.getContents().indexOf("平面") != -1) {
media.setRangs("001001");
} else if (cell.getContents().indexOf("影视媒体") != -1) {
media.setRangs("001002");
} else if (cell.getContents().indexOf("网络") != -1) {
media.setRangs("001003");
}else if(cell.getContents().indexOf("其他")!=-1){
media.setRangs("001004");
}else{
media.setRangs("001004");
}
break;
case 4:
province =cell.getContents();
break;
case 5:
media.setAddress(province+cell.getContents());
province = null;
break;
case 6:
user.setRealName(cell.getContents());
media.setName(cell.getContents());
break;
case 7:
break;
case 8:
if(cell.getContents().indexOf("1") != -1){
media.setPhone(cell.getContents());
}else{
media.setMobile(cell.getContents());
}
break;
case 9:
                    break;
case 10:
break;
case 11:
media.setPosition(cell.getContents());
break;
default:
break;
}
mmeta.setMedia(media);
mmeta.setUser(user);

}

lst.add(mmeta);
}
return lst;

}

private boolean saveImportData(File ioFile) throws Exception {
int rowCount = 0;
int columnCount = 0;
Workbook workbook = Workbook.getWorkbook(ioFile);
Sheet sheet = workbook.getSheet(0);
rowCount = sheet.getRows();
columnCount = sheet.getColumns();
if (rowCount == 0) {
this.addFieldError("importError", "您导入的文件没数据");
return false;
}
List<MediaMeta> lstMedia = getList(sheet, rowCount, columnCount);

for (MediaMeta meta : lstMedia) {
userInfoService.saveMediaInfo(meta);
}

return false;
}

public String execute() throws Exception {
UserInfo user = this.getUser();
if (user == null) {
return ATTSystem.LOGIN;
}
if (newsFile != null) {
if (saveImportData(newsFile)) {
if (this.getFieldErrors().isEmpty()) {
if (type.equals(ATTJob.SOC_POSITION)) {
this.setAttr("_status", "社会职位批量导入数据成功!");
this.setAttr("type", type);
return SUCCESS;
} else if (type.equals(ATTJob.STU_POSITION)) {
this.setAttr("_status", "学生职位批量导入数据成功!");
this.setAttr("type", type);
return SUCCESS;
}
}

}
}
return INPUT;

}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public UserInfoService getUserInfoService() {
return userInfoService;
}

public void setUserInfoService(UserInfoService userInfoService) {
this.userInfoService = userInfoService;
}

}
  • jxl.jar (692.5 KB)
  • 下载次数: 4
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics