java中poi怎么获取指定列的行数

java中poi怎么获取指定列的行数,第1张

读取excel中的数据,先加载好excel表,然后逐行逐列读取即可。

对于上面的行数不一样,其实行数是一样的,只是下面两行的C列数据为空而已,代码中加上判断即可。

示例代码如下:

for(int rowIndex=0;rowIndex<sheetgetPhysicalNumberOfRows();rowIndex++)

{

Row row = sheetgetRow(rowIndex);

if(row == null) continue;

for(int cellIndex=0;cellIndex<rowgetPhysicalNumberOfCells();cellIndex++)

{

Cell content = rowgetCell(cellIndex);

if(content== null) continue; //判断空

//对于内容进行 *** 作

}

}

有问题欢迎提问,,谢谢!

请将单行row提取为数组的相应方法里,将数组长度增加一位,如 :

new String[rgetLastCellNum()+1];这样就不会重复调用一次少一列的问题了,原因可能是

poi以最后的NULL,做结尾吧,你试一下null不会出现在表格里。我已经试过!

一、excel读取的两种方式

Java中解析excel的方式,我目前知道的有两种,一种是 jxl 读取,另一种是 poi 读取

11 jxl 和 poi 的区别和选择

① jxl 只能解析 xls 文件,不能 解析 xlsx 文件; poi 则是可以同时兼容xls 和xlsx两种文件类型,这是要注意的第一个点;

② 这两个方法的读取方式不一样,jxl 读取的是 先读列 然后循环获取的该列每行的信息。poi 读取是 先读行,再循环获取每列的信息。如下图:

package comfounderassetstestcontroller;

import javaioFileNotFoundException;

import javaioFileOutputStream;

import javaioIOException;

import javaioOutputStream;

import javautilArrayList;

import javautilList;

import javaxxmlparsersDocumentBuilder;

import javaxxmlparsersDocumentBuilderFactory;

import javaxxmlparsersParserConfigurationException;

import orgapachepoihssfusermodelHSSFCell;

import orgapachepoihssfusermodelHSSFRichTextString;

import orgapachepoihssfusermodelHSSFRow;

import orgapachepoihssfusermodelHSSFSheet;

import orgapachepoihssfusermodelHSSFWorkbook;

import orgw3cdomDocument;

import orgw3cdomNode;

import orgw3cdomNodeList;

import orgxmlsaxSAXException;

public class POI2Excel {

private static List<String> columnNameList = new ArrayList<String>();

 private static List<String> columnDescList = new ArrayList<String>();

 

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

  String fileName = "E:\\uploadTestWord\\columnsxml";

  parserXml(fileName);

  xlsDto2Excel();

 }

 

 public static void parserXml(String fileName) {

  try {

   DocumentBuilderFactory dbf = DocumentBuilderFactorynewInstance();

   DocumentBuilder db = dbfnewDocumentBuilder();

   Document document = dbparse(fileName);

   NodeList employees = documentgetChildNodes();

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

    Node employee = employeesitem(i);

    NodeList employeeInfo = employeegetChildNodes();

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

     Node node = employeeInfoitem(j);

     NodeList employeeMeta = nodegetChildNodes();

     for (int k = 0; k < employeeMetagetLength(); k++) {

      if(employeeMetaitem(k)getNodeName()equals("column_name")){

       columnNameListadd(employeeMetaitem(k)getTextContent());

      }else if(employeeMetaitem(k)getNodeName()equals("column_desc")){

       columnDescListadd(employeeMetaitem(k)getTextContent());

      }

      Systemoutprintln(employeeMetaitem(k)getNodeName()

      + ":" + employeeMetaitem(k)getTextContent());

     }

    }

   }

   Systemoutprintln("解析完毕");

   } catch (FileNotFoundException e) {

    Systemoutprintln(egetMessage());

   } catch (ParserConfigurationException e) {

    Systemoutprintln(egetMessage());

   } catch (SAXException e) {

    Systemoutprintln(egetMessage());

   } catch (IOException e) {

    Systemoutprintln(egetMessage());

  }

 }

 public static void xlsDto2Excel() throws Exception {

  int CountColumnNum = columnDescListsize();

    HSSFWorkbook hwb = new HSSFWorkbook();

//         XlsDto xlsDto = null;

         // sheet 对应一个工作页

         HSSFSheet sheet = hwbcreateSheet("pldrxkxxmb");

         HSSFRow firstrow = sheetcreateRow(0); // 下标为0的行开始

         HSSFCell[] firstcell = new HSSFCell[CountColumnNum];

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

             firstcell[j] = firstrowcreateCell(j);

             firstcell[j]setCellValue(new HSSFRichTextString(columnDescListget(j)));

         }

      // 创建文件输出流,准备输出电子表格

         OutputStream out = new FileOutputStream("E:\\uploadTestWord\\pldrxkxxmbxls");

         hwbwrite(out);

         outclose();

         Systemoutprintln("数据库导出成功");

 }

}

------------------------------------------------------------------------

经测试,可以运行,注意jar包的引入工程

附件是执行后的结果

以上就是关于java中poi怎么获取指定列的行数全部的内容,包括:java中poi怎么获取指定列的行数、JAVA中用POI生成excel表A之后,再用POI读取表A,会少一列,但是复制A表后再粘贴后命名为B,读取B表正常、怎么获取excel字符间列java等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9495769.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-29
下一篇2023-04-29

发表评论

登录后才能评论

评论列表(0条)

    保存