摘自:http://www.iteye.com/topic/130908
package cn.tap2008.commons.core.util;
/**
* @author Tang Anping
* @since Oct 10, 2007
* 报表类
*/
public class ReportBean {
/**
* x轴的值
*/
private String x;
/**
* y轴的值
*/
private String y;
/**
* 统计值
*/
private String value;
/**
* @param x
* @param y
* @param value
*/
public ReportBean(String x, String y, String value) {
super();
this.x = x;
this.y = y;
this.value = value;
}
/**
* @return the x
*/
public String getX() {
return x;
}
/**
* @param x the x to set
*/
public void setX(String x) {
this.x = x;
}
/**
* @return the y
*/
public String getY() {
return y;
}
/**
* @param y the y to set
*/
public void setY(String y) {
this.y = y;
}
/**
* @return the value
*/
public String getValue() {
return value;
}
/**
* @param value the value to set
*/
public void setValue(String value) {
this.value = value;
}
}
package cn.tap2008.commons.core.util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* @author Tang Anping
* @since Oct 10, 2007
* @since 0.1
*/
public class ReportUtil {
/**
* 2007, 北京, 2
* 2007, 上海, 2
* 2006, 广州, 6
* 2006, 南京, 7
* 2005, 湖南, 8
* 2005, 湖北, 9
* 2004, 天津, 3
* 2004, 海南, 7
* @return
*/
public Map getReportMap() {
Map reportMap = new LinkedHashMap();
Set xset = new LinkedHashSet();
xset.add("2007");
xset.add("2006");
xset.add("2005");
xset.add("2004");
Set yset = new LinkedHashSet();
yset.add("北京");
yset.add("上海");
yset.add("广州");
yset.add("南京");
yset.add("湖南");
yset.add("湖北");
yset.add("天津");
yset.add("海南");
List reportList = new ArrayList();
//x, y, 统计
reportList.add(new ReportBean("2007", "北京", "2"));
reportList.add(new ReportBean("2007", "上海", "2"));
reportList.add(new ReportBean("2006", "广州", "6"));
reportList.add(new ReportBean("2006", "南京", "7"));
reportList.add(new ReportBean("2005", "湖南", "8"));
reportList.add(new ReportBean("2005", "湖北", "9"));
reportList.add(new ReportBean("2004", "天津", "3"));
reportList.add(new ReportBean("2004", "海南", "7"));
HashMap y_map = new HashMap();
y_map.put(yset, reportList);
reportMap.put(xset, y_map);
//System.out.println(xset + "=========" + y_map);
return reportMap;
}
/**
* 2007, 北京, 2
* 2007, 上海, 2
* 2006, 广州, 6
* 2006, 南京, 7
* 2005, 湖南, 8
* 2005, 湖北, 9
* 2004, 天津, 3
* 2004, 海南, 7
* @param tableHead 纵轴的表头
* @param xlist 横轴
* @param ylist 纵轴
* @param reportList 记录数据
* @return
*/
public String[][] getReportArray(String tableHead, List xlist, List ylist, List reportList) {
int x_len = xlist.size();
int y_len = ylist.size();
//第一维数组为y轴,第二维数组为x轴
String[][] reportArray = new String[y_len + 2][x_len + 2];
reportArray[0][0] = tableHead;
reportArray[0][x_len + 1] = "总计";
for(int x = 1; x < x_len + 1; x++) {
reportArray[0][x] = (String) xlist.get(x - 1);
}
for(int y = 1; y < y_len + 1; y++) {
reportArray[y][0] = (String) ylist.get(y - 1);
}
int report_len = reportList.size();
double amount = 0;
double[] y_amount = new double[x_len + 2];
for(int y = 1; y < y_len + 1; y++) {
double x_count = 0;
for(int x = 1; x < x_len + 1; x++) {
for(int k = 0; k < report_len; k++) {
ReportBean reportBean = (ReportBean) reportList.get(k);
if(reportBean.getX().equals(xlist.get(x - 1)) && reportBean.getY().equals(ylist.get(y - 1))) {
String value = reportBean.getValue();
reportArray[y][x] = reportBean.getValue();
x_count += Double.parseDouble(value);
amount += Double.parseDouble(value);
}
}
//如果不存在数据,则为0
if(reportArray[y][x] == null) {
reportArray[y][x] = "0";
}
y_amount[x] += Double.parseDouble(reportArray[y][x]);
reportArray[y_len + 1][x] = y_amount[x] + "";
}
reportArray[y][x_len + 1] = x_count + "";
}
reportArray[y_len + 1][0] = "合计";
reportArray[y_len + 1][x_len + 1] = amount + "";
return reportArray;
}
public String printTable(String[][] reportArray) {
StringBuffer tableBuffer = new StringBuffer("<table border='1'>");
for(int i = 0; i < reportArray.length; i++) {
tableBuffer.append("<tr>");
for(int j = 0; j < reportArray[i].length; j++) {
if(reportArray[i][j] == null) {
reportArray[i][j] = "0";
}
tableBuffer.append("<td align='center'>").append(reportArray[i][j]).append("</td>");
}
tableBuffer.append("</tr>");
}
tableBuffer.append("</table>");
return tableBuffer.toString();
}
public static void main(String[] args) {
List xlist = new ArrayList();
xlist.add("2007");
xlist.add("2006");
xlist.add("2005");
xlist.add("2004");
List ylist = new LinkedList();
ylist.add("北京");
ylist.add("上海");
ylist.add("广州");
ylist.add("南京");
ylist.add("湖南");
ylist.add("湖北");
ylist.add("天津");
ylist.add("海南");
List reportList = new ArrayList();
//x, y, 统计
reportList.add(new ReportBean("2007", "北京", "2"));
reportList.add(new ReportBean("2007", "上海", "2"));
reportList.add(new ReportBean("2006", "广州", "6"));
reportList.add(new ReportBean("2006", "南京", "7"));
reportList.add(new ReportBean("2005", "湖南", "8"));
reportList.add(new ReportBean("2005", "湖北", "9"));
reportList.add(new ReportBean("2004", "天津", "3"));
reportList.add(new ReportBean("2004", "海南", "7"));
String[][] reportArray = new ReportUtil().getReportArray("分类", xlist, ylist, reportList);
System.out.println(new ReportUtil().printTable(reportArray));
for(int i = 0; i < reportArray.length; i++) {
for(int j = 0; j < reportArray[i].length; j++) {
System.out.print(reportArray[i][j] + " ");
}
System.out.println();
}
}
}