Java 使用ApachePOI如何读取特定excel列
使用ApachePOI时excel中出现问题。我可以跨行阅读,但有时我只想阅读特定的列 因此,是否可以读取任何特定列,如仅“A”列或仅“C”列Java 使用ApachePOI如何读取特定excel列,java,excel,apache-poi,Java,Excel,Apache Poi,使用ApachePOI时excel中出现问题。我可以跨行阅读,但有时我只想阅读特定的列 因此,是否可以读取任何特定列,如仅“A”列或仅“C”列 我使用的是Java语言。您可以循环行并从每行读取相同的单元格(这不是一个列吗?)heikkim是对的,下面是一些根据我的一些代码改编的示例代码: import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.
我使用的是Java语言。您可以循环行并从每行读取相同的单元格(这不是一个列吗?)heikkim是对的,下面是一些根据我的一些代码改编的示例代码:
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Row;
...
for (int rowIndex = 0; rowIndex <= sheet.getLastRowNum(); rowIndex++) {
row = sheet.getRow(rowIndex);
if (row != null) {
Cell cell = row.getCell(colIndex);
if (cell != null) {
// Found column and there is value in the cell.
cellValueMaybeNull = cell.getStringCellValue();
// Do something with the cellValueMaybeNull here ...
// break; ???
}
}
}
import org.apache.poi.ss.usermodel.Cell;
导入org.apache.poi.ss.usermodel.Sheet;
导入org.apache.poi.ss.usermodel.Row;
...
对于(int rowIndex=0;rowIndex,以下是按列读取excel数据的代码
public ArrayList<String> extractExcelContentByColumnIndex(int columnIndex){
ArrayList<String> columndata = null;
try {
File f = new File("sample.xlsx")
FileInputStream ios = new FileInputStream(f);
XSSFWorkbook workbook = new XSSFWorkbook(ios);
XSSFSheet sheet = workbook.getSheetAt(0);
Iterator<Row> rowIterator = sheet.iterator();
columndata = new ArrayList<>();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
if(row.getRowNum() > 0){ //To filter column headings
if(cell.getColumnIndex() == columnIndex){// To match column index
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
columndata.add(cell.getNumericCellValue()+"");
break;
case Cell.CELL_TYPE_STRING:
columndata.add(cell.getStringCellValue());
break;
}
}
}
}
}
ios.close();
System.out.println(columndata);
} catch (Exception e) {
e.printStackTrace();
}
return columndata;
}
public ArrayList extractExcelContentByColumnIndex(int-columnIndex){
ArrayList columndata=null;
试一试{
文件f=新文件(“sample.xlsx”)
FileInputStream ios=新的FileInputStream(f);
XSSF工作簿=新XSSF工作簿(ios);
XSSFSheet sheet=workbook.getSheetAt(0);
迭代器rowIterator=sheet.Iterator();
columndata=新的ArrayList();
while(roweiterator.hasNext()){
行=行迭代器。下一步();
迭代器cellIterator=row.cellIterator();
while(cellIterator.hasNext()){
Cell=cellIterator.next();
如果(row.getRowNum()>0){//要筛选列标题
如果(cell.getColumnIndex()==columnIndex){//匹配列索引
开关(cell.getCellType()){
case Cell.Cell\u类型\u数值:
columndata.add(cell.getNumericCellValue()+);
打破
case Cell.Cell\u类型\u字符串:
add(cell.getStringCellValue());
打破
}
}
}
}
}
ios.close();
System.out.println(columndata);
}捕获(例外e){
e、 printStackTrace();
}
返回列数据;
}
好的,从你的问题来看,你只需要阅读一个特定的列。因此,当你在一行上迭代,然后在它的单元格上迭代时,你可以简单地检查列的索引
Iterator<Row> rowIterator = mySheet.iterator(); // Traversing over each row of XLSX file
while (rowIterator.hasNext()) {
Row row = rowIterator.next(); // For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
println "column index"+cell.getColumnIndex()//You will have your columns fixed in Excel file
if(cell.getColumnIndex()==3)//for example of c
{
print "done"
}
}
}
Iterator rowditerator=mySheet.Iterator();//遍历XLSX文件的每一行
while(roweiterator.hasNext()){
Row Row=rowIterator.next();//对于每一行,遍历每一列
迭代器cellIterator=row.cellIterator();
while(cellIterator.hasNext()){
Cell=cellIterator.next();
println“column index”+cell.getColumnIndex()//将在Excel文件中修复列
if(cell.getColumnIndex()==3)//例如c
{
打印“完成”
}
}
}
我使用的是POI 3.12-“org.apache.POI:POI:3.12”
希望有帮助。干杯
Sheet sheet = workBook.getSheetAt(0); // Get Your Sheet.
for (Row row : sheet) { // For each Row.
Cell cell = row.getCell(0); // Get the Cell at the Index / Column you want.
}
我的解决方案在代码方面稍微简单一些。import java.io.*;
import java.io.*;
import org.apache.poi.hssf.util.CellReference;
import org.apache.poi.ss.usermodel.*;
import java.text.*;
public class XSLXReader {
static DecimalFormat df = new DecimalFormat("#####0");
public static void main(String[] args) {
FileWriter fostream;
PrintWriter out = null;
String strOutputPath = "H:\\BLR_Team\\Kavitha\\Excel-to-xml\\";
String strFilePrefix = "Master_5.2-B";
try {
InputStream inputStream = new FileInputStream(new File("H:\\BLR_Team\\Kavitha\\Excel-to-xml\\Stack-up 20L pure storage 11-0039-01 ISU_USA-A 1-30-17-Rev_exm.xls"));
Workbook wb = WorkbookFactory.create(inputStream);
// Sheet sheet = wb.getSheet(0);
Sheet sheet =null;
Integer noOfSheets= wb.getNumberOfSheets();
for(int i=0;i<noOfSheets;i++){
sheet = wb.getSheetAt(i);
System.out.println("Sheet : "+i + " " + sheet.getSheetName());
System.out.println("Sheet : "+i + " " + sheet.getFirstRowNum());
System.out.println("Sheet : "+i + " " + sheet.getLastRowNum());
//Column 29
fostream = new FileWriter(strOutputPath + "\\" + strFilePrefix+i+ ".xml");
out = new PrintWriter(new BufferedWriter(fostream));
out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
out.println("<Bin-code>");
boolean firstRow = true;
for (Row row : sheet) {
if (firstRow == true) {
firstRow = false;
continue;
}
out.println("\t<DCT>");
out.println(formatElement("\t\t", "ID", formatCell(row.getCell(0))));
out.println(formatElement("\t\t", "Table_name", formatCell(row.getCell(1))));
out.println(formatElement("\t\t", "isProddaten", formatCell(row.getCell(2))));
out.println(formatElement("\t\t", "isR3P01Data", formatCell(row.getCell(3))));
out.println(formatElement("\t\t", "LayerNo", formatCell(row.getCell(29))));
out.println("\t</DCT>");
}
CellReference ref = new CellReference("A13");
Row r = sheet.getRow(ref.getRow());
if (r != null) {
Cell c = r.getCell(ref.getCol());
System.out.println(c.getRichStringCellValue().getString());
}
for (Row row : sheet) {
for (Cell cell : row) {
CellReference cellRef = new CellReference(row.getRowNum(), cell.getColumnIndex());
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
System.out.println(cell.getRichStringCellValue().getString());
break;
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
System.out.println(cell.getDateCellValue());
} else {
System.out.println(cell.getNumericCellValue());
}
break;
case Cell.CELL_TYPE_BOOLEAN:
System.out.println(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_FORMULA:
System.out.println(cell.getCellFormula());
break;
case Cell.CELL_TYPE_BLANK:
System.out.println();
break;
default:
System.out.println();
}
}
}
out.write("</Bin-code>");
out.flush();
out.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static String formatCell(Cell cell)
{
if (cell == null) {
return "";
}
switch(cell.getCellType()) {
case Cell.CELL_TYPE_BLANK:
return "";
case Cell.CELL_TYPE_BOOLEAN:
return Boolean.toString(cell.getBooleanCellValue());
case Cell.CELL_TYPE_ERROR:
return "*error*";
case Cell.CELL_TYPE_NUMERIC:
return XSLXReader.df.format(cell.getNumericCellValue());
case Cell.CELL_TYPE_STRING:
return cell.getStringCellValue();
default:
return "<unknown value>";
}
}
private static String formatElement(String prefix, String tag, String value) {
StringBuilder sb = new StringBuilder(prefix);
sb.append("<");
sb.append(tag);
if (value != null && value.length() > 0) {
sb.append(">");
sb.append(value);
sb.append("</");
sb.append(tag);
sb.append(">");
} else {
sb.append("/>");
}
return sb.toString();
}
}
导入org.apache.poi.hssf.util.CellReference;
导入org.apache.poi.ss.usermodel.*;
导入java.text.*;
公共类XSLXReader{
静态DecimalFormat df=新的DecimalFormat(“0”);
公共静态void main(字符串[]args){
文件写入程序流;
PrintWriter out=null;
String strOutputPath=“H:\\BLR\u Team\\Kavitha\\Excel to xml\\”;
字符串strFilePrefix=“Master_5.2-B”;
试一试{
InputStream InputStream=新文件InputStream(新文件(“H:\\BLR\U团队\\Kavitha\\Excel到xml\\Stack up 20L纯存储11-0039-01 ISU\U USA-A 1-30-17-Rev_exm.xls”);
工作簿wb=WorkbookFactory.create(inputStream);
//Sheet Sheet=wb.getSheet(0);
Sheet=null;
整数noOfSheets=wb.getNumberOfSheets();
对于(int i=0;i 0){
某人加上(“>”);
附加(价值);
某人加上(“”);
}否则{
某人加上(“/>”);
}
使某人返回字符串();
}
}
此代码有3个功能:
Excel到XML文件生成。工程名称Dong Kim
打印特定单元格的内容:A13
还要将excel内容打印为正常文本格式。要导入的.jar:poi-3.9.jar、poi-ooxml-3.9.jar、poi-ooxml-schemas-3.9.jar、xbean-2.3.0.jar,xmlbeans-xmlpublic-2.4.0.jar,dom4j-1.5.jar
请注意,使用行单元格迭代器(iterator cellIterator=row.cellIterator();
)遍历列可能会导致无提示跳过列。我刚刚遇到一个文档公开了这种行为
在for循环中使用索引并使用row.getCell(i)进行迭代
没有跳过列,而是在正确的列索引处返回值。谢谢Brian的回答。.是的,但是如果col1填充了4个单元格,而col5填充了6个单元格,我会感到困惑,因为我正在将这些数据输入到测试输入值。那么,您能告诉我如何找到列中填充的单元格数吗…请帮助谢谢Christophe..但我有一个疑问..我在col1中有4个单元格填充,但在col3中有8个单元格填充,所以当我运行代码时,它将运行8次,但我只需要col1中的4个单元格,我只需要在特定列中填充单元格的数量,因为我正在将此计数和数据输入测试的输入..请再次帮助感谢您的时间和代码…只需跳过单元格中具有now值的行即可?我添加了一个break
,在这种情况下忘记它是一个伟大的经典:)您可以直接使用cell=row.getCell(theColIndexYouWant);
,而不是我们