DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How to Convert XLS to XLSX in Java
  • Thermometer Continuation in Scala
  • Deploying a Scala Play Application to Heroku: A Step-by-Step Guide
  • How to Edit a PowerPoint PPTX Document in Java

Trending

  • Unlocking the Benefits of a Private API in AWS API Gateway
  • Unlocking AI Coding Assistants Part 3: Generating Diagrams, Open API Specs, And Test Data
  • Beyond ChatGPT, AI Reasoning 2.0: Engineering AI Models With Human-Like Reasoning
  • Cookies Revisited: A Networking Solution for Third-Party Cookies
  1. DZone
  2. Coding
  3. Languages
  4. Read/Write in Excel Sheet Using Apache POI With Scala

Read/Write in Excel Sheet Using Apache POI With Scala

Want to learn how to read and write in an Excel sheet using Apache POI? Check out this post to learn more about using the Apache POI with Scala.

By 
Teena Vashist user avatar
Teena Vashist
·
Sep. 18, 18 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
28.1K Views

Join the DZone community and get the full member experience.

Join For Free

Yes, you read the title it right — using Apache POI, you can easily read and write in an MS Excel file using Java/Scala.

So, before we get started with the implementation, let's have a quick introduction of Apache POI.

Apache POI is a Java API for manipulating several file formats based upon the Office Open XML standards (OOXML) and Microsoft's OLE 2 Compound Document format (OLE2). In short, you can read and write MS Excel files using Java.

Apache POI contains classes and methods to work on all OLE2 compound documents of the MS Office.

Let's get started with the implementation:

  1. First of all, you need to include the dependency in your build.sbt:
    "org.apache.poi" % "poi-ooxml" % "3.17"
  2. Once you have the XSSFSheet object, you can get  row(getRow), cell(getCell) , and the value of the cell by using setCellValuein the excel sheet.
    You can generate your own data or manipulate your existing data to write it in the excel file. In the below code snippet, I am generating some dummy data for storing it in the excel sheet and then simply writing it in the excel file. Keep in mind that the getRow  method represents the one row ahead of the given input on a sheet. For getRow(3) — this method represents the fourth row on a sheet instead of the third row. If you ask for a row that is not defined, it will give the null.
  val dummyData = (for (i <- 1 to 5)
    yield List(i, i, i, i, i, i, i, i, i, i, i, i)).toList

for (r <- 22 to 26; c <- 1 to 12) {
    val row = sheet.getRow(r - 1)
   try { 
    val cell = row.getCell(c)
        cell.setCellValue(dummyData(r - 22)(c - 1))
    } catch {
      case ex: Exception =>
        val cell = row.createCell(c)
        cell.setCellValue(dummyData(r - 22)(c - 1))
    }
 }

  inputStream.close()
  val fos = new FileOutputStream("""D:\xlsx\test.xlsx""")
  workbook.write(fos)
  fos.close()


Thanks, guys, for reading this post. I hope you found this article helpful.

Happy coding!

Apache POI Scala (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • How to Convert XLS to XLSX in Java
  • Thermometer Continuation in Scala
  • Deploying a Scala Play Application to Heroku: A Step-by-Step Guide
  • How to Edit a PowerPoint PPTX Document in Java

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!