poi导入导出excel文件,兼容.xls和.xlsx两种格式
2015-07-08 15:49
886 查看
这个是测试类:
package com.fishroad.util; import java.io.File; import java.io.FileInputStream; import java.math.BigDecimal; import java.text.SimpleDateFormat; import javax.swing.JOptionPane; import org.apache.poi.hssf.usermodel.HSSFDateUtil; 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 ImportExcel { private final static String XLS = "xls"; private final static String XLSX = "xlsx"; public static void main(String[] args) { String path = UtilTools.getDirPath(); String extensionName = UtilTools.getFileExtName(path); // System.out.println(extensionName); Workbook workbook = null; try { if (extensionName.toLowerCase().equals(XLS)) { workbook = new HSSFWorkbook(new FileInputStream(new File(path))); } else if (extensionName.toLowerCase().equals(XLSX)) { workbook = new XSSFWorkbook(new FileInputStream(new File(path))); } Sheet sheet = workbook.getSheetAt(0); int minRowIx = sheet.getFirstRowNum(); int maxRowIx = sheet.getLastRowNum(); for (int rowIx = minRowIx; rowIx <= maxRowIx; rowIx++) { Row row = sheet.getRow(rowIx); //Cell cell = row.getCell(0); int minCellIx = row.getFirstCellNum(); int maxCellIx = row.getLastCellNum(); for(int cellIx = minCellIx;cellIx<=maxCellIx;cellIx++){ Cell cell = row.getCell(cellIx); if (cell != null) { String a = getValue(cell); if(a==null || "".equals(a)){ System.out.println("---******----"); } System.out.println(a); }else{ System.out.println(cellIx+"---******----"); } } /*if (cell != null) { String a = cell.getStringCellValue(); System.out.println(a); }*/ } } catch (Exception e) { JOptionPane.showMessageDialog(null, "导入文件出错!"); } } private static String getValue(Cell hssfCell) { if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) { // 返回布尔类型的值 return String.valueOf(hssfCell.getBooleanCellValue()); } else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) { if(HSSFDateUtil.isCellDateFormatted(hssfCell)){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); return sdf.format(HSSFDateUtil.getJavaDate(hssfCell.getNumericCellValue())).toString(); } // 返回数值类型的值(记得将数字类型的值转化为BigDecimal,不然的话,取出来会带有小数点,切记!!!!!) return String.valueOf(new BigDecimal(hssfCell.getNumericCellValue()) ); } else { // 返回字符串类型的值 return String.valueOf(hssfCell.getStringCellValue()); } } }UtilTools.java是一个工具类,主要用到了下面两个方法:
/** * swing页面形式的选择文件存储位置 * * @return */ public static String getDirPath() { JFileChooser parseDir = new JFileChooser(); parseDir.setAcceptAllFileFilterUsed(true); parseDir.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); int result = parseDir.showOpenDialog(null); if (result == JFileChooser.APPROVE_OPTION) { System.out.println(parseDir.getSelectedFile().getAbsolutePath()); return parseDir.getSelectedFile().getAbsolutePath(); } else { return ""; } }
/** * 获取文件的后缀名 * * @param filePath * @return */ public static String getFileExtName(String filePath) { File f = new File(filePath); String fileName = f.getName(); String prefix = fileName.substring(fileName.lastIndexOf(".") + 1); System.out.println(prefix); return prefix; }
相关文章推荐
- 每天一个linux命令(5):rm 命令
- unity3d相机跟随物体平滑移动(C#代码)
- AndroidManifest.xml——path-permission
- 训练:建立和使用一个集群配置
- python六核心编程——条件和循环
- Algorithm --> 6174问题
- (剑指Offer)面试题9:斐波那契数列
- 台大机器学习基石笔记(二)——VC 维1
- ReactiveCocoa - iOS开发的新框架
- 1 A+B Problem
- C#设计模式学习之【单例模式】
- HTML基础介绍二(图像和超级链接)
- android学习路线 1.2 Android开发权威指南 所学之对已用处
- ExtJs4 笔记(4) Ext.XTemplate 模板
- ROWNUM 与 ROW_NUMBER()OVER() 的区别
- 命令行下打开bmp文件的命令
- sql建自增序列号
- 【中文分词】亲手开发一款中文分词器--前言
- 查看MYSQL数据库中所有用户及拥有权限
- 【168】ENVI入门系列