java poi导入EXCEL xls文件代码
2014-01-24 17:54
246 查看
/** * */ package com.bn.car.common.report.excel; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import com.bn.car.biz.supply.dto.PartsInfoDTO; /** * @author huangjing * @date 2014-1-24 */ public class XlsMain { public static void main(String[] args) throws IOException { try { XlsMain xlsMain = new XlsMain(); PartsInfoDTO dto = null; List<PartsInfoDTO> list = xlsMain.readXls("d://parts2.xls"); // System.out.println(list.size()); for (int i = 0; i < list.size(); i++) { dto = list.get(i); System.out.println(dto.getPartsCode() +"--"+dto.getPartsName() +"--"+dto.getStoreNum() +"--"+dto.getCostPrice() +"--"+dto.getMarketPrice() +"--"+dto.getRetailPrice() +"--"+dto.getWeight() +"--"+dto.getUpdown()); } System.out.println("OK!!"); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } /** * 读取xls文件内容 * * @return List<XlsDto>对象 * @throws IOException * 输入/输出(i/o)异常 */ public static List<PartsInfoDTO> readXls(String xlspath) throws IOException { InputStream is = new FileInputStream(xlspath); HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is); PartsInfoDTO dto = null; List<PartsInfoDTO> list = new ArrayList<PartsInfoDTO>(); // 循环工作表Sheet for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) { HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet); // System.out.println("hssfSheet:" + hssfSheet); if (hssfSheet == null) { continue; } // 循环行Row,从第一行开始。 for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) { HSSFRow hssfRow = hssfSheet.getRow(rowNum); // System.out.println("hssfRow:"+hssfRow); if (hssfRow == null) { continue; } dto = new PartsInfoDTO(); // 循环列Cell // 0学号 1姓名 2学院 3课程名 4 成绩 // for (int cellNum = 0; cellNum <=4; cellNum++) { HSSFCell cell = hssfRow.getCell(0); // System.out.println("cell:" + cell); if (cell == null) { continue; } dto.setPartsCode(getValue(cell)); cell = hssfRow.getCell(1); // System.out.println("cell1:" + cell); if (cell == null) { continue; } dto.setPartsName(getValue(cell)); cell = hssfRow.getCell(2); // System.out.println("cell2:" + cell); if (cell == null) { continue; } dto.setStoreNum(Float.valueOf(getValue(cell)).intValue()); cell = hssfRow.getCell(3); // System.out.println("cell3:" + cell); if (cell == null) { continue; } dto.setCostPrice(Float.valueOf(getValue(cell))); cell = hssfRow.getCell(4); // System.out.println("cell4:" + cell); if (cell == null) { continue; } dto.setMarketPrice(Float.parseFloat(getValue(cell))); cell = hssfRow.getCell(5); // System.out.println("cell5:" + cell); if (cell == null) { continue; } dto.setRetailPrice(Float.parseFloat(getValue(cell))); cell = hssfRow.getCell(6); // System.out.println("cell6:" + cell); if (cell == null) { continue; } dto.setWeight(Float.parseFloat(getValue(cell))); cell = hssfRow.getCell(7); //写成8了,取不到,所以不执行下面的代码~!! // System.out.println("cell7:" + cell); if (cell == null) { continue; } if(getValue(cell).equals("是")){ dto.setUpdown(1); }else{ dto.setUpdown(0); } // System.out.println("上下架:"+dto.getUpdown()); list.add(dto); } } // System.out.println("SIZE:" + list.size()); return list; } /** * 得到Excel表中的值 * * @param hssfCell * Excel中的每一个格子 * @return Excel中每一个格子中的值 */ @SuppressWarnings("static-access") public static String getValue(HSSFCell hssfCell) { if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) { // 返回布尔类型的值 return String.valueOf(hssfCell.getBooleanCellValue()); } else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) { // 返回数值类型的值 return String.valueOf(hssfCell.getNumericCellValue()); } else { // 返回字符串类型的值 return String.valueOf(hssfCell.getStringCellValue()); } } }
parts2.xls (导入模板文件~!)
相关文章推荐
- java poi导出EXCEL xls文件代码
- 【JavaWeb开发】使用java实现简单的Excel文件的导入与导出(POI)
- JAVA代码自动生成.xls和.xlsx类型的excel文件的详细过程和代码
- java poi 导出.xls的Excel报表 IE11导出文件名称后缀是.action,待修复
- java:poi读取excel文件,分xls和xlsx
- java学习之--导入导出excel文件 通过poi
- java poi将excel文件导入
- 使用POI实现在java程序中导入导出Excel文件数据
- java导入Excel包含多张sheet表的.xls .xslx .txt三种后缀格式的文件
- java poi 读取Excel文件,进行导入
- java 导入Excel 文件,兼容Excel 2003(后缀名:xls)及 2007(后缀名:xlsx)的文件,同时还支持csv格式的文件
- java poi Excel文件导入
- java中用poi导入excel文件到数据库总结
- java poi技术将Excel文件内容导入数据库
- JAVA 通过poi 读取 本地excel(.xls,.xlsx)文件,文件中 包含图片
- poi导入导出excel文件,兼容.xls和.xlsx两种格式
- java代码导入excel数据至oracle(poi方式)
- 一个将数据文件转换成excel文件打印的java实现方法的代码片断(Struts+poi)
- java使用POI读取excel文件,兼容xls和xlsx
- Excel学习4_ java poi 读取有合并单元格的EXCEL文件