【详细介绍】:
描述:将demo数据库中的表feature_product的字段的内容导入随机生成的exl文件中去,相关的字段要对应起来。
1.实现代码:
//将数据库中的有关的数据导入exl中去。
public String AddProduct()
{
try
{
String excelFileName = reExcelFileName().trim() + ".xls";
WritableWorkbook wwb = Workbook.createWorkbook(new File("e:"+excelFileName));
//创建Excel工作表 指定名称和位置
WritableSheet writableSheet = wwb.createSheet("Test Sheet 1",0);
List<Product> productList=productMgr.ShowAll(product);
Label lable1 = new Label(0, 0, "名称");
Label lable2 = new Label(1, 0, "第一段");
Label lable3 = new Label(2, 0, "第二段");
writableSheet.addCell(lable1);
writableSheet.addCell(lable2);
writableSheet.addCell(lable3);
//设置列宽,第1列
writableSheet.setColumnView(0, 50);
//设置列宽,第2列
writableSheet.setColumnView(1, 100);
//设置列宽,第3列
writableSheet.setColumnView(2, 100);
// 写入业务内容
for (int i = 0; i < productList.size(); i++)
{
lable1 = new Label(0, i + 1, productList.get(i).getProductName());
lable2 = new Label(1, i + 1, productList.get(i).getFirstParagraph());
lable3 = new Label(2, i + 1, productList.get(i).getSecondParagraph());
//将标签添加到writableSheet对象中
writableSheet.addCell(lable1);
writableSheet.addCell(lable2);
writableSheet.addCell(lable3);
}
wwb.write();
wwb.close();
}
catch(Exception e)
{
e.printStackTrace();
}
return SUCCESS;
}
//查询出所有的药品信息
public String showAll()
{
try{
this.setProductList(productMgr.ShowAll(product));
}catch(Exception e){}
return SUCCESS;
}
备注:此为在action中的两个方法。具体的业务层的调用参考demo例子的框架。
【完成情况】:完成
【要点小结】:
1.实现思路:
a。创建Excel工作表 指定名称和位置
B.设置列宽
C.写入业务内容
D//产生对象并将标签添加到writableSheet对象中
E 写入到WritableWorkbook对象中
F.关闭WritableWorkbook对象
【参考资料】:
http://www.javaresearch.org/article/76313.htm