Java实现⽣成Excel表格并将数据导⼊及按照Excel模板实现数据导⼊
背景:项⽬中⽤到邮件发送功能,需要将数据导⼊excel表格然后发送给供应商,第⼀次做的时候是直接创建了⼀个excel,领导说⽐较丑,就甲⽅要了个模板,所以本篇⽂章除了介绍直接⽣成excel导⼊数据的功能以外,也介绍按照固定模板的⽅式导⼊数据的功能。
1.新⽣成⼀个Excel,并将数据导⼊
package com.tdhcmon.util;
import del.PuEnquiry;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.ByteArrayOutputStream;
import java.util.List;
/**
* 将对象转换为excel表格,并转化为字节输出流返回
*/
public class CreateExcel {
public static ByteArrayOutputStream CreateExccel(List<PuEnquiry> list){
ByteArrayOutputStream baos =new ByteArrayOutputStream();
//创建⼀个⼯作薄对象
Workbook workbook =new XSSFWorkbook();
// 创建⼀个表
XSSFSheet sheet =(XSSFSheet) ateSheet("sheet1");
//创建⾸⾏
Row row = ateRow(0);
for(int i=1;i<list.size();i++){
XSSFRow row1 = ateRow(i);
怎么创建excel表格
}
}
try{
workbook.write(baos);
baos.close();
}catch(Exception e){
e.printStackTrace();
}
return baos;
}
}
步骤:创建⼯作薄–创建表–创建⾏–创建单元格–操作单元格
备注:不⽤管⽅法的返回值及⼯作流,因为我是为了配合邮件发送excel附件才那么做的,只⽤关注创建⼯作簿到setCellValue就可以了。
2.按照固定excel模板导⼊数据
package com.tdhcmon.util;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.amples.CreateCell;
import java.io.*;
import java.util.List;
public class CreateExcelAsTemp {
public static ByteArrayOutputStream CreateExcel(List<Person> list)throws IOException {
ByteArrayOutputStream baos =new ByteArrayOutputStream();
// 读取源⽂件
FileInputStream fis =new FileInputStream("F:/test.xlsx");
XSSFWorkbook workBook =new XSSFWorkbook(fis);
/
/ 进⾏模板的克隆(接下来的操作都是针对克隆后的sheet)
XSSFSheet sheet = workBook.cloneSheet(0);
workBook.setSheetName(1,"询价单");// 给sheet命名
XSSFCellStyle cellStyle = ateCellStyle();
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);//下边框
cellStyle.setBottomBorderColor(Index());//底框⿊⾊
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
cellStyle.setLeftBorderColor(Index());// 左边⿊⾊
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
cellStyle.setTopBorderColor(Index());// 上边⿊⾊
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
cellStyle.setRightBorderColor(Index());// 右边⿊⾊
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 字体居中
for(int i=10;i<list.size()+10;i++){
Row row = ateRow(i);
Cell cell = ateCell(0);
cell.setCellStyle(cellStyle);
cell.setCellValue(i-9);
Cell cell1 = ateCell(1);
cell1.setCellStyle(cellStyle);
cell1.setCellValue(((i-10).getName()));
Cell cell2 = ateCell(2);
cell2.setCellStyle(cellStyle);
cell2.(i-10).getAge());
cell2.(i-10).getAge());
Cell cell3 = ateCell(3);
cell3.setCellStyle(cellStyle);
cell3.(i-10).getAddress());
Cell cell4 = ateCell(4);
cell4.setCellStyle(cellStyle);
cell4.(i-10).getBirthday());
Cell cell5 = ateCell(5);
cell5.setCellStyle(cellStyle);
cell5.(i-10).getBirthday());
Cell cell6 = ateCell(6);
cell6.setCellStyle(cellStyle);
cell6.(i-10).getAaa());
Cell cell7 = ateCell(7);
cell7.setCellStyle(cellStyle);
cell7.(i-10).getBbb());
Cell cell8 = ateCell(8);
cell8.setCellStyle(cellStyle);
cell8.(i-10).getCcc());
Cell cell9 = ateCell(9);
cell9.setCellStyle(cellStyle);
cell9.(i-10).getDdd());
}
try{
workBook.write(baos);
baos.close();
/*FileOutputStream fileOutputStream = new FileOutputStream("F:/aaa.xlsx");
workBook.write(fileOutputStream);
fileOutputStream.close();*/
}catch(Exception e){
e.printStackTrace();
}
return baos;
}
}
步骤: 从磁盘中读取源⽂件–根据源⽂件克隆sheet–操作(之后的类似于第⼀种操作,多了设置样式的代码是因为我要跟模板对应上,⽽且setCellValue和SetCellStyle不能同时写在⼀⾏,所以后边的⽽代码看起来⽐较多)
备注: 克隆完之后的操作类似于第⼀种操作,这⾥使⽤的是克隆的⽅式,当然也可以使⽤POI读取之类的⽅式,达成效果即可。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。