我在Excel中有同时使用Apache POI的问题。 我可以跨行读取,但有时我在的情况下,我想只能读取特定列。
因此,它是可以读取任何特定列象只“A”仅列或仅列“C”。
我为此使用Java语言。
我在Excel中有同时使用Apache POI的问题。 我可以跨行读取,但有时我在的情况下,我想只能读取特定列。
因此,它是可以读取任何特定列象只“A”仅列或仅列“C”。
我为此使用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 ...
}
}
}
对于colCount
使用像row.getPhysicalNumberOfCells()
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.
}
我的解决方案,简单一点的代码明智。
好了,从你的问题,你只是单纯的想读特定列。 因此,在遍历一排,然后在它的细胞,你可以简单地检查列的索引。
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"
}
}
}
我使用POI 3.12-- 'org.apache.poi:POI:3.12' 希望它可以帮助。 干杯!
你可以只环行和每一行(不包括此列?)读取相同的细胞。
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();
}
}
此代码3件事情:
下面是列读取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;
}
请注意,通过使用行细胞迭代器(列迭代Iterator<Cell> cellIterator = row.cellIterator();
)可能导致沉默跳过列。 我刚刚遇到了一个揭露这种行为的文件。
在一个使用索引for循环和使用迭代row.getCell(i)
未跳过列并在正确的列索引被返回的值。