工作中用到的导入excel一个方法,你还可以通过一些插件导入,代码要你自己了,基本原理如下...
public Object importDoucument(MultipartFile uploadfile)
{
StringBuffer resultMessage = new StringBuffer();
ExcelImport excelImport = new ExcelImport();
Sheet sheet = null;
try
{
// 验证文件格式 如不出错 返回工作簿
excelImport.verifyExeclFile(uploadfile);
ExcelBean excelBean = excelImport.getExcelBean();
if (null != excelBean)
{
sheet = excelBean.getSheet();
}
//导入excel文件分析整理出list对象
List
int num = 0;
if(dataList.size()>0){
for (StcCoreElements itemStcVO : dataList)
{
StcCoreElements stcCoreElementsVo = nitemStcVO
//*****修改些处
this.save(stcCoreElementsVo);
++num;
}
}
resultMessage.append("已成功导入 "+num+" 条核心要素信息");
}
catch (Exception e)
{
resultMessage.append(e.getMessage());
e.printStackTrace();
}
finally
{
excelImport.close();
}
return resultMessage;
}
private List
{
int[] cateRange = new int[2];
List
int lastRowNumber = sheet.getLastRowNum();
Row cateRow = sheet.getRow(rowNum - 1);
Cell cateCell = cateRow.getCell(0);
String cateCellValue = ImportExcelUtil.getCellValue(cateCell, sheet);
if (StringUtils.isNotBlank(cateCellValue))
{
if (StringUtils.startsWith(cateCellValue, startName))
{
cateRange[0] = rowNum + titleRowNum;
}
}
String currentCellValue0 = "";
do
{
Row currentRow = sheet.getRow(rowNum);
StcCoreElements info =new StcCoreElements();
Cell currentCell0 = currentRow.getCell(0);
currentCellValue0 = ImportExcelUtil.getCellValue(currentCell0, sheet);
info.setOverallPlan(currentCellValue0);
dataList.add(info);
rowNum++;
} while (rowNum <= lastRowNumber);
return dataList;
}