JAVA使用POI读取EXCEL文件的简单model
来源:互联网 发布:录入数据打字平台 编辑:程序博客网 时间:2024/04/30 13:32
- package poi;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Iterator;
- import org.apache.poi.hssf.usermodel.HSSFCell;
- import org.apache.poi.hssf.usermodel.HSSFWorkbook;
- import org.apache.poi.ss.usermodel.Cell;
- import org.apache.poi.ss.usermodel.Row;
- import org.apache.poi.ss.usermodel.Sheet;
- import org.apache.poi.ss.usermodel.Workbook;
- import org.apache.poi.xssf.usermodel.XSSFWorkbook;
- public class ReadExcel001 {
- public static void main(String[] args) {
- readXml(”D:/test.xlsx”);
- System.out.println(”————-“);
- readXml(”d:/test2.xls”);
- }
- public static void readXml(String fileName){
- boolean isE2007 = false; //判断是否是excel2007格式
- if(fileName.endsWith(“xlsx”))
- isE2007 = true;
- try {
- InputStream input = new FileInputStream(fileName); //建立输入流
- Workbook wb = null;
- //根据文件格式(2003或者2007)来初始化
- if(isE2007)
- wb = new XSSFWorkbook(input);
- else
- wb = new HSSFWorkbook(input);
- Sheet sheet = wb.getSheetAt(0); //获得第一个表单
- Iterator<Row> rows = sheet.rowIterator(); //获得第一个表单的迭代器
- while (rows.hasNext()) {
- Row row = rows.next(); //获得行数据
- System.out.println(”Row #” + row.getRowNum()); //获得行号从0开始
- Iterator<Cell> cells = row.cellIterator(); //获得第一行的迭代器
- while (cells.hasNext()) {
- Cell cell = cells.next();
- System.out.println(”Cell #” + cell.getColumnIndex());
- switch (cell.getCellType()) { //根据cell中的类型来输出数据
- case HSSFCell.CELL_TYPE_NUMERIC:
- System.out.println(cell.getNumericCellValue());
- break;
- case HSSFCell.CELL_TYPE_STRING:
- System.out.println(cell.getStringCellValue());
- break;
- case HSSFCell.CELL_TYPE_BOOLEAN:
- System.out.println(cell.getBooleanCellValue());
- break;
- case HSSFCell.CELL_TYPE_FORMULA:
- System.out.println(cell.getCellFormula());
- break;
- default:
- System.out.println(”unsuported sell type”);
- break;
- }
- }
- }
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- }
package poi;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.util.Iterator;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.usermodel.Sheet;import org.apache.poi.ss.usermodel.Workbook;import org.apache.poi.xssf.usermodel.XSSFWorkbook;public class ReadExcel001 { public static void main(String[] args) { readXml("D:/test.xlsx"); System.out.println("-------------"); readXml("d:/test2.xls"); } public static void readXml(String fileName){ boolean isE2007 = false; //判断是否是excel2007格式 if(fileName.endsWith("xlsx")) isE2007 = true; try { InputStream input = new FileInputStream(fileName); //建立输入流 Workbook wb = null; //根据文件格式(2003或者2007)来初始化 if(isE2007) wb = new XSSFWorkbook(input); else wb = new HSSFWorkbook(input); Sheet sheet = wb.getSheetAt(0); //获得第一个表单 Iterator<Row> rows = sheet.rowIterator(); //获得第一个表单的迭代器 while (rows.hasNext()) { Row row = rows.next(); //获得行数据 System.out.println("Row #" + row.getRowNum()); //获得行号从0开始 Iterator<Cell> cells = row.cellIterator(); //获得第一行的迭代器 while (cells.hasNext()) { Cell cell = cells.next(); System.out.println("Cell #" + cell.getColumnIndex()); switch (cell.getCellType()) { //根据cell中的类型来输出数据 case HSSFCell.CELL_TYPE_NUMERIC: System.out.println(cell.getNumericCellValue()); break; case HSSFCell.CELL_TYPE_STRING: System.out.println(cell.getStringCellValue()); break; case HSSFCell.CELL_TYPE_BOOLEAN: System.out.println(cell.getBooleanCellValue()); break; case HSSFCell.CELL_TYPE_FORMULA: System.out.println(cell.getCellFormula()); break; default: System.out.println("unsuported sell type"); break; } } } } catch (IOException ex) { ex.printStackTrace(); } }}
阅读全文
0 0
- JAVA使用POI读取EXCEL文件的简单model
- JAVA使用POI读取EXCEL文件的简单model
- JAVA使用POI读取EXCEL文件的简单model
- 使用java poi 读取excel文件
- 【java】使用POI读取excel文件内容
- Java使用poi读取Excel文件例子
- Java使用POI读取Excel文件
- java使用POI实现Excel文件的读取
- 使用POI读取EXCEL文件
- 使用POI读取EXCEL文件
- 使用POI读取excel文件
- java poi读取excel文件
- Java--POI读取excel文件
- java poi读取excel文件
- java 读取Excel文件(POI)
- java POI 读取EXCEL文件
- java使用poi读取excel
- 如何创建和读取excel文件-poi的简单使用(一)
- leetcode 7. Reverse Integer
- Qt:Application failed to start because platform plugin “windows” is missing.
- PHP基础
- html第九课时,圆柱体体积
- 解决上传图片预览时获取绝对路径问题
- JAVA使用POI读取EXCEL文件的简单model
- winform自定义下拉框
- Java 简介
- leetcode 第15题 3Sum
- Scala函数式编程课后习题答案(第五章)
- echarts 饼图hover效果,饼图中间显示自定义信息
- Tomcat启用HTTPS协议配置过程
- Yocto工具链编译Busybox
- html第九课时,js显示helloworld