poi批量导入excel
1.先下载poi依赖的包,复制张贴代码一定没有错误
2.直接上代码,如果导入有错误,可能是别人给你发的excel模版有错误,可以自己创建一个excel测试
public static void getDataFromExcel(String filePath) throws Exception
\{
Workbook wookbook = null;
//判断是否为excel类型文件
if(!filePath.endsWith(".xls")&&!filePath.endsWith(".xlsx"))
\{
System.out.println("文件不是excel类型");
\}
FileInputStream fis =null;
try
\{
//获取一个绝对地址的流
fis = new FileInputStream(filePath);
\}
catch(Exception e)
\{
e.printStackTrace();
\}
try
\{
//2003版本的excel,用.xls结尾
wookbook = new HSSFWorkbook(fis);//得到工作簿
\}
catch (Exception ex)
\{
//ex.printStackTrace();
try
\{
//2007版本的excel,用.xlsx结尾
wookbook = new XSSFWorkbook(fis);//得到工作簿
\} catch (IOException e)
\{
// TODO Auto-generated catch block
e.printStackTrace();
\}
\}
//得到一个工作表
Sheet sheet = wookbook.getSheetAt(0);
//获得数据的总行数
int totalRowNum = sheet.getLastRowNum();
String code="";
String name="";
ImportExcelUtil importExcelUtil = new ImportExcelUtil();
for (int i = 1; i <= totalRowNum; i++) \{
// 获得第i行对象
Row row = sheet.getRow(i);
// 获得获得第i行第0列的 String类型对象
Cell cell = row.getCell((short) 0);
code=importExcelUtil.getCellValue(cell).toString();
cell = row.getCell((short) 1);
name=importExcelUtil.getCellValue(cell).toString();
System.out.println(code+"---"+name);
\}
\}
3.测试方法
public static void main(String[] args) {
try {
getDataFromExcel(“E:\\test.xls”);
} catch (Exception e) {
e.printStackTrace();
}
}
4.ImportExcelUtil工具类
public class ImportExcelUtil {
private final static String excel2003L =”.xls”; //2003- 版本的excel
private final static String excel2007U =”.xlsx”; //2007+ 版本的excel
/\*\*
\* 描述:获取IO流中的数据,组装成List<List<Object>>对象
\* @param in,fileName
\* @throws IOException
\*//\*
public List<List<Object>> getBankListByExcel(InputStream in,String fileName) throws Exception\{
List<List<Object>> list = null;
POIFSFileSystem fs = new POIFSFileSystem(in);
HSSFWorkbook work = new HSSFWorkbook(fs);
HSSFSheet sheet= null;
HSSFRow row=null;
Cell cell = null;
//户主 电话 身份证 有无房屋 房屋缴费类型 房屋面积(m²) 房屋单价:m²/元 有无车位 车位费 车位缴费类型 单元(栋)
list = new ArrayList<List<Object>>();
//遍历Excel中所有的sheet
for (int i = 0; i < work.getNumberOfSheets(); i++) \{
sheet = work.getSheetAt(i);
if(sheet==null)\{continue;\}
//遍历当前sheet中的所有行 从第6行开始遍历
for (int j = 6; j <=sheet.getLastRowNum(); j++)\{
row = sheet.getRow(j);
//遍历所有的列 row.getFirstCellNum()
List<Object> li = new ArrayList<Object>();
for (int y =0 ; y < row.getLastCellNum(); y++) \{
if(y>=15) break;
cell = row.getCell(y);
if(cell!=null)
\{
cell.setCellType(Cell.CELL\_TYPE\_STRING);//设置列值类型
if(StringUtils.isEmpty(cell.getStringCellValue().trim().replaceAll(" ", "")))
\{
cell.setCellValue("0");
\}else
\{
cell.setCellValue(cell.getStringCellValue());
\}
li.add(this.getCellValue(cell));
\}
cell=null;
\}
if(li.size()>=15)
\{
list.add(li);
\}
\}
\}
//work.close();
return list;
\} \*/
/\*\*
\* 描述:根据文件后缀,自适应上传文件的版本
\* @param inStr,fileName
\* @return
\* @throws Exception
\*/
public Workbook getWorkbook(InputStream inStr,String fileName) throws Exception\{
Workbook wb = null;
String fileType = fileName.substring(fileName.lastIndexOf("."));
if(excel2003L.equals(fileType))\{
wb = new HSSFWorkbook(inStr); //2003-
\}else if(excel2007U.equals(fileType))\{
wb = new XSSFWorkbook(inStr); //2007+
\}else\{
throw new Exception("解析的文件格式有误!");
\}
return wb;
\}
/\*\*
\* 描述:对表格中数值进行格式化
\* @param cell
\* @return
\*/
public Object getCellValue(Cell cell)\{
Object value = null;
DecimalFormat df = new DecimalFormat("0"); //格式化number String字符
SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd"); //日期格式化
DecimalFormat df2 = new DecimalFormat("0.00"); //格式化数字
switch (cell.getCellType()) \{
case Cell.CELL\_TYPE\_STRING:
value = cell.getRichStringCellValue().getString();
break;
case Cell.CELL\_TYPE\_NUMERIC:
if("General".equals(cell.getCellStyle().getDataFormatString()))\{
value = df.format(cell.getNumericCellValue());
\}else if("m/d/yy".equals(cell.getCellStyle().getDataFormatString()))\{
value = sdf.format(cell.getDateCellValue());
\}else\{
value = df2.format(cell.getNumericCellValue());
\}
break;
case Cell.CELL\_TYPE\_BOOLEAN:
value = cell.getBooleanCellValue();
break;
case Cell.CELL\_TYPE\_BLANK:
value = "";
break;
default:
break;
\}
return value;
\}
public static void main(String\[\] args) \{
\}
}
5.excel模版,我这个只读取了第一列和第二列的数据
还没有评论,来说两句吧...