本文介绍如何使用Spire.XLS for Java将一维数组和二维数组写入Excel单元格。
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
public class InsertArray {
public static void main(String[] args) {
//创建Workbook对象
Workbook wb = new Workbook();
//获取第一张工作表
Worksheet sheet = wb.getWorksheets().get(0);
//定义一维数据
String[] oneDimensionalArray = new String[]{"苹果", "梨子", "葡萄", "香蕉"};
//将数组从指定单个格开始写入工作表,true表示纵向写入,设置为false为横向写入
sheet.insertArray(oneDimensionalArray, 1, 1, true);
//定义二维数组
String[][] twoDimensionalArray = new String[][]{
{"姓名", "年龄", "性别", "学历"},
{"小张", "25", "男", "本科"},
{"小王", "24", "男", "本科"},
{"小李", "26", "女", "本科"}
};
//从指定单元格开始写入二维数组到工作表
sheet.insertArray(twoDimensionalArray, 1, 3);
//保存文档
wb.saveToFile("InsertArrays.xlsx", ExcelVersion.Version2016);
}
}