第一步:定义数据结构
public class MarketCollection {//所要返回的List集合数据类型中的每一个成员
private String areaName;
private String province;
private String city;
private String custName;
private String custId;
private String time;
private String year;
private String date;
private double totalPrice;
private String custCode;
private List<ProductBean> product = new ArrayList<ProductBean>(); //含有另一个javabean对象
private List dates = new ArrayList();
//getter setter 方法略去
}
//每一个的ProductBean对象 ,这个对象在MarketCollection以集合的方式被保存
public class ProductBean {
private String productName;
private String productId;
private String originPrice;
private String pruductType;
public ProductBean() { }
public ProductBean(String productName, String productId,
String originPrice, String pruductType) {
super();
this.productName = productName;
this.productId = productId;
this.originPrice = originPrice;
this.pruductType = pruductType;
}
//getter setter 方法略去
}
package com.zeroglobal.wrkmng.excel.market;
import ........................省略;
@Component("marketSumExportDao")
@SuppressWarnings("unchecked")
public class MarketSumExportDaoImpl implements MarketSumExportDao {
@Resource(name="jdbcTemplate")private JdbcTemplate jdbcTemplate;
/**
* 得到产品种类集合
*/
@Override
public List<Map<Object, Object>> allProducts(){
String sql = “sql省略”;
return jdbcTemplate.queryForList(sql);
}
/**
* 查出原始数据
* @return
*/
private List<Map<Object, Object>> getAll(){
String sql = “sql省略”;
sqlString.append("order by c.cust_id asc");//按照标志位排序
return jdbcTemplate.queryForList(sqlString.toString());
}
/**
* 按标志位组装成特定条数数据
* @return
*/
private List<MarketCollection> getCollectData(){
List<MarketCollection> collectData = new ArrayList<MarketCollection>();
List<Map<Object, Object>> mall = getAll();
String cid = "";//定义标志位,以门店ID区分一条记录
double totalPrice = 0;//总价格
MarketCollection c = null;
for (int i = 0; i < mall.size(); i++) {
Map map = mall.get(i);
if (! cid.equals(String.valueOf(map.get("cid")))) {//每次标志位的变化经为collectData 总添加新的一个MarketCollection对象
c = new MarketCollection();
collectData.add(c);
totalPrice = 0;
}
c.setAreaName(String.valueOf(map.get("areaName")));
c.setProvince(String.valueOf(map.get("provinceName")));
c.setCity(String.valueOf(map.get("cityName")));
c.setCustId(String.valueOf(map.get("cid")));
c.setCustCode(String.valueOf(map.get("ccode")));
c.setCustName(String.valueOf(map.get("custName")));
String date = String.valueOf(map.get("ctime"));
c.getDates().add(date);
// if (!date.equals("null")&&!date.equals("")) {
// c.setDate(date);
// c.setYear(C.formatDate(date,"yyyy"));
// c.setTime(C.formatDate(date, "MM-dd HH:mm:ss"));
// }
ProductBean pb = new ProductBean();
pb.setProductId(String.valueOf(map.get("pid")));
pb.setProductName(String.valueOf(map.get("pname")));
String price = String.valueOf(map.get("price"));
pb.setOriginPrice((price.equals("null")||price.equals("")||price.equals("0.00"))?"":price);
totalPrice += (price.equals("null")||price.equals("")||price.equals("0.00"))?0:Double.parseDouble(price);
c.setTotalPrice(totalPrice);
pb.setPruductType(String.valueOf(map.get("categoryId")));
c.getProduct().add(pb);
cid = String.valueOf(map.get("cid")) ;
}
return collectData;
}
/**
* 需要显示的数据
* @return
*/
@Override
public List<MarketCollection> getCollectBackData(){
List<MarketCollection> getCollectBackData = new ArrayList<MarketCollection>();
List<Map<Object, Object>> allProducts = allProducts();
List<MarketCollection> getCollectData = getCollectData();
for (int i = 0; i < getCollectData.size(); i++) {
MarketCollection cold = getCollectData.get(i);
MarketCollection cback = new MarketCollection();
cback.setAreaName(cold.getAreaName());
cback.setProvince(cold.getProvince());
cback.setCity(cold.getCity());
cback.setCustId(cold.getCustId());
cback.setCustCode(cold.getCustCode());
cback.setCustName(cold.getCustName());
String d [] = sortTime(cold.getDates());
cback.setYear(C.formatDate(d[1],"yyyy"));
cback.setTime("从"+C.formatDate(d[0],"MM-dd HH:mm:ss")+"到"+C.formatDate(d[1],"MM-dd HH:mm:ss"));
cback.setTotalPrice(cold.getTotalPrice());
for (int j = 0; j < allProducts.size(); j++) {
Map p = allProducts.get(j);
//has方法用来判断当前ProductBean 是否在allProducts 存在,如果存在则返回对象,不存在返回空对象,也可以返回空
ProductBean pb = has(String.valueOf(p.get("pid")),cold.getProduct());
cback.getProduct().add(pb);
}
getCollectBackData.add(cback);
}
return getCollectBackData;
}
/**
* 判断是否含有
* @param pid 当前种类ID
* @param product 当前product
* @return
*/
private ProductBean has(String pid, List<ProductBean> product) {
for (int i = 0; i < product.size(); i++) {
if (pid.equals(product.get(i).getProductId())) {
return product.get(i);
}
}
//为避免不必要的空指针,所以返回空对象
return new ProductBean("","","","");
}
/**
* 日期排序(因为要返回的字段是一个时间段内的数据)
* @param dates
* @return
*/
private static String[] sortTime(List dates){
long s2 [] = new long[dates.size()];
String backData [] = new String[2];
int size = dates.size();
int index = 0;
for (int i = 0; i <size; i++) {
s2[index++]=Long.parseLong(String.valueOf(dates.get(i)).replaceAll("[-,\\s*,:,\\.[a-zA-Z]]", ""));
}
Arrays.sort(s2);
for (int i = 0; i < dates.size(); i++) {
String d = String.valueOf(dates.get(i)).replaceAll("[-,\\s*,:,\\.[a-zA-Z]]", "");
if (d.equals(String.valueOf(s2[0]))) {
backData[0] = String.valueOf(dates.get(i));
}
if (d.equals(String.valueOf(s2[s2.length-1]))) {
backData[1] = String.valueOf(dates.get(i));
}
}
return backData;
}
//导出为excel格式
public static void main(String[] args) throws Exception{
ApplicationContext a = new ClassPathXmlApplicationContext("spring/applicationContext-*.xml");
MarketSumExportDao dao = (MarketSumExportDao)a.getBean("marketSumExportDao");
System.out.println(dao.getCollectBackData().size());
WritableWorkbook book = Workbook.createWorkbook(new File("d:\\market.xls"));
WritableSheet sheet = book.createSheet("销量汇总报表", 0);
WritableFont wf = new WritableFont(WritableFont.ARIAL, 15,
WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
jxl.format.Colour.CORAL); // 定义格式 字体 下划线 斜体 粗体 颜色
WritableCellFormat wcf = new WritableCellFormat(wf); // 单元格定义
// wcf.setBackground(jxl.format.Colour.BLACK); // 设置单元格的背景颜色
wcf.setAlignment(jxl.format.Alignment.CENTRE); // 设置对齐方式
List<MarketCollection> list = dao.getCollectBackData();
List<Map<Object, Object>> allProducts = dao.allProducts();
sheet.addCell(new Label(0,0,"年"));
sheet.setColumnView(0, 13); // 设置列的宽度
sheet.addCell(new Label(1,0,"时间段"));
sheet.setColumnView(1, 35); // 设置列的宽度
sheet.addCell(new Label(2,0,"区域"));
sheet.setColumnView(2, 13); // 设置列的宽度
sheet.addCell(new Label(3,0,"省份"));
sheet.setColumnView(3, 13); // 设置列的宽度
sheet.addCell(new Label(4,0,"城市"));
sheet.setColumnView(4, 13); // 设置列的宽度
sheet.addCell(new Label(5,0,"门店"));
sheet.setColumnView(5, 20); // 设置列的宽度
sheet.addCell(new Label(6,0,"门店代码"));
sheet.setColumnView(6, 13); // 设置列的宽度
for (int i = 0; i < allProducts.size(); i++) {
Map map = allProducts.get(i);
String pname = map.get("pname")+"("+map.get("categoryId")+")";
sheet.addCell(new Label((6+i+1),0,pname));
sheet.setColumnView((6+i+1), 20); // 设置列的宽度
}
sheet.addCell(new Label(6+allProducts.size(),0,"总计"));
sheet.setColumnView((6+allProducts.size()), 15); // 设置列的宽度
for (int i = 0; i < list.size(); i++) {
MarketCollection m = list.get(i);
sheet.addCell(new Label(0,(i+1),m.getYear()));
sheet.addCell(new Label(1,(i+1),m.getTime()));
sheet.addCell(new Label(2,(i+1),m.getAreaName()));
sheet.addCell(new Label(3,(i+1),m.getProvince()));
sheet.addCell(new Label(4,(i+1),m.getCity()));
sheet.addCell(new Label(5,(i+1),m.getCustName()));
sheet.addCell(new Label(6,(i+1),m.getCustCode()));
List<ProductBean> listpb = m.getProduct();
for (int j = 0; j < listpb.size(); j++) {
ProductBean pb = listpb.get(j);
sheet.addCell(new Label((6+j+1),(i+1),pb.getOriginPrice()));
}
sheet.addCell(new Label(6+allProducts.size(),(i+1),String.valueOf(m.getTotalPrice())));
}
book.write();
book.close();
}
}