• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

433
Views
Apache poi rellena el valor de la celda por fórmula

Soy nuevo en apache poi tratando de escribir el archivo de Excel. Tengo algún problema al configurar la fórmula en la celda.

A continuación se muestra mi muestra de Excel:

Usuario País Valor
Rohit Reino Unido
Juan INDIANA

Necesito completar la columna Valor según los campos Usuario y País . A continuación se muestra la fórmula de Excel que quiero convertir a apache poi java

 =IF(AND(LEN([@[User]]) > 0, [@Country] = "UK"),1,0)

Alguien puede ayudarme ?

Código de muestra

 try { InputStream inputStream = this.getClass().getResourceAsStream("/sample.xlsx"); XSSFWorkbook workbook = new XSSFWorkbook (inputStream); System.out.println("inside the controller"); XSSFSheet sheet = workbook.getSheetAt(0); Object[][] bookData = { {"Rohit","UK",null}, {"John","IND",null} }; int rowCount = sheet.getLastRowNum(); int count=0; //CellStyle cell1; for (Object[] aBook : bookData) { Row row = sheet.createRow(++rowCount); int columnCount = 0; Cell cell = row.createCell(columnCount); // cell.setCellValue(rowCount); for (Object field : aBook) { cell = row.createCell(columnCount++); if(field==null){ cell.setCellFormula("IF(AND(LEN(A1:A3)>0,(B1:B3)=UK),1,0)"); } else if (field instanceof String) { cell.setCellValue((String) field); } else if (field instanceof Integer) { cell.setCellValue((Integer) field); } } } java.util.List<XSSFTable> l = sheet.getTables(); l.get(0).getCTTable().setRef("A1:L4"); FileOutputStream outputStream = new FileOutputStream("D://demo/sample_with_values.xlsx"); workbook.write(outputStream); workbook.close(); outputStream.close(); } catch (IOException | EncryptedDocumentException ex) { ex.printStackTrace(); }
over 3 years ago · Santiago Trujillo
2 answers
Answer question

0

Como mencionó @Axel Richter , usar == no es válido.

cell.setCellFormula("IF(AND(LEN(A1:A3)>0,(B1:B3)==UK),1,0)");

Errores con su fórmula.

#1. El error...

 Parse error near char 25 '=' in specified formula 'IF(AND(LEN(A1:A3)>0,(B1:B3)==UK),1,0)'. Expected cell ref or constant literal`

…implica que está utilizando un = adicional en la fórmula.

#2. (B1:B3)==UK debe ser (B1:B3)="UK" . Está comparando un valor de cadena, por lo que debe estar entre comillas dobles.

Código :

 cell.setCellFormula("IF(AND(LEN(A1:A3)>0,(B1:B3)=\"UK\"),1,0)");

Salida :

ingrese la descripción de la imagen aquí

over 3 years ago · Santiago Trujillo Report

0

public static void main(String[] args) throws Exception { HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet spreadsheet = workbook.createSheet("example"); HSSFRow row = spreadsheet.createRow((short) 0); row.createCell(0).setCellValue("User"); row.createCell(1).setCellValue("Country"); row.createCell(2).setCellValue("Value"); row = spreadsheet.createRow((short) 1); row.createCell(0).setCellValue("Rohit"); row.createCell(1).setCellValue("UK"); row.createCell(2).setCellFormula("IF(AND(LEN(A1:A3)>0,(B1:B3)=\"UK\"),1,0)"); row = spreadsheet.createRow((short) 2); row.createCell(0).setCellValue("John"); row.createCell(1).setCellValue("IND"); row.createCell(2).setCellFormula("IF(AND(LEN(A1:A3)>0,(B1:B3)=\"UK\"),1,0)"); FileOutputStream out = new FileOutputStream( new File("/Users/anand.keshri/workspace/poi/first.xls") ); workbook.write(out); out.close(); workbook.close(); System.out.println("first.xls written successfully");
over 3 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error