Je rencontre un problème dans Excel lorsque j'utilise Apache POI. Je peux lire sur plusieurs lignes, mais parfois je suis dans une situation où j'aimerais lire uniquement une colonne particulière.
Ainsi, il est possible de lire une colonne particulière telle que uniquement la colonne "A" ou la colonne "C" uniquement.
J'utilise le langage Java pour cela.
heikkim a raison, voici un exemple de code adapté d'un code que j'ai:
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 ...
}
}
}
Pour la colCount
, utilisez quelque chose comme 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.
}
Ma solution, un peu plus simple du point de vue du code.
D'accord, d'après votre question, vous voulez simplement lire une colonne en particulier. Ainsi, en parcourant une ligne puis ses cellules, vous pouvez simplement vérifier l’index de la colonne.
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"
}
}
}
J'utilise POI 3.12-- 'org.Apache.poi: poi: 3.12' J'espère que cela vous aidera. À votre santé!
Vous pouvez simplement boucler les lignes et lire la même cellule de chaque ligne (cela ne comprend-il pas une colonne?).
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();
}
}
Ce code fait 3 choses:
Voici le code pour lire les données Excel par colonne.
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;
}