我想用java来读取Excel文件的某行某列,就是指定读取某个位置的数据,然后显示出来!怎么弄?求
2016-06-29
展开全部
工作中用到的导入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<StcCoreElements> dataList = getAssessCateRange(sheet, "战略要素名称", "战略要素名称", 2, 1);
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<StcCoreElements> getAssessCateRange(Sheet sheet, String startName, String endName, int rowNum, int titleRowNum)
{
int[] cateRange = new int[2];
List<StcCoreElements> dataList = new ArrayList<StcCoreElements>();
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;
}
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<StcCoreElements> dataList = getAssessCateRange(sheet, "战略要素名称", "战略要素名称", 2, 1);
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<StcCoreElements> getAssessCateRange(Sheet sheet, String startName, String endName, int rowNum, int titleRowNum)
{
int[] cateRange = new int[2];
List<StcCoreElements> dataList = new ArrayList<StcCoreElements>();
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;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询