Excel写入

public static void ExcelUtils(String[] header,List<Object[]> list,String path){

File file = new File(path);

try {

// 创建新的可写入的Excel工作薄对象

WritableWorkbook targetWorkbook = Workbook.createWorkbook(file);

// 读取新文件中第一张工作表

WritableSheet workSheet = targetWorkbook.createSheet("sheet1", 0);

WritableCellFormat wformat = new WritableCellFormat();// 表样式

wformat.setBorder(Border.ALL, BorderLineStyle.THIN);

WritableFont fontTitle = new WritableFont(WritableFont.TIMES,12, WritableFont.BOLD);

WritableCellFormat wformatTitle = new WritableCellFormat(fontTitle);// 表标题样式

wformatTitle.setBorder(Border.ALL, BorderLineStyle.THIN);

//设置格式

WritableFont font12 = new WritableFont(WritableFont.createFont("宋体"), 12);

WritableCellFormat titleFormat = new WritableCellFormat(font12);

titleFormat.setAlignment(Alignment.CENTRE);

titleFormat.setBorder(Border.ALL, BorderLineStyle.THIN);

//写入标题

for (int i = 0; i < header.length; i++) {

//第一个数字表示列,第二个数字表示行,下标从0开始

workSheet.addCell(new Label(i,0,header[i],wformatTitle));

}

//写入主体数据

for (int i = 0; i < list.size(); i++) {

workSheet.addCell(new Label(0,i+1,String.valueOf(i+1),titleFormat));

for (int j = 1; j < list.get(i).length-3; j++) {

workSheet.addCell(new Label(j,i+1,list.get(i)[j].toString(),titleFormat));

}

}

// 写入Excel对象

targetWorkbook.write();

// 关闭可写入的Excel对象

targetWorkbook.close();

} catch (Exception e) {

e.printStackTrace();

throw new RuntimeException("导出Excel失败");

}

}

猜你喜欢

转载自blog.csdn.net/weixin_41771218/article/details/84978291
今日推荐