如何在apache spark java中使用hadoop office库将数据集写入excel文件

目前我正在使用com.crealytics.spark.excel来读取excel文件,但是使用这个库我不能将数据集写入excel文件。 这个链接说使用hadoop办公库( org.zuinnote.spark.office.excel )我们可以读取和写入excel文件

请帮助我将数据集对象写入spark java中的excel文件。

您可以使用org.zuinnote.spark.office.excel来读写使用数据集的Excel文件。 示例在https://github.com/ZuInnoTe/spark-hadoopoffice-ds/上给出。 但是,如果您读取“数据集中的Excel”并尝试将其写入另一个Excel文件,则会出现一个问题。 请参阅https://github.com/ZuInnoTe/hadoopoffice/issues/12中scala的问题和解决方法。

我使用org.zuinnote.spark.office.excel编写了一个Java示例程序,并在该链接中给出了解决方法。 请看看这是否有助于你。

 public class SparkExcel { public static void main(String[] args) { //spark session SparkSession spark = SparkSession .builder() .appName("SparkExcel") .master("local[*]") .getOrCreate(); //Read Dataset<Row> df = spark .read() .format("org.zuinnote.spark.office.excel") .option("read.locale.bcp47", "de") .load("c:\\temp\\test1.xlsx"); //Print df.show(); df.printSchema(); //Flatmap function FlatMapFunction<Row, String[]> flatMapFunc = new FlatMapFunction<Row, String[]>() { @Override public Iterator<String[]> call(Row row) throws Exception { ArrayList<String[]> rowList = new ArrayList<String[]>(); List<Row> spreadSheetRows = row.getList(0); for (Row srow : spreadSheetRows) { ArrayList<String> arr = new ArrayList<String>(); arr.add(srow.getString(0)); arr.add(srow.getString(1)); arr.add(srow.getString(2)); arr.add(srow.getString(3)); arr.add(srow.getString(4)); rowList.add(arr.toArray(new String[] {})); } return rowList.iterator(); } }; //Apply flatMap function Dataset<String[]> df2 = df.flatMap(flatMapFunc, spark.implicits().newStringArrayEncoder()); //Write df2.write() .mode(SaveMode.Overwrite) .format("org.zuinnote.spark.office.excel") .option("write.locale.bcp47", "de") .save("c:\\temp\\test2.xlsx"); } } 

我已经用Java 8和Spark 2.1.0testing了这个代码。 我从https://mvnrepository.com/artifact/com.github.zuinnote/spark-hadoopoffice-ds_2.11/1.0.3使用maven并添加了&#x5BF9;org.zuinnote.spark.office.excel依赖