NioCopy
public static void nioCopyFile(String resource,String destination)
throws Exception{
FileInputStream fis = new FileInputStream(resource);
FileOutputStream fos = new FileOutputStream(destination);
//得到readChannel
FileChannel readChannel = fis.getChannel();
//得到writeChannel
FileChannel writeChannel = fos.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
while(true){
buffer.clear();
int len = readChannel.read(buffer);
if(len==-1){
break;
}
buffer.flip();//read--->write
writeChannel.write(buffer);
}
readChannel.close();
writeChannel.close();
}
//POI
package com.yk.ln.util;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class SXSSFUtil {
private SXSSFWorkbook workbook = null;
public Boolean CreateWorkBook(String tablename, Map<String, Object> headmap, List<Map<String, Object>> datalist,
String path) {
try {
workbook = new SXSSFWorkbook();
Sheet sheet = workbook.createSheet(tablename);
createHead(sheet, headmap);
createBody(sheet, headmap, datalist);
CreateFile(path);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
private void CreateFile(String path) throws Exception {
File file = new File(path).getParentFile();
if (!file.exists()) {
file.mkdirs();
}
FileOutputStream output;
output = new FileOutputStream(path);
workbook.write(output);
output.flush();
output.close();
}
private void createBody(Sheet sheet, Map<String, Object> headmap, List<Map<String, Object>> datalist) {
int rownum = 1;
for (Map<String, Object> map : datalist) {
Row row = sheet.createRow(rownum);
Iterator<Entry<String, Object>> titer = headmap.entrySet().iterator();
int cellnum = 0;
while (titer.hasNext()) {
Cell cell = row.createCell(cellnum);
Entry<String, Object> ent = titer.next();
String keyt = ent.getKey().toString();
// String valuet = ent.getValue().toString();
if (map.containsKey(keyt)) {
// Object vv = map.get(keyt);
cell.setCellValue(map.get(keyt).toString());
}
cellnum++;
}
rownum++;
}
}
private void createHead(Sheet sheet, Map<String, Object> headmap) {
Row headrow = sheet.createRow(0);
Iterator<Entry<String, Object>> titer = headmap.entrySet().iterator();
int heanum = 0;
while (titer.hasNext()) {
Cell cell = headrow.createCell(heanum);
Entry<String, Object> ent =titer.next();
String valuet = ent.getValue().toString();
cell.setCellValue(valuet);
heanum++;
}
}
}
POI2
public class SXSSFUtil2 {
//Y轴数据
private List<String> yList ;
//x轴数据
private List<String> xList;
//x尾部轴书
private List<String> xTail;
//当前行号/
private int rowLine=1;
//当前列号 只为title使用
private int rowCol=0;
//开始时间列
private int fCol=0;
//开始常量列
private int constFCol=0;
//结束列
private int lCol=30;
//存放行数据
private Map<String,Object> rowMap = new HashMap<>();
//列数据
private Map<String,Object> colMap = new HashMap<>();
//总计数据
private Map<String,Object> totalMap = new HashMap<>();
static Workbook workBook=null;
/**
* 工作区
*/
Sheet sheet = null;
private int newLine=2;
private int frow=0;
private int lrow=2;
public SXSSFUtil2(){
this.init();
}
/**
* TODO 写入Excel Title
* @param titleName 月份 -部门名-收费统计
* @param frow 起始行 从0开始 下一次为循环中 9+(列*2)
* @param lrow 结束行 包含尾 9+(列*2)+2
* @param fclo 起始列 从0开始 0
* @param lcol 结束列 计算得出 4+x*5+8
* @return
*/
public boolean writeTitle(String titleName,int frow,int lrow,int fclo,int lcol){
if(null!=sheet){
this.addMergedRegion(frow,lrow,fclo,lcol);
Row row = sheet.createRow(frow);
Cell titleCeil = row.createCell(fclo);
CellStyle cellStyle = workBook.createCellStyle();
Font font = workBook.createFont();
font.setBold(true);
cellStyle.setFont(font);
titleCeil.setCellStyle(cellStyle);
titleCeil.setCellValue(titleName);
this.rowLine = lrow+1; //开启新行
return true;
}
return false;
}
/**
* 数据格式 Map中
* time '具体日期' ,路段,指标,现金,电支 差额
*/
/**
* 写入Excel头
* @param beginRow 序号账号对应的row
* @param endRow 日期为
* @param endCol
* @param headMap
* @return
*/
public boolean writeHead(int beginRow,List<Map<String,Object>> listMap){
if(listMap == null){
throw new IllegalArgumentException("listMap 不能为空!");
}
//序号账号
Row constRow = null;
writeHeadBefore(beginRow);
int timeRowLine = this.rowLine-1;
int i=0;
if(null!=listMap){
for(Map<String,Object> headMap:listMap){
Set<Map.Entry<String, Object>> entries = headMap.entrySet();
for(Map.Entry<String, Object> en:entries){
String key = en.getKey();
if(key.equals("time")){ //时间合并
Object pdaTime = en.getValue();
if(timeRow == null){
timeRow = sheet.createRow(timeRowLine); //需要-1
}
// System.out.println(this.fCol+": this.fClol");
this.addMergedRegion(timeRowLine,timeRowLine,this.fCol==4?4:this.fCol,this.fCol+4);
Cell timeRowCell = timeRow.createCell(this.fCol == 4 ? 4 : this.fCol);
timeRowCell.setCellValue(en.getValue().toString());
fontMiddle(timeRowCell,workBook,false,false);
this.fCol=this.fCol+5; //时间的轴
}else {
//普通常量
if(null==constRow){
constRow = sheet.createRow(this.rowLine);
}
Cell cosntHeadCell = constRow.createCell(this.constFCol++);
cosntHeadCell.setCellValue(en.getKey());
if(key.equals("差额")){
fontMiddle(cosntHeadCell,workBook,false,true);
}else {
fontMiddle(cosntHeadCell,workBook,false,false);
}
}
}
}
}
this.addMergedRegion(timeRowLine,timeRowLine,this.fCol==4?4:this.fCol,this.fCol+1);
Cell timeRowCell = timeRow.createCell(this.fCol);
this.fCol=this.fCol+1;
timeRowCell.setCellValue("小计金额");
fontMiddle(timeRowCell,workBook,false,false);
Cell cosntHeadCell = constRow.createCell(this.constFCol++);
cosntHeadCell.setCellValue("现金");
fontMiddle(cosntHeadCell,workBook,false,false);
cosntHeadCell = constRow.createCell(this.constFCol++);
cosntHeadCell.setCellValue("电支");
fontMiddle(cosntHeadCell,workBook,false,false);
appendTail(this.timeRow,workBook,sheet,this.rowLine-1,this.fCol,TestSXSSDomain.tailMap);
return true;
}
/**
* 合并单元格
* @param beginRow 开始行号
* @param endRow 结束行行
* @param beginCol 开始列
* @param endCol 结束列
*/
public void addMergedRegion(int beginRow,int endRow,int beginCol,int endCol){
sheet.addMergedRegion(new CellRangeAddress(beginRow,endRow,beginCol,endCol));
}
//当前绘制的HeadRow
private Row timeRow = null;
public boolean writeBody(List<List<String>> e ){
if(e.size()==0){
return true;
}
//重置fcol
this.fCol = 0;
//前两个占2行2列
int eachIndex= 0;
int synNumber = 0;
int syn2=0;
Cell cell = null;
int each = 0 ;
for(List<String> e2:e){
syn2++;
this.fCol=0;
synNumber = 0;
this.rowLine = this.rowLine+1;
Row row = sheet.createRow(this.rowLine);
int size = e2.size();
for (int i = 0; i < size; i++) {
synNumber++;
if(synNumber==1){
this.fCol+=3;
row.setHeight((short)(520));
this.addMergedRegion(this.rowLine,this.rowLine,0,1); //序号
cell =row.createCell(0);
cell.setCellValue(syn2);
this.addMergedRegion(this.rowLine,this.rowLine,2,3);//姓名
cell= row.createCell(2);
fontMiddle(cell,workBook,false,false);
cell.setCellValue(e2.get(i)); //插入姓名-登陆名
increatemntFCol();
continue;
}
if(i==size-4) {
this.addMergedRegion(this.rowLine,this.rowLine,this.fCol++,this.fCol);
Cell cell1 = row.createCell(this.fCol-1);
fontMiddle(cell1,workBook,false,false);
cell1.setCellValue(e2.get(i));
increatemntFCol();
this.addMergedRegion(this.rowLine,this.rowLine,this.fCol++,this.fCol);
cell1 = row.createCell(this.fCol-1);
fontMiddle(cell1,workBook,false,false);
cell1.setCellValue(e2.get(i+1));
increatemntFCol();
this.addMergedRegion(this.rowLine,this.rowLine,this.fCol++,this.fCol);
cell1 = row.createCell(this.fCol-1);
fontMiddle(cell1,workBook,false,false);
cell1.setCellValue(e2.get(i+2));
increatemntFCol();
this.addMergedRegion(this.rowLine,this.rowLine,this.fCol++,this.fCol);
cell1 = row.createCell(this.fCol-1);
fontMiddle(cell1,workBook,false,false);
cell1.setCellValue(e2.get(i+3));
break;
}else {
row.createCell(this.fCol++).setCellValue(e2.get(i));
}
}
}
//尾部合并 Y轴合并 现阶段不做 时间紧 绘制已经完成
// appendHeadTail(this.rowLine);
neline();
return true;
}
/*
private void appendHeadTail(int rowLine) {
//移入下一行
this.rowLine = rowLine+1;
System.out.println(this.rowLine+"appendHeadTail");
//测试list
List<String> tailList = new ArrayList<>();
tailList.add("1");
tailList.add("2");
Row row = sheet.createRow(this.rowLine);
this.addMergedRegion(this.rowLine,this.rowLine,0,3);
Cell xiaojijine = row.createCell(0);
xiaojijine.setCellValue("小计金额");
fontMiddle(xiaojijine,workBook,false,false);
//填充Y轴合并数 从第4列开始
this.fCol = 4;
Cell cell = null;
//TODO !!!!!!44444444444!!!!!!!! 这里要插入尾部小计金额数据
for(String data:tailList){
System.out.println(this.fCol+"::11");
cell = row.createCell(this.fCol);
cell.setCellValue(data);
fontMiddle(cell,workBook,false,false);
increatemntFCol();
}
//生成合计金额
incrementRoadLine();
this.addMergedRegion(this.rowLine,this.rowLine+1,0,3);
Cell hejinjiner = sheet.createRow(this.rowLine).createCell(0);
fontMiddle(hejinjiner,workBook,false,false);
hejinjiner.setCellValue("合计金额");
//TODO !!!!555555555555!!!!!!!!!! 这里要插入尾部合计金额数据
incrementRoadLine();
}*/
/**
* 行自增
*/
private void incrementRoadLine() {
this.rowLine=this.rowLine+1;
}
/**
* 列自增
*/
private void increatemntFCol() {
this.fCol++;
}
/**
* 得到计算列
* @param headSize
* @return
*/
public int getTitleSize(int headSize){
if(headSize==0){
return this.lCol+4-1;
}
return 4+(headSize*5)+8-1+2;
}
/**
* 写出表格
* @param fileName
*/
public void write(String fileName){
FileOutputStream fileOut1 = null;
try {
String fileAbsPath = Constant.EXportPath+fileName+".xlsx";
File f = new File(fileAbsPath);
if(f.exists()){
f.delete();
}
fileOut1 = new FileOutputStream(fileAbsPath);
workBook.write(fileOut1);
fileOut1.flush();
fileOut1.close();
workBook.close();
}catch (Exception e){
e.printStackTrace();
}
}
/**
* TODO 文字居中 是否加粗 是否带颜色
* @param cell 单元格
* @param w 对象
* @param bold 是否加粗
* @param color 是否带颜色
*/
public static void fontMiddle(Cell cell,Workbook w,boolean bold,boolean color){
CellStyle cellStyle = w.createCellStyle();
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
if(bold||color){
Font font = workBook.createFont();
if(bold){
font.setBold(true);
}
if(color){
font.setColor(HSSFFont.COLOR_RED);
}
cellStyle.setFont(font);
}
cell.setCellStyle(cellStyle);
}
/**
* TODO 写入序号账号
endRow为 beginRow+1
*/
private void writeHeadBefore(int beginRow) {
int endRow = beginRow+1; //起新行
Row row = sheet.createRow(beginRow);
/* System.out.println(beginRow+":beginRow");
System.out.println(endRow+":endRow");*/
this.rowLine = endRow; //行+1 为下一次合并做准备
Set<Map.Entry<String, Integer>> entries = TestSXSSDomain.headMaps.entrySet();
for(Map.Entry<String, Integer> entry:entries){
String key = entry.getKey();
if(key.equals("序号")){
this.addMergedRegion(beginRow,endRow,0,1);
Cell synCell = row.createCell(0);
synCell.setCellValue(key);
fontMiddle(synCell,workBook,false,false);
}else {
if(key.equals("账号")){
this.addMergedRegion(beginRow,endRow,2,3);
Cell userCountCell = row.createCell(2);
userCountCell.setCellValue(key);
fontMiddle(userCountCell,workBook,false,false);
}
}
}
// 时间轴
this.fCol=4;
//字段轴
this.constFCol=4;
timeRow = row;
}
/**
* TODO 1 :初始化workSheet
* @return
*/
public Sheet init(){
workBook = new XSSFWorkbook();
sheet = workBook.createSheet("工作业绩");
return sheet;
}
/**
*TODO x轴 尾部追加
*/
public void appendTail(Row row1,Workbook workBook,Sheet sheet,int row,int col,Map<String,String> dataMap){
col=col+1;
Set<Map.Entry<String, String>> entries = dataMap.entrySet();
for(Map.Entry<String, String> entry:entries){
String key = entry.getKey();
//1:创建head
this.addMergedRegion(row,row+1,col,col+1);
Cell cell = row1.createCell(col);
cell.setCellValue(key);
col=col+2;
if(key.equals("差额合计")){
fontMiddle(cell,workBook,false,true);
}else {
fontMiddle(cell,workBook,false,false);
}
}
}
/**
* 换行操作开启下一轮
*/
private void neline(){
this.rowLine = this.rowLine+2;
this.fCol=0;
}
public int getRowLine() {
return rowLine;
}
public void setRowLine(int rowLine) {
this.rowLine = rowLine;
}
public int getRowCol() {
return rowCol;
}
public void setRowCol(int rowCol) {
this.rowCol = rowCol;
}
public int getfCol() {
return fCol;
}
public void setfCol(int fCol) {
this.fCol = fCol;
}
public int getConstFCol() {
return constFCol;
}
public void setConstFCol(int constFCol) {
this.constFCol = constFCol;
}
public int getlCol() {
return lCol;
}
public void setlCol(int lCol) {
this.lCol = lCol;
}
public int getNewLine() {
return newLine;
}
public void setNewLine(int newLine) {
this.newLine = newLine;
}
}