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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Modernizing Apache Spark Applications With GenAI: Migrating From Java to Scala
  • Mastering Advanced Aggregations in Spark SQL
  • How to Convert XLS to XLSX in Java
  • Thermometer Continuation in Scala

Trending

  • Introducing senro: A Pipeline Engine You Program in Go
  • Building a Real Pipeline With senro: Monorepos, Caching, and Triggers
  • YAGNI in the Age of AI Coding
  • Kill the Worker, Keep the Research: Build a Recoverable LangGraph Agent on Temporal

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Sep. 18, 18 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
28.8K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

  • Modernizing Apache Spark Applications With GenAI: Migrating From Java to Scala
  • Mastering Advanced Aggregations in Spark SQL
  • How to Convert XLS to XLSX in Java
  • Thermometer Continuation in Scala

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook