1. 程式人生 > >軟件工程實踐記錄p3(day7-9)

軟件工程實踐記錄p3(day7-9)

creates 內容 number 從零開始 軟件工程 工程 bean tty workbook

這三天的主要內容是模仿前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框架的實踐,我熟悉了這個前端web設計到後端功能設計和配置,數據庫的配置。加上軟件工程實踐之前的電子商務實踐,這第二學年的小學期讓我收獲良多。

軟件工程實踐記錄p3(day7-9)