如何获得用Java使用Apache POI
读取的“ Book1.xls”中的公式类型单元格中存储的文本?
这是我的在单元格中设置公式的代码:
JTextArea test = new javax.swing.JTextArea();
InputStream fis = new FileInputStream("D:\\StudyWell\\Book1.xls");
POIFSFileSystem fs = new POIFSFileSystem( fis );
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
int count = sheet.getLastRowNum();
for(int j =0; j<=count; j++){
HSSFRow row=sheet.getRow(j);
HSSFCell cell=row.getCell(0);
HSSFRichTextString rts=cell.getRichStringCellValue();
String text = rts.toString();
an.append("\n");
an.setEditable(false);
a2.append(",");
a2.setEditable(false);
String otext = "A"+String.valueOf(j+1);
String atext = "E"+String.valueOf(j+1);
String formula = "SUBSTITUTE"+"("+otext+","+atext+","+"\"______________\""+")";
System.out.println(formula);
HSSFCell cell_temp = row.createCell(2);
cell_temp.setCellFormula(formula);
for (int i=1; i<rts.length(); i++) {
HSSFFont font = wb.getFontAt(rts.getFontAtIndex(i));
if(font.getUnderline()==1){
String s = "";
s = s + String.valueOf(text.charAt(i));
test.setText(s);
a2.append(test.getText());
an.append(test.getText());
}
}
}
String aforprint = a2.getText();
for(int a=0; a<=count; a++){
HSSFRow row=sheet.getRow(a);
HSSFCell cell_temp2 = row.createCell(4);
String[] temp3 = aforprint.split(",");
List<String> fixedlen = Arrays.asList(temp3);
ArrayList<String> alist = new ArrayList<String>(fixedlen);
if(alist.get(0).equals("")){
alist.remove(0);
}
String celltext = String.valueOf(alist.get(a));
cell_temp2.setCellValue(celltext);
}
try {
FileOutputStream out = new FileOutputStream(new File("D:\\StudyWell\\Book1.xls"));
wb.write(out);
out.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
} catch(Exception e) { e.printStackTrace(); } }
您可以看到,单元格Substitute(A1, E1)
中有一个公式C1
,]。>
现在,我想获取以Java字符串形式存储在C1
中的文本。
这是我尝试过的:
JTextArea test = new javax.swing.JTextArea(); InputStream fis = new FileInputStream("D:\\StudyWell\\Book1.xls"); POIFSFileSystem fs = new POIFSFileSystem( fis ); HSSFWorkbook wb = new HSSFWorkbook(fs); DataFormatter formatter = new DataFormatter(); HSSFSheet sheet = wb.getSheetAt(0); int count = sheet.getLastRowNum(); for(int j =0; j<=count; j++){ HSSFRow row=sheet.getRow(j); HSSFCell cellatc=row.getCell(2); //String rts = cellatc.getStringCellValue(); //HSSFRichTextString rts = cellatc.getRichStringCellValue(); //String text = rts.toString(); String rts = formatter.formatCellValue(cellatc); HSSFCell cellata = row.getCell(0); cellata.setCellValue(rts); cellatc.setCellValue(""); } try { FileOutputStream out = new FileOutputStream(new File("D:\\StudyWell\\Book1.xls")); wb.write(out); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } catch(Exception e) { e.printStackTrace(); }
这样做得到
OutOfMemory: Insufficient Java Heap Size error
,我将Java堆大小增加到3024MB。即使这样做,问题仍然存在。
还有其他方法可以完成此任务吗?请帮助,TIA!
我如何使用Apache POI在Java读取的“ Book1.xls”中的公式类型单元格中存储文本?这是在单元格中设置公式的代码:JTextArea test = new javax.swing.JTextArea(); ...
您需要使用ExcelExtractor
类,例如