Java POI读取Excel的时候怎么按列读取

图片描述

Excel结构如图所示,我现在有两行数据,我要把这两行数据给对应起来.
第一行的数据,表示的是一个分类,第二行是这个分类下的子类.
比如性别分类有男和女,年龄段分类有青年,中年,老年.后面依次.
我现在读取Excel的时候,怎么才能把他们对应起来呢.
我的想法是按照列读取,但是POI好像没有按列读取的方法.
或者谁有更好的建议,提出来,帮帮我.

回答:

public static void main(String[] args) throws Exception {

InputStream is = new FileInputStream("d:/test.xls");

HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);

HSSFSheet sheetAt = hssfWorkbook.getSheetAt(0);

HSSFRow title = sheetAt.getRow(0);

HSSFCell cf = null;

HSSFCell c = null;

for (int rowNum = 1; rowNum <= sheetAt.getLastRowNum(); rowNum++) {

HSSFRow r = sheetAt.getRow(rowNum);

cf = title.getCell(0); //性别

c = r.getCell(0);//男

System.out.println(cf.getStringCellValue() + ":" + c.getStringCellValue());

c = r.getCell(1);//女

System.out.println(cf.getStringCellValue() + ":" + c.getStringCellValue());

cf = title.getCell(2); //年龄

c = r.getCell(2);//年轻

System.out.println(cf.getStringCellValue() + ":" + c.getStringCellValue());

c = r.getCell(3);//中年

System.out.println(cf.getStringCellValue() + ":" + c.getStringCellValue());

c = r.getCell(4);//老年

System.out.println(cf.getStringCellValue() + ":" + c.getStringCellValue());

}

}

图片描述

回答:

采纳的这个答案有一点小问题就是所有的数据都是写死的.我自己又折腾了一下,把数据写活了.代码如下

package org.linuxsogood.miscTest;

import java.io.File;

import java.io.FileInputStream;

import java.io.InputStream;

import java.util.HashMap;

import java.util.Map;

import java.util.Map.Entry;

import java.util.Random;

import org.apache.poi.ss.util.CellRangeAddress;

import org.apache.poi.xssf.usermodel.XSSFCell;

import org.apache.poi.xssf.usermodel.XSSFRow;

import org.apache.poi.xssf.usermodel.XSSFSheet;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class TestPOIP1Title {

public static void main(String[] args) throws Exception {

String pathname = "E:\\files\\title.xlsx";

File file = new File(pathname);

InputStream in = new FileInputStream(file);

//得到整个excel对象

XSSFWorkbook excel = new XSSFWorkbook(in);

//获取整个excel有多少个sheet

int sheets = excel.getNumberOfSheets();

//便利第一个sheet

Map<String,String> colMap = new HashMap<String, String>();

for(int i = 0 ; i < sheets ; i++ ){

XSSFSheet sheet = excel.getSheetAt(i);

if(sheet == null){

continue;

}

int mergedRegions = sheet.getNumMergedRegions();

XSSFRow row2 = sheet.getRow(0);

Map<Integer,String> category = new HashMap<Integer, String>();

for(int j = 0 ; j < mergedRegions; j++ ){

CellRangeAddress rangeAddress = sheet.getMergedRegion(j);

int firstRow = rangeAddress.getFirstColumn();

int lastRow = rangeAddress.getLastColumn();

category.put(rangeAddress.getFirstColumn(), rangeAddress.getLastColumn()+"-"+row2.getCell(firstRow).toString());

}

//便利每一行

for( int rowNum = 1 ; rowNum <= sheet.getLastRowNum() ; rowNum++ ){

System.out.println();

XSSFRow row = sheet.getRow(rowNum);

if(row == null){

continue;

}

short lastCellNum = row.getLastCellNum();

String cate = "";

Integer maxIndex = 0;

for( int col = row.getFirstCellNum() ; col < lastCellNum ; col++ ){

XSSFCell cell = row.getCell(col);

if(cell == null ){

continue;

}

if("".equals(cell.toString())){

continue;

}

int columnIndex = cell.getColumnIndex();

String string = category.get(columnIndex);

if(string != null && !string.equals("")){

String[] split = string.split("-");

cate = split[1];

maxIndex = Integer.parseInt(split[0]);

System.out.println(cate+"<-->"+cell.toString());

}else {

//如果当前便利的列编号小于等于合并单元格的结束,说明分类还是上面的分类名称

if(columnIndex<=maxIndex){

System.out.println(cate+"<-->"+cell.toString());

}else {

System.out.println("分类未知"+"<-->"+cell.toString());

}

}

}

}

}

}

}

运行的结果如下

性别<-->男

性别<-->女

年龄段<-->青年

年龄段<-->中年

年龄段<-->老年

年代情怀<-->60前

年代情怀<-->60后

年代情怀<-->70后

年代情怀<-->80后

年代情怀<-->90后

年代情怀<-->00后

行业<-->IT|通信|电子|互联网

行业<-->金融业

行业<-->房地产|建筑工程

行业<-->设计|印刷|艺术

行业<-->财务|人力资源|行政

行业<-->贸易|批发|零售|租赁业

行业<-->咨询|法律|教育|翻译

行业<-->销售|客服|市场

行业<-->生产|加工|制造

行业<-->交通|运输|物流|仓储

行业<-->服务业

行业<-->文化|传媒

行业<-->娱乐|体育

行业<-->能源|矿产|环保|科研

行业<-->政府|非盈利机构

行业<-->农|林|牧|渔

行业<-->学生

兴趣<-->音乐

兴趣<-->美食

兴趣<-->运动

兴趣<-->旅行

兴趣<-->摄影

兴趣<-->电影

兴趣<-->读书

兴趣<-->购物

兴趣<-->经管金融

兴趣<-->人文

兴趣<-->艺术

兴趣<-->娱乐

兴趣<-->游戏

兴趣<-->命理|运势|占卜

兴趣<-->居家

兴趣<-->时尚

兴趣<-->极客

兴趣<-->趣味

兴趣<-->自我管理

兴趣<-->教育

兴趣<-->设计

兴趣<-->动漫

兴趣<-->美容护肤

宗教信仰<-->佛教

宗教信仰<-->基督教

宗教信仰<-->伊斯兰教

岗位状态<-->自由职业

岗位状态<-->退休

婚恋状态<-->单身

婚恋状态<-->异地恋

婚恋状态<-->同城恋

婚恋状态<-->已婚

婚恋状态<-->离异

生育状态<-->已生育

生育状态<-->未生育

回答:

《使用POI按Excel标题动态读取列数据》http://www.mybry.com/?p=291

以上是 Java POI读取Excel的时候怎么按列读取 的全部内容, 来源链接: utcz.com/p/181727.html

回到顶部