Java POI读取Excel 2003/2007/2010例子
来源:互联网 发布:pi数据库 edna 编辑:程序博客网 时间:2024/06/05 18:40
http://blog.csdn.net/zht666/article/details/11598711#comments
Java读写Excel的包是Apache POI(项目地址:http://poi.apache.org/),因此需要先获取POI的jar包,本实验使用的是POI 3.9稳定版。
Apache POI 代码例子地址:http://poi.apache.org/spreadsheet/quick-guide.html
本例子可以读取Microsoft Office Excel 2003/2007/2010,具体代码及注释如下:
读取“.xls”格式使用 import org.apache.poi.hssf.usermodel.*;包的内容,例如:HSSFWorkbook
读取“.xlsx”格式使用 import org.apache.poi.xssf.usermodel.*; 包的内容,例如:XSSFWorkbook
读取两种格式使用 import org.apache.poi.ss.usermodel.* 包的内容,例如:Workbook
引入包如下:
- 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.ss.usermodel.WorkbookFactory;
- import org.apache.poi.ss.usermodel.DateUtil;
- /**
- * 读取Excel测试,兼容 Excel 2003/2007/2010
- */
- public String readExcel()
- {
- SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
- try {
- //同时支持Excel 2003、2007
- File excelFile = new File("/home/zht/test.xls"); //创建文件对象
- FileInputStream is = new FileInputStream(excelFile); //文件流
- Workbook workbook = WorkbookFactory.create(is); //这种方式 Excel 2003/2007/2010 都是可以处理的
- int sheetCount = workbook.getNumberOfSheets(); //Sheet的数量
- //遍历每个Sheet
- for (int s = 0; s < sheetCount; s++) {
- Sheet sheet = workbook.getSheetAt(s);
- int rowCount = sheet.getPhysicalNumberOfRows(); //获取总行数
- //遍历每一行
- for (int r = 0; r < rowCount; r++) {
- Row row = sheet.getRow(r);
- int cellCount = row.getPhysicalNumberOfCells(); //获取总列数
- //遍历每一列
- for (int c = 0; c < cellCount; c++) {
- Cell cell = row.getCell(c);
- int cellType = cell.getCellType();
- String cellValue = null;
- switch(cellType) {
- case Cell.CELL_TYPE_STRING: //文本
- cellValue = cell.getStringCellValue();
- break;
- case Cell.CELL_TYPE_NUMERIC: //数字、日期
- if(DateUtil.isCellDateFormatted(cell)) {
- cellValue = fmt.format(cell.getDateCellValue()); //日期型
- }
- else {
- cellValue = String.valueOf(cell.getNumericCellValue()); //数字
- }
- break;
- case Cell.CELL_TYPE_BOOLEAN: //布尔型
- cellValue = String.valueOf(cell.getBooleanCellValue());
- break;
- case Cell.CELL_TYPE_BLANK: //空白
- cellValue = cell.getStringCellValue();
- break;
- case Cell.CELL_TYPE_ERROR: //错误
- cellValue = "错误";
- break;
- case Cell.CELL_TYPE_FORMULA: //公式
- cellValue = "错误";
- break;
- default:
- cellValue = "错误";
- }
- System.out.print(cellValue + " ");
- }
- System.out.println();
- }
- }
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- return Action.SUCCESS;
- }
0 0
- Java POI读取Excel 2003/2007/2010例子
- Java POI读取Excel 2003/2007/2010例子
- Java POI读取Excel 2003/2007/2010例子
- Java POI读取Excel 2003/2007/2010例子
- Java POI读取Excel 2003/2007/2010例子
- Java POI读取Excel 2003/2007/2010例子
- java POI读取Excel 2003/2007/2010
- Java POI读取excel的例子
- Java使用poi读取Excel文件例子
- java--poi 读取Excel 兼容2003/2007
- JAVA POI读取Office excel (2003,2007)
- java POI读取excel 2007/2003
- Java POI读取Office excel (2003,2007)
- java的poi技术读取Excel[2003-2007,2010]
- java的poi技术读取Excel[2003-2007,2010]
- Java的poi技术读取Excel[2003-2007,2010]
- Java的poi技术读取Excel[2003-2007,2010]
- poi读取excel封装例子
- mysql教程
- github常见操作和常见错误!错误提示:fatal: remote origin already exists
- 大数据爬虫基础(四)MAVEN的安装配置和使用(下)--JAVA简单爬虫
- Eclipse编写jsp文件连接Mysql数据库的安装配置指南
- win7升级到win10所遇到的坑
- Java POI读取Excel 2003/2007/2010例子
- spring开发 MethodInvokingFactoryBean的学习
- 神经网络
- Qt下使用GDB远程调试ARM板
- 手势识别
- oracle 五种约束 常用单组分组函数
- RecyclerView的Adapter的抽取
- offsetof' was not declared in this scope
- 100+ 超全的web开发工具和资源