java excel 透视

这里面的透视的主要思路是java通过模板导出数据,这个模板是写好带透视的

package com.test;

import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class PoiChart {
public static void main(String[] args){

try{

POIFSFileSystem fs=new POIFSFileSystem(new FileInputStream("d:/chart.xls"));//E:/Module.xls

HSSFWorkbook wb=new HSSFWorkbook(fs);

HSSFSheet sheet=wb.getSheetAt(0);

HSSFRow row;

//写入表头

String[] aTblHeaders=new String[]{

"姓名","语文","数学","英语","物理" ,"计算机"

};

row=sheet.createRow(0);

for(int i=0,len=aTblHeaders.length;i<len;i++){

row.createCell((short)i).setCellValue(aTblHeaders[i]);

}

//向模板中写入数据

String[] aNames=new String[]{

"张飞","刘备","关羽","曹操","孙权","吕布","小布"

};

for(int i=0,nRowLen=aNames.length;i<nRowLen;i++){

row=sheet.createRow(i+1);

row.createCell((short)0).setCellValue(aNames[i]);

for(int j=1,nColLen=aTblHeaders.length;j<nColLen;j++){

row.createCell((short)j).setCellValue(Math.floor(Math.random()*1000));

}

}

//输出文件

FileOutputStream fout=new FileOutputStream("d:/chart.xls");

wb.write(fout);

fout.close();

}catch(Exception e){

e.printStackTrace();

}

}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值