Skip to content

Instantly share code, notes, and snippets.

@mrvisser
Created July 15, 2016 04:01
Show Gist options
  • Save mrvisser/e0cba158eee939a105844e084f7ba6b8 to your computer and use it in GitHub Desktop.
Save mrvisser/e0cba158eee939a105844e084f7ba6b8 to your computer and use it in GitHub Desktop.
// Add the POI core and OOXML support dependencies into your build.sbt
libraryDependencies ++= Seq(
"org.apache.poi" % "poi" % "3.15-beta2",
"org.apache.poi" % "poi-ooxml" % "3.15-beta2",
"org.apache.poi" % "poi-ooxml-schemas" "3.15-beta2"
)
// Import the required classes
import org.apache.poi.xssf.usermodel.XSSFWorkbook
import java.io.FileOutputStream
object XSSFMain extends App {
// Automatically convert Java collections to Scala equivalents
import scala.collection.JavaConversions._
// Read the contents of the workbook
val workbook = new XSSFWorkbook("SampleSS.xlsx")
for {
// Iterate and print the sheets
(sheet, i) <- workbook.zipWithIndex
_ = println(s"Sheet $i of ${workbook.getNumberOfSheets}: ${sheet.getSheetName}")
// Iterate and print the rows
row <- sheet
_ = println(s"\tRow ${row.getRowNum}")
// Iterate and print the cells
cell <- row
} {
println(s"\t\t${cell.getColumnIndex}: ${cell.getStringCellValue}")
}
// Add a sheet to the workbook
val sheet = workbook.createSheet("new sheet")
val row = sheet.createRow(7)
val cell = row.createCell(42)
cell.setAsActiveCell()
cell.setCellValue("The answer to life, the universe, and everything")
// Save the updated workbook as a new file
val fos = new FileOutputStream("UpdatedSampleSS.xlsx")
workbook.write(fos)
workbook.close()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment