Java POI读取Excel的时候怎么按列读取
展开全部
按列读取的方法:
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());
}
}
}
}
}
}
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());
}
}
}
}
}
}
TableDI
2024-07-18 广告
2024-07-18 广告
在Excel表格中筛选数据是一个简单而强大的功能,能够帮助用户快速定位所需信息。要筛选数据,首先确保数据已按列整齐排列。接着,点击表格顶部你想要筛选的列标题,选择“数据”选项卡中的“筛选”功能。此时,列标题上会出现一个下拉箭头,点击它,在下...
点击进入详情页
本回答由TableDI提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |