Java 如何使用Apache Poi从excel中读取complexnumber?
我想读取一个内容为complexnumber的xlsx文件。我使用apache poi。但我不知道如何使用此库从Excel中读取复数。复数作为字符串存储在Java 如何使用Apache Poi从excel中读取complexnumber?,java,apache-poi,complex-numbers,Java,Apache Poi,Complex Numbers,我想读取一个内容为complexnumber的xlsx文件。我使用apache poi。但我不知道如何使用此库从Excel中读取复数。复数作为字符串存储在Excel单元格中 例如,=COMPLEX(3,4)的结果值将是字符串3+4i。这也可以通过与其他字符串单元格值相同的方式通过ApachePOI读取 更好的方法是将数据格式化程序与公式化计算器结合使用 例如: 卓越: 爪哇: 问题是什么?您想在Java中对此做什么 import org.apache.poi.ss.usermodel.*;
Excel
单元格中
例如,=COMPLEX(3,4)
的结果值将是字符串3+4i
。这也可以通过与其他字符串单元格值相同的方式通过ApachePOI读取
更好的方法是将数据格式化程序
与公式化计算器
结合使用
例如:
卓越:
爪哇:
问题是什么?您想在Java
中对此做什么
import org.apache.poi.ss.usermodel.*;
import java.io.FileInputStream;
class ExcelReadCOMPLEX {
public static void main(String[] args) throws Exception {
DataFormatter formatter = new DataFormatter();
Workbook workbook = WorkbookFactory.create(new FileInputStream("COMPLEX.xlsx"));
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
Sheet sheet = workbook.getSheetAt(0);
Row row = sheet.getRow(1);
Cell cell;
for (int c = 0 ; c < 3; c++) {
cell = row.getCell(c);
System.out.println(cell.getCellFormula()); //COMPLEX(3,4), IMREAL(A2), IMAGINARY(A2)
System.out.println(formatter.formatCellValue(cell, evaluator)); //3+4i, 3, 4
}
String[] formulas = new String[]{"COMPLEX(5, -6)", "IMREAL(INDEX(A:A, ROW()))", "IMAGINARY(INDEX(A:A, ROW()))"};
row = sheet.createRow(2);
for (int c = 0 ; c < 3; c++) {
cell = row.createCell(c);
cell.setCellFormula(formulas[c]);
System.out.println(cell.getCellFormula());
System.out.println(formatter.formatCellValue(cell, evaluator)); //5-6i, 5, -6
}
workbook.close();
}
}
COMPLEX(3,4)
3+4i
IMREAL(A2)
3
IMAGINARY(A2)
4
COMPLEX(5, -6)
5-6i
IMREAL(INDEX(A:A, ROW()))
5
IMAGINARY(INDEX(A:A, ROW()))
-6