生成excel
action
/*
* 生成excel 表格
*/
public String generateExcel() throws Exception{
getDownloadFile();
return SUCCESS;
}
@SuppressWarnings("unchecked")
public InputStream getDownloadFile() {
liangConditionList = (List<OptNumberDto>)this.getSession().get("liangListss");
Pagination pagination = new Pagination(pageNo, 10);
return ServiceManager.agent.getAdminNumberService().getInputStream(
liangConditionList,pagination);
}
//service层的文件流
@SuppressWarnings({ "deprecation", "deprecation", "unchecked" })
@Override
public InputStream getInputStream(List<OptNumberDto> liangList, Pagination pagination) {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("sheet1");
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("靓号号码");
cell = row.createCell((short) 1);
cell.setCellValue("消费者名称");
cell = row.createCell((short) 2);
cell.setCellValue("号码类型");
cell = row.createCell((short) 3);
cell.setCellValue("备注");
cell = row.createCell((short) 4);
cell.setCellValue("级别");
cell = row.createCell((short) 5);
cell.setCellValue("靓号价格");
List<OptNumberDto> list= liangList;
if(list !=null && list.size() >0){
for (int i = 0; i < list.size(); i++)
{
OptNumberDto optDto = list.get(i);
row = sheet.createRow(i + 1);
cell = row.createCell((short) 0);
cell.setCellValue(optDto.getSpNo());
cell = row.createCell((short) 1);
cell.setCellValue(optDto.getConsuName());
cell = row.createCell((short) 2);
if(optDto.getSpType() == 1){
cell.setCellValue("消费者");
}else if(optDto.getSpType() == 2){
cell.setCellValue("实体店");
}
cell = row.createCell((short) 3);
cell.setCellValue(optDto.getSpComment());
cell = row.createCell((short) 4);
cell.setCellValue(optDto.getSpLevel());
cell = row.createCell((short) 5);
cell.setCellValue(optDto.getSpBuyFee());
}
}
ByteArrayOutputStream os = new ByteArrayOutputStream();
try
{
wb.write(os);
}
catch (IOException e)
{
e.printStackTrace();
}
byte[] content = os.toByteArray();
InputStream is = new ByteArrayInputStream(content);
return is;
}
而后在struts.xml 做相应的配置就可以了
<action name="generateExcel" class="myGoodNumberAction" method="generateExcel">
<result name="success" type="stream">
<param name="contentType">application/vnd.ms-excel</param>
<param name="contentDisposition">attachment;filename="goodNumbers.xls" </param>
<param name="inputName">downloadFile</param>
</result>
</action>
页面只要写给form调用 generateExcel.do 就可以了。