Apache POI - Вопрос по выводу значений ячеек
....................
D5 - C34
C34
....................
Почему выводится сама формула (=C34) , стоящая в ячейке D5, а не число, которое видно в ячейке D5 если открыть сам файл?
Как сделать чтобы выводилось само число?
Код:
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.*;
public class POITest {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("C:/Test/1.xlsm");
XSSFWorkbook workbook = new XSSFWorkbook(fis);
XSSFSheet sheet = workbook.getSheetAt(1);
CellReference ref = new CellReference("D5");
int row = ref.getRow();
int col = ref.getCol();
Cell cell = sheet.getRow(row).getCell(col);
gettingTheCellContents(ref, cell);
fis.close();
workbook.close();
}
// https://poi.apache.org/spreadsheet/quick-guide.html#CellContents
private static void gettingTheCellContents(CellReference cellRef, Cell cell) {
DataFormatter formatter = new DataFormatter();
System.out.print(cellRef.formatAsString());
System.out.print(" - ");
// get the text that appears in the cell by getting the cell value and applying any data formats (Date, 0.00, 1.23e9, $1.23, etc)
String text = formatter.formatCellValue(cell);
System.out.println(text);
// Alternatively, get the value and format it yourself
switch (cell.getCellTypeEnum()) {
case STRING:
System.out.println(cell.getRichStringCellValue().getString());
break;
case NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
System.out.println(cell.getDateCellValue());
} else {
System.out.println(cell.getNumericCellValue());
}
break;
case BOOLEAN:
System.out.println(cell.getBooleanCellValue());
break;
case FORMULA:
System.out.println(cell.getCellFormula());
break;
case BLANK:
System.out.println();
break;
default:
System.out.println();
}
}
}
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.*;
public class POITest {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("C:/Test/1.xlsm");
XSSFWorkbook workbook = new XSSFWorkbook(fis);
XSSFSheet sheet = workbook.getSheetAt(1);
CellReference ref = new CellReference("D5");
int row = ref.getRow();
int col = ref.getCol();
Cell cell = sheet.getRow(row).getCell(col);
gettingTheCellContents(ref, cell);
fis.close();
workbook.close();
}
// https://poi.apache.org/spreadsheet/quick-guide.html#CellContents
private static void gettingTheCellContents(CellReference cellRef, Cell cell) {
DataFormatter formatter = new DataFormatter();
System.out.print(cellRef.formatAsString());
System.out.print(" - ");
// get the text that appears in the cell by getting the cell value and applying any data formats (Date, 0.00, 1.23e9, $1.23, etc)
String text = formatter.formatCellValue(cell);
System.out.println(text);
// Alternatively, get the value and format it yourself
switch (cell.getCellTypeEnum()) {
case STRING:
System.out.println(cell.getRichStringCellValue().getString());
break;
case NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
System.out.println(cell.getDateCellValue());
} else {
System.out.println(cell.getNumericCellValue());
}
break;
case BOOLEAN:
System.out.println(cell.getBooleanCellValue());
break;
case FORMULA:
System.out.println(cell.getCellFormula());
break;
case BLANK:
System.out.println();
break;
default:
System.out.println();
}
}
}