关于excel数据的导入,急急急急!!!

若天-- 发布于 2013/08/01 19:10
阅读 246
收藏 0
POI

通过输入流Workbook获取excel里的数据,之后写进数据库,但是在linux系统里没用,是怎么回事,主要下面这段代码

上传页面----------------------

<form onsubmit="return validate()" action="usercar_importExcel.action" name="importForm" method="POST" enctype="multipart/form-data">
  导入文件:<input type="file" name="excelFile" id="excelFile"/><br><br>
  <input type="submit" value="提交"  class="l-button l-button-submit hontekFormButton"/><br>
  <p><font color="green">目前只支持导入Excel文件类型.</font>
  </form>


action类-----------------------------

private File excelFile;(分别有get、set方法)
private String excelFileFileName;(分别有get、set方法)

/**
* 导入Excel中的用户信息,批量保存用户

* @return "success"
*/
public String importExcel() throws Exception {
 ExcelImport excelImport = new ExcelImport(excelFile, excelFileFileName);
LoginUser loginUser = (LoginUser) getSession().getAttribute("loginUser");
try {
String user_id = loginUser.getUserId();
List<UserCar> userCars = excelImport.importExcel(user_id);
int w = 0;
System.out.println("总记录数:" + userCars.size());
for (int i = 0; i < userCars.size(); i++) {
UserCar userCar = userCars.get(i);
boolean bl = usercarservice.selectUserCar(userCar);
if (bl) {
continue;
}
usercarservice.save(userCars.get(i));
w++;
}
if (w == 0) {
jsonMsg.setSuccess("抱歉,您预导入的信息已经存在。");
} else {
jsonMsg.setSuccess("恭喜,成功导入 " + w + " 条记录。");
}

} catch (Exception e) {
errorLog = "导入失败! " + e.getMessage();
jsonMsg.setError(errorLog);
logger.error(errorLog, e);
}
jsonMsg.setMsg(getJSON(jsonMsg));
return "import";
}


ExcelImport类---------------------------

package com.hontek.mms.car.tools;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


import com.hontek.mms.car.po.UserCar;


public class ExcelImport {
private File excelFile;
private String excelFileFileName;
private ExcelWorkSheet<UserCar> excelWorkSheet;

public ExcelImport(File file,String fileName) {
this.excelFile=file;
this.excelFileFileName=fileName;
}

public List<UserCar> importExcel(String user_id) throws Exception{
Workbook workbook=createWorkBook(new FileInputStream(excelFile));
Sheet sheet=workbook.getSheetAt(0);
excelWorkSheet=new ExcelWorkSheet<UserCar>();
excelWorkSheet.setSheetName(sheet.getSheetName());//保存工作单名称
Row firstRow=sheet.getRow(0);
Iterator<Cell> iterator=firstRow.cellIterator();
//保存列名
List<String> cellNames=new ArrayList<String>();
while(iterator.hasNext()){
cellNames.add(iterator.next().getStringCellValue());

}
excelWorkSheet.setColumns(cellNames);

// 获得当前店铺用户.  String user_id
String u_id = user_id;
for(int i=1;i<=sheet.getLastRowNum();i++){
Row row=sheet.getRow(i);
UserCar userCar=new UserCar();
if(!"".equals(row) && row !=null){
if(!row.getCell(0).getStringCellValue().equals("")){
userCar.setsId(u_id);
userCar.setUcName(row.getCell(0).getStringCellValue());
row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
userCar.setUcTelephone(row.getCell(1).getStringCellValue());

userCar.setUcCarcode(row.getCell(2).getStringCellValue());

String cartype=row.getCell(3).getStringCellValue();
if(cartype.equals("摩托车")){
userCar.setUcCartype(new BigDecimal(1));
}else{
userCar.setUcCartype(new BigDecimal(0));
}

userCar.setUcLicensecode(row.getCell(4).getStringCellValue());

SimpleDateFormat simpDf=new SimpleDateFormat("yyyy-MM-dd hh🇲🇲ss");
userCar.setUcRegtime(simpDf.format(row.getCell(5).getDateCellValue()));

String pay=row.getCell(6).getStringCellValue();
if(pay.equals("启用代付")){
userCar.setUcPay(new BigDecimal(0));
}else{
userCar.setUcPay(new BigDecimal(1));
}
excelWorkSheet.getData().add(userCar);
}
}
}
return excelWorkSheet.getData();
}

private Workbook createWorkBook(InputStream is) throws IOException {
if(excelFileFileName.toLowerCase().endsWith("xls")){
return new HSSFWorkbook(is);
}
if(excelFileFileName.toLowerCase().endsWith("xlsx")){
return new XSSFWorkbook(is);
}
return null;
}

}







加载中
0
情天大圣
情天大圣
空手套白狼啊,代码呢?
若天--
若天--
Workbook workbook=createWorkBook(new FileInputStream(excelFile)); Sheet sheet=workbook.getSheetAt(0); excelWorkSheet=new ExcelWorkSheet<UserCar>();
0
你要爪子
你要爪子
你导入有报什么错么?
若天--
若天--
<form action="usercar_importExcel.action" name="importForm" method="POST" enctype="multipart/form-data"> 导入文件:<input type="file" name="excelFile" id="excelFile"/>
你要爪子
你要爪子
回复 @凌兴 : 我感觉可能是上传的问题。可以贴下上传的代码。 我这边用poi导入excel。win下和linux下都是一样的代码,没特殊改变
若天--
若天--
本地是成功运行导入数据,但是在linux系统服务器里,好像连action都没进,
返回顶部
顶部