import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; 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; public class ImportExcel { public static void main(String[] args) { String filePath = "E:\\kemu.xls"; File myFile = new File(filePath); String strAdd = ""; try { FileInputStream fis = new FileInputStream(myFile); HSSFWorkbook workbook; workbook = new HSSFWorkbook(fis); HSSFSheet sheet = null; HSSFRow row = null; HSSFCell cell = null; SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd"); String strPrefix = sdf1.format(new Date()); if (workbook != null) { sheet = workbook.getSheetAt(0); } if (sheet == null) { System.out.println("不能导入空的Excel文件!"); } if (sheet != null) { row = sheet.getRow(5);// 从第五行开始读取 } for (int j = 5; row != null; j++, row = sheet.getRow(j)) { strAdd = ""; for (int index = 1; index <= 10; index++) { cell = row.getCell((short) (index - 1)); if (cell != null) { if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) { strAdd = cell.getStringCellValue(); } else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) { strAdd = String.valueOf(cell.getNumericCellValue()); }else if (cell.getCellType() == HSSFCell.CELL_TYPE_BLANK) { strAdd = ""; } } System.out.println(strAdd); } } } catch (FileNotFoundException e) { e.printStackTrace(); }catch( IOException ie ){ ie.printStackTrace(); } } }
JAVA 读Excel 表格中的数据.
最新推荐文章于 2022-07-27 18:36:41 发布