/**
* 解析excel的每行数据
* 2014年6月30日 下午4:29:23
* @param args
* @throws FileNotFoundException
* @throws IOException
*/
public static void main(String[] args) throws FileNotFoundException, IOException {
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(new File("C:/Users/Administrator/Downloads/123.xls")));
HSSFSheet sheet = wb.getSheetAt(0);
for (Iterator<Row> rit = sheet.rowIterator(); rit.hasNext(); ) {
Row row = rit.next();
if (row.getRowNum() > 2) {
try {
for (Iterator<Cell> cit = row.cellIterator(); cit.hasNext(); ) {
Cell cell = cit.next();
switch(cell.getCellType()){
case Cell.CELL_TYPE_BOOLEAN:
//得到Boolean对象的方法
System.out.println("第"+cell.getRowIndex()+"行,第"+cell.getColumnIndex()+"列"+cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_NUMERIC:
//先看是否是日期格式
if(DateUtil.isCellDateFormatted(cell)){
//读取日期格式
System.out.println("第"+cell.getRowIndex()+"行,第"+cell.getColumnIndex()+"列"+cell.getDateCellValue());
}else{
//读取数字
System.out.println("第"+cell.getRowIndex()+"行,第"+cell.getColumnIndex()+"列"+cell.getNumericCellValue());
}
break;
case Cell.CELL_TYPE_FORMULA:
//读取公式
System.out.println("第"+cell.getRowIndex()+"行,第"+cell.getColumnIndex()+"列"+cell.getCellFormula());
break;
case Cell.CELL_TYPE_STRING:
//读取String
System.out.println("第"+cell.getRowIndex()+"行,第"+cell.getColumnIndex()+"列"+cell.getRichStringCellValue());
break;
}
}
} catch (Exception e) {
System.out.println("数据行为:"+(row.getRowNum()+1) +","+e.getMessage());
}
}
}
}
/**
* 获取sheet里所有的合并单元格
* 2014年6月25日 下午1:51:47 添加此方法
* @param sheet
* @return
*/
public static List<CellRangeAddress> getCombineCell(HSSFSheet sheet){
List<CellRangeAddress> list = new ArrayList<CellRangeAddress>();
//获得一个 sheet 中合并单元格的数量
int sheetmergerCount = sheet.getNumMergedRegions();
//遍历合并单元格
for(int i = 0; i<sheetmergerCount;i++)
{
//获得合并单元格加入list中
CellRangeAddress ca = sheet.getMergedRegion(i);
list.add(ca);
}
return list;
}
/**
* 判断单元格是否在合并单元格的范围内,若存在则返回地址
* @param listCombineCell 存放合并单元格的list
* @param cell 需要判断的单元格
* @param sheet sheet
* @return
*/
public static CellRangeAddress isCombineCell(List<CellRangeAddress> listCombineCell,Cell cell) throws Exception{
int firstC = 0;
int lastC = 0;
int firstR = 0;
int lastR = 0;
CellRangeAddress cra = null;
boolean isInMergedRegion = false;
for(CellRangeAddress ca:listCombineCell){
//获得合并单元格的起始行, 结束行, 起始列, 结束列
firstC = ca.getFirstColumn();
lastC = ca.getLastColumn();
firstR = ca.getFirstRow();
lastR = ca.getLastRow();
if(cell.getRowIndex() >= firstR && cell.getRowIndex() <= lastR){
if(cell.getColumnIndex() >= firstC && cell.getColumnIndex() <= lastC){
isInMergedRegion = true;
}
}
if (isInMergedRegion) {
cra = ca;
break;
}
}
return cra;
}
/**
*
* 生成HSSFWorkbook
* @return
*/
public static void produceExcel() {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("sheet");
//设置字体对象
HSSFFont headfont = wb.createFont();
headfont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 加粗
//居中样式
HSSFCellStyle style = wb.createCellStyle(); // 样式对象
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setWrapText(true);//设置自动换行
style.setFont(headfont);//字体设置
//创建第一行
HSSFRow row0 = sheet.createRow(0);
row0.setHeight((short)(2*256));
//合并单元格,0, 0, 0, 5分别代表第一行,最后一行,第一列,最后一列
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 5));
HSSFCell row0Cell0 = row0.createCell(0);//创建单元格
row0Cell0.setCellValue("生成excel");//复制
row0Cell0.setCellStyle(style);//给单元格设置样式
}
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 5));这个是合并单元的方法,需要注意的是重复合并单元格时 ,生成的excel 之后,合并的单元格会有蓝色的背景,并且在解析excel中有几个合并单元格时,这种类型会判定为两个合并单元格。