poi php,POI 操作 excel表格 (简单整理)

import org.apache.poi.ss.usermodel.*;importorg.apache.poi.ss.util.CellRangeAddress;importorg.apache.poi.xssf.streaming.SXSSFCell;importorg.apache.poi.xssf.streaming.SXSSFRow;importorg.apache.poi.xssf.streaming.SXSSFSheet;importorg.apache.poi.xssf.streaming.SXSSFWorkbook;importorg.apache.poi.xssf.usermodel.XSSFWorkbook;importjava.io.BufferedOutputStream;importjava.io.File;importjava.io.FileOutputStream;importjava.io.IOException;/*** Created by Administrator on 2019/6/12.*/

public classSXSSFWorkbook_Main

{public static voidmain(String[] args)

{long startTime =System.currentTimeMillis();

String filePath= "E:\\111.xlsx";

SXSSFWorkbook sxssfWorkbook= null;

BufferedOutputStream outputStream= null;try{//这样表示SXSSFWorkbook只会保留100条数据在内存中,其它的数据都会写到磁盘里,这样的话占用的内存就会很少

sxssfWorkbook = new SXSSFWorkbook(getXSSFWorkbook(filePath),100);//获取第一个Sheet页

SXSSFSheet sheet = sxssfWorkbook.getSheetAt(0);//合并行之后创建第一行

sheet.addMergedRegion(new CellRangeAddress(0,0,0,3));

SXSSFRow row= sheet.createRow(0);//添加第一行的单元格

SXSSFCell cell = row.createCell(0);

CellStyle cellStyle=getHeadStyle(sxssfWorkbook);

cell.setCellStyle(cellStyle);

row.createCell(1).setCellStyle(cellStyle);

row.createCell(2).setCellStyle(cellStyle);

row.createCell(3).setCellStyle(cellStyle); //合并单元格之后要加边框, 所以都要加上

cell.setCellValue("项目工作清单"); //合并单元格之后设置值//创建第2行

SXSSFRow row1 = sheet.createRow(1);

CellStyle style=getHeadStyle(sxssfWorkbook);

style.setFillForegroundColor(IndexedColors.SKY_BLUE.getIndex());

row1.createCell(0).setCellStyle(style);

outputStream= new BufferedOutputStream(newFileOutputStream(filePath));

sxssfWorkbook.write(outputStream);

outputStream.flush();

sxssfWorkbook.dispose();//释放workbook所占用的所有windows资源

} catch(IOException e) {

e.printStackTrace();

}finally{if(outputStream!=null) {try{

outputStream.close();

}catch(IOException e) {

e.printStackTrace();

}

}

}long endTime =System.currentTimeMillis();

System.out.println(endTime-startTime);

}private staticCellStyle getHeadStyle(SXSSFWorkbook sxssfWorkbook)

{

CellStyle cellStyle=sxssfWorkbook.createCellStyle();

cellStyle.setAlignment(HorizontalAlignment.CENTER);//设置单元格的水平居中//上下左右的边框

cellStyle.setBorderTop(BorderStyle.THIN);

cellStyle.setBorderBottom(BorderStyle.THIN);

cellStyle.setBorderLeft(BorderStyle.THIN);

cellStyle.setBorderRight(BorderStyle.THIN);//设置单元格背景填充颜色

cellStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());

cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);returncellStyle;

}public staticXSSFWorkbook getXSSFWorkbook(String filePath) {

XSSFWorkbook workbook= null;

BufferedOutputStream outputStream= null;try{

File fileXlsxPath= newFile(filePath);

outputStream= new BufferedOutputStream(newFileOutputStream(fileXlsxPath));

workbook= newXSSFWorkbook();

workbook.createSheet("测试Sheet");

workbook.write(outputStream);

}catch(Exception e) {

e.printStackTrace();

}finally{if(outputStream!=null) {try{

outputStream.close();

}catch(IOException e) {

e.printStackTrace();

}

}

}returnworkbook;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值