function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Yadhagu LYadhagu L 

is it possible to use apache poi in salesforce

Prateek Prasoon 25Prateek Prasoon 25
Yes, it is possible to use Apache POI in Salesforce. Apache POI is a Java-based library for working with Microsoft Office documents, including Excel. Since Salesforce supports Java, you can use Apache POI to read, create, and manipulate Excel files within Salesforce.
To use Apache POI in Salesforce, you can create a custom Apex class that includes the Apache POI libraries and methods. You can then call this Apex class from your Salesforce code to work with Excel files.
Here's an example of how to use Apache POI in Salesforce to read data from an Excel file:
// Import the Apache POI libraries
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
// Create a new workbook object from an Excel file
XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream('MyExcelFile.xlsx'));
// Get the first worksheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0);
// Loop through the rows in the worksheet and print the cell values
for (Row row : sheet) {
  for (Cell cell : row) {
    System.out.println(cell.getStringCellValue());
  }
}

If you find this answer helpful,Please mark it as the best answer.
VinayVinay (Salesforce Developers) 
Hi Yadhagu,

Salesforce provides support for Java in its platform, so you can use Apache POI in Salesforce by following these steps.
  • Create a new Java class in Salesforce by going to Setup > Develop > Apex Classes and clicking the "New" button.
  • Write your code using the Apache POI library to read or write Excel files. Here's an example of code that reads data from an Excel file.
// Load the Excel file
Workbook workbook = WorkbookFactory.create(new FileInputStream("path/to/excel/file.xlsx"));

// Get the first sheet
Sheet sheet = workbook.getSheetAt(0);

// Iterate over the rows and cells
for (Row row : sheet) {
  for (Cell cell : row) {
    // Do something with the cell value
    System.out.println(cell.getStringCellValue());
  }
}
  • Save your Java class and run it in Salesforce by creating a new Apex trigger or adding it to an existing one.
Note that you'll need to include the Apache POI library in your Salesforce project by either uploading the JAR file or using a dependency management tool like Maven or Gradle.

Please mark as Best Answer if above information was helpful.

Thanks,
Alain CabonAlain Cabon
You cannot use jar files directly with Salesforce (Sales and Service Cloud) on the server side with Apex:
https://salesforce.stackexchange.com/questions/201556/how-to-use-java-jar-files-in-saleforce

On the client side, we use the javascript equivalent of POI ( xlsx.js , free) with static ressources (javascript engine are present in all the internet browsers but there are no accessible JVM on the server side of Salesforce by default, these JVMs could be located on Heroku for example and you use webservices to exchange the request (raw data)/response (xlsx)) or Salesforce Functions (extra license) : https://functions.salesforce.com/signups

xlsx.js :
https://developer.salesforce.com/forums/ForumsMain?id=9062I000000UcLhQAK
 
Yadhagu LYadhagu L

Hi @Alain Cabon,

Thanks for your time, 

Need your assistance on creating/adding/looping data in new excel worksheet but in same workbook(each record in seperate sheet, but need on single workbook). E.g. I have looped through 10 accounts, I need these selected data (fields) for each looped account in a new worksheet and at the end of execution the workbook should be download. I'll be using this functionality from list view page. 

This was the purpose I had asked for Apache POI. Thanks in advance.