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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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
Partner Zones AWS Cloud
by AWS Developer Relations
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
Partner Zones
AWS Cloud
by AWS Developer Relations
  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.

Teena Vashist user avatar by
Teena Vashist
·
Sep. 18, 18 · Tutorial
Like (1)
Save
Tweet
Share
25.69K 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.

Popular on DZone

  • Microservices 101: Transactional Outbox and Inbox
  • mTLS Everywere
  • Microservices Testing
  • Required Knowledge To Pass AWS Certified Solutions Architect — Professional Exam

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: