初识poi
1.添加相应jar包
2.编写相应代码实现功能
public class poi {
@Test //建立表格,表单对象
public void test() throws IOException {
String filePath="C:\\code\\poi\\table1.xlsx";//存储路径
XSSFWorkbook workbook=new XSSFWorkbook(); //建立文档对象
XSSFSheet sheet=workbook.createSheet(); //创建表单(不给名字,会自动建名sheet0)
workbook.createSheet(); //创建表单(不给名字,会自动建名sheet1)
sheet=workbook.createSheet("exc1"); //创建表单(赋值名字exc1)
FileOutputStream out=new FileOutputStream(filePath);//建立文件输出流,加入路径
workbook.write(out);//输出文件
out.close(); //关闭输出流
System.out.println("SUCCESS");
}
@Test //建立行列并赋值
public void test1() throws IOException{
String filePath="c:\\code\\poi\\test1.xlsx";
XSSFWorkbook workbook=new XSSFWorkbook();
XSSFSheet sheet=workbook.createSheet("sh1");
XSSFRow row=sheet.createRow(0);//第一行
XSSFCell cell1=row.createCell(0);//第一行第一个的单元格(第一列)
cell1.setCellValue(false);//赋值
XSSFCell cell2=row.createCell(1);//第一行第二个的单元格(第二列)
cell2.setCellValue(new Date());//赋值
row.createCell(2).setCellValue(false);//第一行第三个的单元格并赋值(第三列)
XSSFCell cell3=row.createCell(3);//第一行第四个的单元格(第四列)
cell3.setCellValue(new Date());//赋值
//第二行
XSSFRow row1=sheet.createRow(1);
row1.createCell(0).setCellValue(false);
row1.createCell(1).setCellValue(new Date());
row1.createCell(2).setCellValue(false);
FileOutputStream out=new FileOutputStream(filePath);
workbook.write(out);
out.close();
System.out.println("SUCCESS");
}
@Test //根据数据循环,打印表格数据
public void test2() throws IOException{
List<user> list=new ArrayList<user>();
list.add(new user(1,"名字","电话","地址","密码"));
list.add(new user(2,"名字2","电话2","地址2","密码2"));
list.add(new user(3,"名字3","电话3","地址3","密码3"));
list.add(new user(4,"名字4","电话4","地址4","密码4"));
list.add(new user(5,"名5字","电话5","地址5","密5码"));
list.add(new user(6,"名字6","电话6","地址6","密6码"));
String filePath="c:\\code\\poi\\test2.xlsx";
XSSFWorkbook workbook=new XSSFWorkbook();
XSSFSheet sheet=workbook.createSheet();
if(list!=null){
for(int i=0;i<list.size();i++){ //循环行数,行数即为集合的长度
XSSFRow row=sheet.createRow(i);
for(int j=0;j<5;j++){ //循环列数,列数自定义,但是不能超过对象属性的个数
XSSFCell cell=row.createCell(j);
if(j==0){
cell.setCellValue(list.get(i).getId());
}else if(j==1){
cell.setCellValue(list.get(i).getName());
}else if(j==2){
cell.setCellValue(list.get(i).getPhone());
}else if(j==3){
cell.setCellValue(list.get(i).getAddress());
}else if(j==4){
cell.setCellValue(list.get(i).getPassword());
}
}
}
}
FileOutputStream out=new FileOutputStream(filePath);
workbook.write(out);
out.close();
System.out.println("SUCCESS");
}
@Test //建立表格添加数据,并给数据添加样式
public void test3() throws Exception{
String filePath="c:\\code\\poi\\test3.xlsx";
XSSFWorkbook workbook=new XSSFWorkbook();
XSSFSheet sheet=workbook.createSheet("test3");
XSSFRow row=sheet.createRow(0);//建立第一行
//为某列设置列宽,第一个参数为第几列,第二个参数为宽度
//*256,这个参数的单位是1/256个字符宽度,也就是说,这里是把3列的宽度设置为了20个字符。
sheet.setColumnWidth(1, 20*256);
sheet.setColumnWidth(2, 20*256);
XSSFCell cell=row.createCell(0);//建立第一行的第一个单元格
cell.setCellValue(1);//给单元格赋值
XSSFCell cell2=row.createCell(1);//建立第一行的第二个单元格
cell2.setCellValue(new Date());//给单元格赋值
XSSFCell cell3=row.createCell(2);//建立第一行的第三个单元格
cell3.setCellValue(new Date());//给单元格赋值
XSSFCellStyle style=workbook.createCellStyle();//给表格添加 单元格样式
style.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));//定义时间样式规则
cell2.setCellStyle(style);//给单元格添加样式规则
cell3.setCellStyle(style);//给单元格添加样式规则
FileOutputStream out=new FileOutputStream(filePath);
workbook.write(out);
out.close();
System.out.println("SUCCESS");
}
@Test //合并行和列
public void test4() throws Exception{
String filePath="c:\\code\\poi\\test4.xlsx";
XSSFWorkbook workbook=new XSSFWorkbook();
XSSFSheet sheet=workbook.createSheet("test4");
CellRangeAddress region=null;
/*XSSFRow row=sheet.createRow(0);//0为行的起始位置
XSSFCell cell=row.createCell(0);//0为列的起始位置
cell.setCellValue("姓名");
region=new CellRangeAddress(0,0,0,5);//行一样,合并列
sheet.addMergedRegion(region);*/
/*XSSFRow row2=sheet.createRow(0);
XSSFCell cell2=row2.createCell(6);
cell2.setCellValue("性别");
region=new CellRangeAddress(0,5,6,6);//列一样,合并行
sheet.addMergedRegion(region);*/
/*XSSFRow row3=sheet.createRow(0);//0行的起始位置
XSSFCell cell3=row3.createCell(7);//7列的起始位置
cell3.setCellValue("爱好");//起始位置单元格的值
region=new CellRangeAddress(0,2,7,8);//行列都不一样,合并行和列
sheet.addMergedRegion(region);*/
XSSFRow row4=sheet.createRow(5);//行的起始位置
XSSFCell cell4=row4.createCell(3);//列的起始位置
cell4.setCellValue("地址");
region=new CellRangeAddress(5,9,3,5);
sheet.addMergedRegion(region);
FileOutputStream out = new FileOutputStream(filePath);
workbook.write(out);
out.close();
}
@Test //设置单元格样式
public void test5() throws Exception{
String filePath="c:\\code\\poi\\test5.xlsx";
XSSFWorkbook workbook=new XSSFWorkbook();
XSSFSheet sheet=workbook.createSheet("test5");
XSSFRow row=sheet.createRow(0);
XSSFCell cell=row.createCell(1);
cell.setCellValue("颜色");
//设置文字样式
XSSFFont font=workbook.createFont();
font.setFontHeightInPoints((short)50);//设置字体高度大小
font.setFontName("微软雅黑");//设置字体
font.setBoldweight(Font.BOLDWEIGHT_BOLD);//设置粗体
//font.setItalic(true);//设置是否倾斜
font.setColor(HSSFColor.BRIGHT_GREEN.index);//设置字体颜色
//font.setStrikeout(true);//设置删除线
//设置单元格样式
XSSFCellStyle style=workbook.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//文字居中
//style.setRotation((short)70);//倾斜,数字为倾斜角度
//为文字添加单元格样式
style.setFont(font);
//为单元格添加单元格样式
cell.setCellStyle(style);
FileOutputStream out = new FileOutputStream(filePath);
workbook.write(out);
out.close();
}
@Test //设置单元格样式
public void test6() throws Exception{
String filePath="c:\\code\\poi\\test6.xlsx";
XSSFWorkbook workbook=new XSSFWorkbook();
XSSFSheet sheet=workbook.createSheet("test6");
XSSFRow row=sheet.createRow(0);
XSSFCell cell=row.createCell(1);
cell.setCellValue("颜色");
XSSFCellStyle style = workbook.createCellStyle();
style.setAlignment(XSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
style.setWrapText(true);
style.setIndention((short) 5);
style.setRotation((short) 60);
//为单元格添加单元格样式
cell.setCellStyle(style);
FileOutputStream out = new FileOutputStream(filePath);
workbook.write(out);
out.close();
}
@Test //为单元格添加边框
public void test7() throws Exception{
String filePath="c:\\code\\poi\\test7.xlsx";
XSSFWorkbook workbook=new XSSFWorkbook();
XSSFSheet sheet=workbook.createSheet("test7");
XSSFCellStyle style = workbook.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//文字居中
//设置边框样式
style.setBorderBottom(XSSFCellStyle.BORDER_THIN);
style.setBorderLeft(XSSFCellStyle.BORDER_THIN);
style.setBorderTop(XSSFCellStyle.BORDER_THIN);
style.setBorderRight(XSSFCellStyle.BORDER_THIN);
for (int i = 0; i < 10; i++) {
XSSFRow row = sheet.createRow(i);
for (int j = 0; j < 10; j++) {
XSSFCell cell = row.createCell(j);
cell.setCellValue(j);
cell.setCellStyle(style);//为单元格添加 定义好的单元格样式
}
}
FileOutputStream out = new FileOutputStream(filePath);
workbook.write(out);
out.close();
}
@Test //从Excel中导出数据到项目中
public void test8() throws Exception{
String filePath="c:\\code\\poi\\test7.xlsx";
FileInputStream stream = new FileInputStream(filePath);
XSSFWorkbook workbook = new XSSFWorkbook(stream);
XSSFSheet sheet = workbook.getSheet("test7");
for (Row row : sheet) {
for (Cell cell : row) {
System.out.print(cell.toString() + "\t");
}
System.out.println();
}
}
}