Java Excelファイルのアップロードデータのインポート
5095 ワード
Controllerでformフォームのコミットを受信するファイルドメイン:
public Map importConsumer(@RequestParam("file") MultipartFile file)
Excelツールクラスを読み込むここではConsumerエンティティクラスで書きます.皆さんは自分でカプセル化することができます.
注意:java.lang.NoClassDefFoundError:org/apache/commons/collections 4/ListValuedMap
これはjarパッケージが欠けています.commons-collections 4-4.1.jarをダウンロードしてlibの下に置いて、build pathをプロジェクトに追加する必要があります.
ループexcel列はすべてStringタイプに変換する必要があります.row.getCell(0).setCellType(CellType.STRING);
基礎コードは、自分で拡張する必要があります.
public Map importConsumer(@RequestParam("file") MultipartFile file)
Excelツールクラスを読み込むここではConsumerエンティティクラスで書きます.皆さんは自分でカプセル化することができます.
public class ReadExcel
{
//
private int totalRows = 0;
//
private int totalCells = 0;
//
private String errorMsg;
//
public ReadExcel()
{
}
//
public int getTotalRows()
{
return totalRows;
}
//
public int getTotalCells()
{
return totalCells;
}
//
public String getErrorInfo()
{
return errorMsg;
}
/**
* EXCEL
*
* @param filePath
* @return
*/
public boolean validateExcel(String filePath)
{
if(filePath == null || !(WDWUtil.isExcel2003(filePath) || WDWUtil.isExcel2007(filePath)))
{
errorMsg = " excel ";
return false;
}
return true;
}
/**
* EXCEL ,
*
* @param fielName
* @return
*/
public List getExcelInfo(String fileName, MultipartFile Mfile)
{
// spring MultipartFile CommonsMultipartFile
CommonsMultipartFile cf = (CommonsMultipartFile) Mfile; //
File file = new File("D:\\fileupload");
// ( File , 。)
if(!file.exists())
file.mkdirs();
//
File file1 = new File("D:\\fileupload" + new Date().getTime() + ".xlsx");
//
try
{
cf.getFileItem().write(file1);
}
catch (Exception e)
{
e.printStackTrace();
}
//
List customerList = new ArrayList();
//
InputStream is = null;
try
{
//
if(!validateExcel(fileName))
{
return null;
}
// 2003 2007
boolean isExcel2003 = true;
if(WDWUtil.isExcel2007(fileName))
{
isExcel2003 = false;
}
//
is = new FileInputStream(file1);
// excel
customerList = getExcelInfo(is, isExcel2003);
is.close();
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if(is != null)
{
try
{
is.close();
}
catch (IOException e)
{
is = null;
e.printStackTrace();
}
}
}
return customerList;
}
/**
* excel
*
* @param is
*
* @param isExcel2003
* excel 2003 2007
* @return
* @throws IOException
*/
public List getExcelInfo(InputStream is, boolean isExcel2003)
{
List customerList = null;
try
{
/** Workbook */
Workbook wb = null;
// excel 2003
if(isExcel2003)
{
wb = new HSSFWorkbook(is);
}
else
{// excel 2007
wb = new XSSFWorkbook(is);
}
// Excel
customerList = readExcelValue(wb);
}
catch (IOException e)
{
e.printStackTrace();
}
return customerList;
}
/**
* Excel
* @param wb
* @return
*/
private List readExcelValue(Workbook wb){
// shell
Sheet sheet=wb.getSheetAt(0);
// Excel
this.totalRows=sheet.getPhysicalNumberOfRows();
// Excel ( )
if(totalRows>=1 && sheet.getRow(0) != null){
this.totalCells=sheet.getRow(0).getPhysicalNumberOfCells();
}
List customerList=new ArrayList();
Consumer consumer;
// Excel , 。
for(int r=1;r
Serviceで呼び出されます.public Map importConsumer(MultipartFile file,Users currentUser)
{
Map map = new HashMap();
boolean b = false;
// EXCEL
ReadExcel readExcel = new ReadExcel();
// excel, 。
List ConsumerList = readExcel.getExcelInfo(file.getOriginalFilename(), file);
if(ConsumerList != null)
{
b = true;
map.put("code", 0);
}
else
{
map.put("code", -1);
}
map.put("msg", "");
for(Consumer consumer:ConsumerList)
{
String guid = java.util.UUID.randomUUID().toString();
consumer.setId(guid);
consumer.setCompanyId(currentUser.getCompanyId());
consumer.setCreateTime(formatter.format(new Date()));
consumerDao.insert(consumer); //
}
return map;
}
注意:java.lang.NoClassDefFoundError:org/apache/commons/collections 4/ListValuedMap
これはjarパッケージが欠けています.commons-collections 4-4.1.jarをダウンロードしてlibの下に置いて、build pathをプロジェクトに追加する必要があります.
cell.getStringCellValue()
このコードは異常を報告する可能性もあり、取得したセルのデータはStringタイプであり、セルのデータは他のタイプのデータである可能性もあります.ループexcel列はすべてStringタイプに変換する必要があります.row.getCell(0).setCellType(CellType.STRING);
基礎コードは、自分で拡張する必要があります.