以下是整个代码:
package open.report.excel.crosstab;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import open.model.Field;
import open.model.IRecordSetVO;
import open.model.ModelException;
import open.model.RecordSetVO;
import open.model.RecordVO;
import open.report.config.sheet.CrossTab;
import open.report.config.sheet.crosstab.ColField;
import open.report.config.sheet.crosstab.CrossStatField;
import open.report.config.sheet.crosstab.RowField;
import open.report.config.sheet.common.CrossValueField;
import open.report.config.sheet.common.Font;
import open.report.config.sheet.common.StatField;
import open.report.excel.tab.CrossTabOut;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class ReportSample {
/**
* @param args
*/
static public void main(String[] args) throws Exception{
POIFSFileSystem fs = null;
HSSFWorkbook wb = null;
try {
fs = new POIFSFileSystem(new FileInputStream("d:/openhandx/report/test2.xls"));
wb = new HSSFWorkbook(fs);
wb.getSheetAt(0);
CrossTab configs = new CrossTab();
configs.setValueType(CrossTab.VALUETYPE_LENGTHWAYS);
configs.setX(1);
configs.setY(1);
configs.setValueField(getValueField());
configs.setRowField(getRowField());
configs.setColField(getColField());
configs.setStatField(new ArrayList<CrossStatField>());
CrossStatField crossStatField = new CrossStatField();
crossStatField.setText("总计");
crossStatField.setFont(new Font());
crossStatField.getFont().setColor((short)4);
configs.getStatField().add(crossStatField);
IRecordSetVO rsvo = getData();
System.out.println(rsvo);
new CrossTabOut().write(wb,wb.getSheetAt(0),configs,rsvo);
wb.write(new FileOutputStream("d:\\Book5.xls"));
} catch (Exception e) {
e.printStackTrace();
}
}
//设置行
public static RowField getRowField(){
RowField rowField = new RowField();
rowField.setName("Name");
return rowField;
}
//设置列
public static ColField getColField(){
ColField colField = new ColField();
colField.setName("School");
return colField;
}
//交叉表字段配置
public static List<CrossValueField> getValueField(){
List<CrossValueField> valueFields = new ArrayList<CrossValueField>();
CrossValueField valueField = new CrossValueField();
valueField.setName("Salary");
valueFields.add(valueField);
return valueFields;
}
//数据
public static IRecordSetVO getData() throws ModelException{
IRecordSetVO rsvo = new RecordSetVO();
//添加数据列
rsvo.addField("Name", Field.DataType.STRING);
rsvo.addField("Sex",Field.DataType.STRING);
rsvo.addField("School",Field.DataType.STRING);
rsvo.addField("Salary",Field.DataType.BIGDECIMAL);
//添加数据
RecordVO rvo = new RecordVO();
rvo.add("Name", "张三");
rvo.add("Sex", "男");
rvo.add("School", "博士");
rvo.add("Salary", 10000);
rsvo.add(rvo);
rvo = new RecordVO();
rvo.add("Name", "李四");
rvo.add("Sex", "女");
rvo.add("School", "研究生");
rvo.add("Salary", 7000);
rsvo.add(rvo);
rvo = new RecordVO();
rvo.add("Name", "王二");
rvo.add("Sex", "男");
rvo.add("School", "本科");
rvo.add("Salary", 4200);
rsvo.add(rvo);
rvo = new RecordVO();
rvo.add("Name", "刘三");
rvo.add("Sex", "男");
rvo.add("School", "研究生");
rvo.add("Salary", 6200);
rsvo.add(rvo);
rvo = new RecordVO();
rvo.add("Name", "小红");
rvo.add("Sex", "女");
rvo.add("School", "博士");
rvo.add("Salary", 9200);
rsvo.add(rvo);
rvo = new RecordVO();
rvo.add("Name", "小刘");
rvo.add("Sex", "女");
rvo.add("School", "本科");
rvo.add("Salary", 5200);
rsvo.add(rvo);
rvo = new RecordVO();
rvo.add("Name", "小光");
rvo.add("Sex", "男");
rvo.add("School", "大专");
rvo.add("Salary", 2200);
rsvo.add(rvo);
return rsvo;
}
}
回复 更多评论