软件工程实践记录p3(day7-9)

这三天的主要内容是模仿前6天的客户关系管理系统创建库存管理系统,大体框架和客户系统类似,增加了时间记录、根据数据生成excel文件,另对界面进行了美化。

 

 

增加时间相关代码

Date date = new Date();
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String stocktime = format.format(date);
        item.setStocktime(stocktime);

生成excel相关代码

GenerateExcelAction.java

package com.crm.action;

import java.io.InputStream;

import com.crm.service.ItemService;
import com.opensymphony.xwork2.ActionSupport;

public class GenerateExcelAction extends ActionSupport{
    private ItemService excelService;
    public  ItemService getExcelService(){
        return excelService;
    }
    public void setExcelService(ItemService excelService){
        this.excelService =excelService;
    }
    public InputStream getDownloadFile(){
        return this.excelService.getInputStream();
        
    }
    @Override
    public String execute() throws Exception{
        //TODO Auto-generated method stub
        return SUCCESS;
    }


}

ItemDaoImpl.java创建表格代码

public InputStream getInputStream(){
        //Apache moi hssf对象
        HSSFWorkbook wb = new HSSFWorkbook();
        //创建sheet
        HSSFSheet sheet = wb.createSheet("sheet1");
        //表头开始//创建行
        HSSFRow row = sheet.createRow(0);
        //创建单元格 第一个单元格从零开始 第一列
        HSSFCell cell = row.createCell((short)0);
        //设置编码
        cell.setEncoding(HSSFCell.ENCODING_UTF_16);
        cell.setCellValue("商品编号");
        //第二列
        cell = row.createCell((short)1);
        cell.setEncoding(HSSFCell.ENCODING_UTF_16);
        cell.setCellValue("名称");
        //第三列
        cell = row.createCell((short)2);
        cell.setEncoding(HSSFCell.ENCODING_UTF_16);
        cell.setCellValue("数量");
        //第四列
        cell = row.createCell((short)3);
        cell.setEncoding(HSSFCell.ENCODING_UTF_16);
        cell.setCellValue("价格");
        //第五列
        cell = row.createCell((short)4);
        cell.setEncoding(HSSFCell.ENCODING_UTF_16);
        cell.setCellValue("种类");
        //第六列
        cell = row.createCell((short)5);
        cell.setEncoding(HSSFCell.ENCODING_UTF_16);
        cell.setCellValue("入库时间");
        //表头结束//查询数据库
        List<Item> list =this.ItemDao.findAllItem();
        for(int i=0;i<list.size();++i){
            Item item = list.get(i);
            //把数据放到表格中
            row = sheet.createRow(i+1);
            cell = row.createCell((short)0);
            cell.setEncoding(HSSFCell.ENCODING_UTF_16);
            cell.setCellValue(item.getItemnumber());
            cell = row.createCell((short)1);
            cell.setEncoding(HSSFCell.ENCODING_UTF_16);
            cell.setCellValue(item.getItemname());
            cell = row.createCell((short)2);
            cell.setEncoding(HSSFCell.ENCODING_UTF_16);
            cell.setCellValue(item.getItemamount());
            cell = row.createCell((short)3);
            cell.setEncoding(HSSFCell.ENCODING_UTF_16);
            cell.setCellValue(item.getItemvariety());
            cell = row.createCell((short)4);
            cell.setEncoding(HSSFCell.ENCODING_UTF_16);
            if("1".equals(item.getItemvariety()){
                cell.setCellValue("日用品");
            }else if( "2".equals(item.getItemvariety()){
                    cell.setCellValue("数码科技");
            }else if("3".equals(item.getItemvariety())){
                    cell.setCellValue("其他");
            }
            cell = row.createCell((short)5);
            cell.setEncoding(HSSFCell.ENCODING_UTF_16);
            cell.setCellValue(item.getItemprice());    
            }
            //根据输出流,输出到文件中
        File file = new File("item.xml");
        try{
            OutputStream os = new FileOutputStream(file);
            //输出写入到文件中
            wb.write(os);
            //关闭输出流
            os.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        InputStream is null;
        try {
            is = new FileInputStream(file);
        }catch (FileNotFoundExcept e){
            e.printStackTrace();
        }
        return is;
        }

 

struts.xml(

jsp->struts.xml->前端映射
->后端Action映射

)配置

    <!-- 导出excel -->
        <action name="GenerateExcelAction" class="GenerateExcelAction">
        <result name="success" type="stream">
        <param name="contentType">application/vnd.ms-excel</param>
        <param name="contentDisposition">filename="AllItem.xls"</param>
        <param name="inputName">downloadFile</param>
        </result>
        </action>

applicationContext.xml配置

<!-- 配置excel -->
    <bean id="GenerateExcelAction" class="com.crm.action.GenerateExcelAction">
    <property name="getInputStream" ref="itemService"></property>
    </bean>

ItemInfo.jsp增加“生成excel”按键:

<input width="100" type = "button" value="生成excel" onClick="funExcel();"/>

HTML添加背景图片:

<BODY background="图片路径">
//图片路径:比如图片保存在images文件夹

 通过自己的亲身对ssh(struts2,spring ,hibernate)框架的实践,我熟悉了这个前端web设计到后端功能设计和配置,数据库的配置。加上软件工程实践之前的电子商务实践,这第二学年的小学期让我收获良多。

posted @ 2017-07-05 11:10  夜尉迟  阅读(162)  评论(0编辑  收藏  举报