Created
November 14, 2018 11:26
-
-
Save fintanmm/51bf76135e4531dba0b89752ade6c7f6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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