本文介绍如何使用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);
}
}