这里面的透视的主要思路是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();
}
}
}
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();
}
}
}