要将数据写入Excel中的多个工作表,可以使用Apache Spark的DataFrameWriter功能来实现。下面是一个使用Scala语言的代码示例:
import org.apache.spark.sql.{DataFrame, SparkSession}
object SparkExcelWriter {
def main(args: Array[String]): Unit = {
val spark = SparkSession.builder()
.appName("SparkExcelWriter")
.master("local")
.getOrCreate()
val data = Seq(
("John", 25, "USA"),
("Mike", 30, "UK"),
("Anna", 35, "Germany")
)
val df = spark.createDataFrame(data).toDF("Name", "Age", "Country")
val outputFilePath = "path/to/output.xlsx"
val sheetNames = Seq("Sheet1", "Sheet2", "Sheet3")
writeExcel(df, outputFilePath, sheetNames)
spark.stop()
}
def writeExcel(df: DataFrame, outputFilePath: String, sheetNames: Seq[String]): Unit = {
val workbook = new org.apache.poi.xssf.usermodel.XSSFWorkbook()
sheetNames.foreach(sheetName => {
val sheet = workbook.createSheet(sheetName)
val headerRow = sheet.createRow(0)
// Write column names as headers
df.schema.fields.zipWithIndex.foreach { case (field, index) =>
headerRow.createCell(index).setCellValue(field.name)
}
// Write data rows
df.collect().zipWithIndex.foreach { case (row, rowIndex) =>
val dataRow = sheet.createRow(rowIndex + 1)
row.toSeq.zipWithIndex.foreach { case (value, columnIndex) =>
val cell = dataRow.createCell(columnIndex)
cell.setCellValue(value.toString)
}
}
})
val fileOut = new java.io.FileOutputStream(outputFilePath)
workbook.write(fileOut)
fileOut.close()
workbook.close()
}
}
请注意,这个示例假定你已经在项目中添加了Apache POI库的依赖。