用poi怎么读取文件?求一个案例?
解决方案 »
- SSH集成问题 2011-9-9 22:17:11 org.apache.catalina.core.StandardContext filterStart
- SSH整合中关于Hibernate实现级联删除和不实现级联删除
- 使用struts和hibernate完成一个注册
- spring的jdbcTemplate问题
- jsp页面中的jsp代码是何时被执行的
- JBPM的SIGNAVIO没法生成JPDL文件的问题
- 关于webwork上传问题~高手进~~在线等~
- Hibernate的入门问题
- J2EE可以做什么?
- 有个问题,请高手帮我看看
- 如何翻页、、急急急
- 邮件客户端在有新邮件来的时候,会自动弹出一个对话框,而不用自己去接邮件怎么实现
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;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 POIExcelUtil { private int totalRows = 0;// 总行数
private int totalCells = 0;// 总列数 public POIExcelUtil() {
} // 根据文件名读取excel文件
public List<ArrayList<String>> read(String fileName) {
List<ArrayList<String>> dataLst = new ArrayList<ArrayList<String>>(); // 检查文件名是否为空或者是否是Excel格式的文件
if (fileName == null || !fileName.matches("^.+\\.(?i)((xls)|(xlsx))$")) {
return dataLst;
} boolean isExcel2003 = true;
// 对文件的合法性进行验
if (fileName.matches("^.+\\.(?i)(xlsx)$")) {
isExcel2003 = false;
} // 检查文件是否存在
File file = new File(fileName);
if (file == null || !file.exists()) {
return dataLst;
} try {
// 读取excel
dataLst = read(new FileInputStream(file), isExcel2003);
} catch (Exception ex) {
ex.printStackTrace();
}
return dataLst;
} // 根据流读取Excel文件
public List<ArrayList<String>> read(InputStream inputStream, boolean isExcel2003) {
List<ArrayList<String>> dataLst = null;
try {
// 根据版本选择创建Workbook的方式
Workbook wb = isExcel2003 ? new HSSFWorkbook(inputStream) : new XSSFWorkbook(inputStream);
dataLst = read(wb);
} catch (IOException e) {
e.printStackTrace();
}
return dataLst;
} // 得到总行数
public int getTotalRows() {
return totalRows;
} // 得到总列数
public int getTotalCells() {
return totalCells;
} // 读取数据
private List<ArrayList<String>> read(Workbook wb) {
List<ArrayList<String>> dataLst = new ArrayList<ArrayList<String>>(); Sheet sheet = wb.getSheetAt(0);// 得到第一个shell
this.totalRows = sheet.getPhysicalNumberOfRows();
if (this.totalRows >= 1 && sheet.getRow(0) != null) {
this.totalCells = sheet.getRow(0).getPhysicalNumberOfCells();
} // 循环Excel的行
for (int r = 0; r < this.totalRows; r++) {
Row row = sheet.getRow(r);
if (row == null) {
continue;
} ArrayList<String> rowLst = new ArrayList<String>(); // 循环Excel的列
for (short c = 0; c < this.getTotalCells(); c++) {
Cell cell = row.getCell(c);
String cellValue = "";
if (cell == null) {
rowLst.add(cellValue);
continue;
} // 处理数字型的,自动去零
if (Cell.CELL_TYPE_NUMERIC == cell.getCellType()) { // 在excel里,日期也是数字,在此要进行判断
if (HSSFDateUtil.isCellDateFormatted(cell)) {
cellValue = DateUtil.get4yMdHms(cell.getDateCellValue());
} else {
cellValue = getRightStr(cell.getNumericCellValue() + "");
}
} else if (Cell.CELL_TYPE_STRING == cell.getCellType()) {// 处理字符串型
cellValue = cell.getStringCellValue();
} else if (Cell.CELL_TYPE_BOOLEAN == cell.getCellType()) {// 处理布尔型
cellValue = cell.getBooleanCellValue() + "";
} else {// 其它数据类型
cellValue = cell.toString() + "";
} rowLst.add(cellValue);
}
dataLst.add(rowLst);
}
return dataLst;
} // 正确地处理整数后自动加零的情况
private String getRightStr(String sNum) {
DecimalFormat decimalFormat = new DecimalFormat("#.000000");
String resultStr = decimalFormat.format(new Double(sNum));
if (resultStr.matches("^[-+]?\\d+\\.[0]+$")) {
resultStr = resultStr.substring(0, resultStr.indexOf("."));
}
return resultStr;
} public static void main(String[] args) throws Exception { String fileName = "e:\\2012.xls";//读取2003的OK
//String fileName = "e:\\2013.xlsx";// 读取 2007的OK
POIExcelUtil excelUtil = new POIExcelUtil();
List<ArrayList<String>> dataLst = excelUtil.read(fileName);// 读取excle,并把数据打包成list System.out.println(" --------------------------excel内容如下--------------------------------"); for (ArrayList<String> innerLst : dataLst) {
StringBuffer rowData = new StringBuffer();
for (String dataStr : innerLst) {
rowData.append(",").append(dataStr);
}
if (rowData.length() > 0) {
System.out.println(" " + rowData.deleteCharAt(0).toString());
}
} System.out.println(" 总行数:" + excelUtil.getTotalRows() + " , 总列数:" + excelUtil.getTotalCells()); }
}
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;public class DateUtil {
//得到当前的时间
public static Date getDate() {
Calendar canlendar = Calendar.getInstance();
return canlendar.getTime();
}
//提到指定的millis得到时间
public static Date getDate(long millis) {
Calendar canlendar = Calendar.getInstance();
canlendar.clear();
canlendar.setTimeInMillis(millis);
return canlendar.getTime();
} public static long getMillis() {
return Calendar.getInstance().getTimeInMillis();
} //得到指定日期的字符串(yyyy-MM-dd HH:mm:ss.SSS)
public static String getDateFormate(Date date, String formate) {
try {
SimpleDateFormat simpleDateFormate = new SimpleDateFormat(formate);
return simpleDateFormate.format(date);
} catch (Exception e) {
}
return "";
} //根据日期得到YYYY-MM-DD HH:MM:SS.SSS格式字符串
public static String get4yMdHmsS(Date date) {
return DateUtil.getDateFormate(date, "yyyy-MM-dd HH:mm:ss.SSS");
} //根据日期得到YYYY-MM-DD HH:MM:SS格式字符串
public static String get4yMdHms(Date date) {
return DateUtil.getDateFormate(date, "yyyy-MM-dd HH:mm:ss");
} //根据日期得到YYYY-MM-DD HH:MM格式字符串
public static String get4yMdHm(Date date) {
return DateUtil.getDateFormate(date, "yyyy-MM-dd HH:mm");
} //根据日期得到YYYY-MM-DD格式字符串
public static String get4yMd(Date date) {
return DateUtil.getDateFormate(date, "yyyy-MM-dd");
}
//把指定字符(yyyy-MM-dd HH:mm:ss.SSS)串转成Date
public static Date parse4yMdHmsS(String sDate) {
return DateUtil.parseDate(sDate, "yyyy-MM-dd HH:mm:ss.SSS");
} //把指定字符(yyyy-MM-dd HH:mm:ss)串转成Date
public static Date parse4yMdHms(String sDate) {
return DateUtil.parseDate(sDate, "yyyy-MM-dd HH:mm:ss");
} //把指定字符(yyyy-MM-dd HH:mm)串转成Date
public static Date parse4yMdHm(String sDate) {
return DateUtil.parseDate(sDate, "yyyy-MM-dd HH:mm");
} //把指定字符(yyyy-MM-dd)串转成Date
public static Date parse4yMd(String sDate) {
return DateUtil.parseDate(sDate, "yyyy-MM-dd");
} //根据指定格式,把字符串转成日期
public static Date parseDate(String sDate, String formate) {
SimpleDateFormat simpleDateFormate = new SimpleDateFormat(formate);
try {
return simpleDateFormate.parse(sDate);
} catch (ParseException e) {
return null;
}
} //两个长整型的时间相差(时间的毫秒数),可以得到指定的毫秒数,秒数,分钟数,天数
public static double getDifTwoTime(Date minuendTime, Date subtrahendTime, String tdatestr) {
if (minuendTime == null || subtrahendTime != null) {
return DateUtil.getDifTwoTime(minuendTime.getTime(), subtrahendTime.getTime(), tdatestr);
}
return 0;
} //两个长整型的时间相差(时间的毫秒数),可以得到指定的毫秒数,秒数,分钟数,天数
public static double getDifTwoTime(long minuendTime, long subtrahendTime, String tdatestr) {
if (tdatestr == null || tdatestr.equals("")) {
tdatestr = "MS";
}
double temp = 1;
/** 毫秒数 */
if ("MS".equalsIgnoreCase(tdatestr)) {
temp = 1;
}
/** 得到秒 */
if ("S".equalsIgnoreCase(tdatestr)) {
temp = 1000;
}
/** 得到分 */
if ("M".equalsIgnoreCase(tdatestr)) {
temp = 1000 * 60;
}
/** 得到小时 */
if ("H".equalsIgnoreCase(tdatestr)) {
temp = 1000 * 60 * 60;
}
/** 得到天 */
if ("D".equalsIgnoreCase(tdatestr)) {
temp = 1000 * 60 * 60 * 24;
}
return (minuendTime - subtrahendTime) / temp;
} //从日期中得到指定部分(YYYY/MM/DD/HH/SS/SSS)数字
public static int getPartOfTime(Date date, String part) {
Calendar canlendar = Calendar.getInstance();
canlendar.clear();
canlendar.setTime(date);
if (part.equalsIgnoreCase("Y")) {//得到年
return canlendar.get(Calendar.YEAR);
}
if (part.equalsIgnoreCase("M")) {//得到月
return canlendar.get(Calendar.MONTH) + 1;
}
if (part.equalsIgnoreCase("D")) {//得到日
return canlendar.get(Calendar.DAY_OF_MONTH);
}
if (part.equalsIgnoreCase("H")) {//得到时
return canlendar.get(Calendar.HOUR_OF_DAY);
}
if (part.equalsIgnoreCase("M")) {//得到分
return canlendar.get(Calendar.MINUTE);
}
if (part.equalsIgnoreCase("S")) {//得到秒
return canlendar.get(Calendar.SECOND);
}
if (part.equalsIgnoreCase("MS")) {//得到毫秒
return canlendar.get(Calendar.MILLISECOND);
}
return -1;
}
}
需要的jar包:
dom4j-1.6.1.jar
poi-3.6.jar
poi-ooxml-3.6.jar
poi-ooxml-schemas-3.6.jar
xmlbeans-2.3.0.jar
http://blog.csdn.net/kings988/article/details/5865882