Dynamic Bean 使用案例

4:16 下午 0 Comments

DynaProperty[] props = new DynaProperty[9+storeList.size()];

int row = 0;
props[row++] = new DynaProperty("itemCode", String.class);
props[row++] = new DynaProperty("cname", String.class);
props[row++] = new DynaProperty("remark", String.class);
props[row++] = new DynaProperty("mainSupp", String.class);
props[row++] = new DynaProperty("realSupp", String.class);
props[row++] = new DynaProperty("suppCname", String.class);
props[row++] = new DynaProperty("dcStock", Number.class);
props[row++] = new DynaProperty("storeStock", Number.class);
props[row++] = new DynaProperty("totalStock", Number.class);


BasicDynaClass dynaClass = new BasicDynaClass("saleSearch",null,props);
for(....)
{
DynaBean saleSearch = dynaClass.newInstance();
CfstMstoreModelExt m = (CfstMstoreModelExt) itr.next();
saleSearch.set("itemCode",m.getItemCode());
saleSearch.set("cname",m.getCname());
saleSearch.set("remark",m.getRemark());
saleSearch.set("mainSupp",m.getMainSupp());
saleSearch.set("realSupp",m.getRealSupp());
saleSearch.set("suppCname",m.getSuppCname());
saleSearch.set("dcStock",m.getDcStock());
saleSearch.set("storeStock",m.getTotalStock());
................
}

他是master 在最前面,然後,後面跟著數量不一定的detail, 還要可以排序
-------------------------------------------------------------------------------
匯出Excel

public HSSFWorkbook writeToExcel()
throws Exception
{
//in poi;

HSSFWorkbook wb = new HSSFWorkbook();
HSSFCellStyle center = wb.createCellStyle();
HSSFCellStyle right = wb.createCellStyle();
HSSFCellStyle left = wb.createCellStyle();
center.setAlignment(HSSFCellStyle.ALIGN_CENTER);
right.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
left.setAlignment(HSSFCellStyle.ALIGN_LEFT);

HSSFSheet sheet = wb.createSheet();
Date date = new Date();
wb.setSheetName(0,"庫存資料查詢",HSSFWorkbook.ENCODING_UTF_16);

int rowCnt = 0;
int cellCnt = 0;
HSSFRow row = null;
HSSFCell cell = null;
//開始寫明細檔資料
row = sheet.createRow((short)rowCnt++);
cellCnt = 0;
for(Iterator itr = headerList.iterator(); itr.hasNext();)
{
String header = (String) itr.next();
cell = row.createCell((short) cellCnt++);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(header);

}
for (Iterator iter = this.valueList.iterator(); iter.hasNext();)
{
DynaBean d = (DynaBean) iter.next();
row = sheet.createRow((short)rowCnt++);
cellCnt = 0;
for(Iterator fieldItr = fieldList.iterator(); fieldItr.hasNext();)
{
Object o = fieldItr.next();
cell = row.createCell((short) cellCnt++);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
if(o instanceof Number)
{
cell.setCellValue(String.valueOf(((Number)o).intValue()));
}
else
{
cell.setCellValue(String.valueOf(o.toString()));
}
}
}
return wb;
}
-------------------------------------------------------------------------------
OUT TO RESPONSE
SaleSearchVO vo = (SaleSearchVO)request.getSession().getAttribute("saleSearchResult");
HSSFWorkbook wb = vo.writeToExcel();
response.setContentType("application/octet-stream; charset=MS950");
//request.setCharacterEncoding("MS950");
String fileName = "CarreFour_Sales_Search.xls";
response.setHeader(
"Content-Disposition",
"attachment;filename="+fileName);
OutputStream out = response.getOutputStream();

wb.write(out);

0 意見: