scala如何储存exl文件
时间: 2023-05-16 11:05:49 浏览: 94
Scala可以使用Apache POI库来读取和写入Excel文件。您可以使用以下代码来读取Excel文件:
```scala
import org.apache.poi.ss.usermodel.{WorkbookFactory, DataFormatter}
val file = new File("path/to/excel/file.xlsx")
val workbook = WorkbookFactory.create(file)
val sheet = workbook.getSheetAt(0)
val formatter = new DataFormatter()
for (row <- sheet) {
for (cell <- row) {
val cellValue = formatter.formatCellValue(cell)
println(cellValue)
}
}
```
要写入Excel文件,您可以使用以下代码:
```scala
import org.apache.poi.ss.usermodel.{WorkbookFactory, DataFormatter}
import org.apache.poi.xssf.usermodel.{XSSFWorkbook, XSSFCellStyle, XSSFColor}
import java.io.FileOutputStream
val workbook = new XSSFWorkbook()
val sheet = workbook.createSheet("Sheet1")
val row = sheet.createRow(0)
val cell = row.createCell(0)
cell.setCellValue("Hello, world!")
val style = workbook.createCellStyle()
style.setFillForegroundColor(new XSSFColor(new java.awt.Color(255, 255, 0)))
style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND)
cell.setCellStyle(style)
val fileOut = new FileOutputStream("path/to/excel/file.xlsx")
workbook.write(fileOut)
fileOut.close()
```
阅读全文