Skip to content

Instantly share code, notes, and snippets.

@fintanmm
Created November 14, 2018 11:26
Show Gist options
  • Save fintanmm/51bf76135e4531dba0b89752ade6c7f6 to your computer and use it in GitHub Desktop.
Save fintanmm/51bf76135e4531dba0b89752ade6c7f6 to your computer and use it in GitHub Desktop.
import java.io.File
import org.apache.poi.ss.usermodel.WorkbookFactory
import org.apache.poi.ss.usermodel.{Row, Cell}
val baseUnitFile = new File("./BaseUnits_export_.xlsx")
var workbook = WorkbookFactory.create(baseUnitFile)
val sheet = workbook.getSheetAt(0)
val xmlTemplate = (DevName: Cell, IP: Cell) => <MirrorOp>
<Device>
<Device_name>{DevName}</Device_name>
<IP>{IP}</IP>
</Device>
</MirrorOp>
val xmlNodeBuffer = new xml.NodeBuffer
def createXMLFrom(row: Row, addToNodeBuffer: xml.NodeBuffer) = row match {
case row if row.getRowNum.toInt == 0 => ""
case default => addToNodeBuffer += xmlTemplate(row.getCell(1), row.getCell(3))
}
sheet.forEach(row => createXMLFrom(row, xmlNodeBuffer))
val pp = new scala.xml.PrettyPrinter(80, 4)
val xmlParent = xml.XML.loadString(pp.format(<Devices>{xmlNodeBuffer}</Devices>))
scala.xml.XML.save("./baseunit.xml", xmlParent, "UTF-8", true, null)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment