package poidefect; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.util.CellUtil; public class PoiDefect { public static void main(String[] args) { String formula1 = "IFERROR(VLOOKUP(A1,$A$2:$B$2,2,FALSE),\"\")"; String formula2 = "IF(ISERROR(VLOOKUP(A1,$A$2:$B$2,2,FALSE)),\"\",VLOOKUP(A1,$A$2:$B$2,2,FALSE))"; HSSFWorkbook book = new HSSFWorkbook(); HSSFSheet main_sheet = book.createSheet("main"); // Lookup target row HSSFRow desc_row = main_sheet.createRow(1); CellUtil.createCell(desc_row, 0, "X"); CellUtil.createCell(desc_row, 1, "Y"); // Lookup formula row HSSFRow main_row = main_sheet.createRow(0); CellUtil.createCell(main_row, 0, "X"); HSSFCell main0 = main_row.createCell(0); main0.setCellValue("X"); // write the result HSSFCell main1 = main_row.createCell(1); main1.setCellFormula(formula1); HSSFCell main2 = main_row.createCell(2); main2.setCellFormula(formula2); try { book.write(new FileOutputStream("demo.xls")); } catch (Exception e) { e.printStackTrace(); } } }