Javaサービス側excelファイルxlsフォーマットの内容を読み込む


                 excel    ,excel               。        ,                          ,          :
<html>
<body>
<div class="offset1 span10 container-fluid">
            <form id="firmForm" class="form-horizontal"
                action="/front/cashFlow/payForAnotherInformationList.htm"
                method="post" enctype="multipart/form-data" accept-charset="utf-8">

                <div class="control-group">
                    <div class="control-group">
                        <label class="control-label" for="input01">    :label>
                        <div class="controls" style="padding-top: 5px">
                            <input id="entrustFile" name="entrustFile" type="file" value="  ">
                        div>
                    div>

                <input name="btnSubmit" type="button" class="tjBtn" id="btnSubmit"
                        value="  " onclick="javascript:submit();"/>

            form>
    div>

body>
html>

<script type="text/javascript">
    function submit() {
        document.getElementById("firmForm").submit();
    }
script>

クライアントの仕事はファイルのテストをアップロードすることができて、今主にサービス側の仕事を紹介して、まず私はexcelファイルの内容を解析するために1つのフレームワークを導入します:poi-3.9-20121203.jarそれから私達の第1歩はrequestの中からこのファイルを取得して、HttpServiceletRequestタイプの要求をMultipartHttpServiceletRequestタイプの要求に強く転換して、次に、M u l t i p artHttpServertRequestには、ファイル名でファイルを取得し、ファイルを取得した後、getInputStream()関数でファイルの内容を取得する方法があります.
    /**
     * Return the contents plus description of an uploaded file in this request,
     * or {@code null} if it does not exist.
     * @param name a String specifying the parameter name of the multipart file
     * @return the uploaded content in the form of a {@link MultipartFile} object
     */
    MultipartFile getFile(String name);

次にソースコードを直接見ます.
    /**
     *   excel  
     */
    public List haveFileFromClient(HttpServletRequest request) throws IOException {
        //    MultipartHttpRequest:
        MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
        //     :
        MultipartFile contactFile = multiRequest.getFile("entrustFile");
        //          
        List resultList = new ArrayList();
        if (null != contactFile && !contactFile.isEmpty()) {
            //    
            POIFSFileSystem fs;
            //      
            HSSFWorkbook wb;
            //  
            HSSFSheet sheet;
            //  
            HSSFRow row;
            //     
            try {

                fs = new POIFSFileSystem(contactFile.getInputStream());
                wb = new HSSFWorkbook(fs);
            } catch (IOException e) {
                e.printStackTrace();
                wb = new HSSFWorkbook();
            }
            String entrustNo = RandomStringUtils.randomNumeric(9);
            //     
            sheet = wb.getSheetAt(0);
            //      
            int rowNum = sheet.getLastRowNum();
            //             ,         
            for (int i = 1; i <= rowNum; i++) {
                String outNo = entrustNo + "_" + RandomStringUtils.randomNumeric(6);
                row = sheet.getRow(i);
                String accountType = "";
                String accountName = "";
                String accountNum = "";
                String bankName = "";
                String bankProv = "";
                String bankCity = "";
                BigDecimal amount = null;
                String bankBranch = "";
                String reason = "";
                try {
                    int idx = 0;
                    idx++;
                    accountType = getCellFormatValue(row.getCell(idx));
                    idx++;
                    accountName = getCellFormatValue(row.getCell(idx));
                    idx++;
                    accountNum = getCellFormatValue(row.getCell(idx));
                    idx++;
                    bankName = getCellFormatValue(row.getCell(idx));
                    idx++;
                    bankProv = getCellFormatValue(row.getCell(idx));
                    idx++;
                    bankCity = getCellFormatValue(row.getCell(idx));
                    idx++;
                    amount = new BigDecimal(getCellFormatValue(row.getCell(idx)));
                    idx++;
                    bankBranch = getCellFormatValue(row.getCell(idx));
                    idx++;
                    reason = getCellFormatValue(row.getCell(idx));

                    CashOutRecord cashEntrustRecord = new CashOutRecord();
                    cashEntrustRecord.setOutNo(outNo);
                    cashEntrustRecord.setAccountname(accountName);
                    cashEntrustRecord.setAccountnum(accountNum);
                    cashEntrustRecord.setBankname(bankName);
                    cashEntrustRecord.setBankbranch(bankBranch);
                    cashEntrustRecord.setBankprov(bankProv);
                    cashEntrustRecord.setBankcity(bankCity);
                    cashEntrustRecord.setAmount(amount);
                    if (accountType.equals("    ")) {
                        cashEntrustRecord.setAccounttype(11);
                    } else if (accountType.equals("    ")) {
                        cashEntrustRecord.setAccounttype(12);
                    }
                    cashEntrustRecord.setRemark(reason);
                    resultList.add(cashEntrustRecord);
                } catch (Exception ex) {
                    System.out.print(ex);
                }
            }
            return resultList;
        }
        return null;
    }


----------
    excel                  
----------


    /**
     *   excel  cell   
     */
    private String getCellFormatValue(HSSFCell cell) {
        String cellvalue = "";
        if (cell != null) {
            //     Cell Type
            switch (cell.getCellType()) {
            //     Cell Type NUMERIC
            case HSSFCell.CELL_TYPE_NUMERIC:
            case HSSFCell.CELL_TYPE_FORMULA: {
                //      cell   Date
                if (HSSFDateUtil.isCellDateFormatted(cell)) {
                    Date date = cell.getDateCellValue();
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    cellvalue = sdf.format(date);
                }
                //       
                else {
                    //     Cell   
                    cellvalue = String.valueOf(cell.getNumericCellValue());
                }
                break;
            }
            //     Cell Type STRIN
            case HSSFCell.CELL_TYPE_STRING:
                //      Cell   
                cellvalue = cell.getRichStringCellValue().getString();
                break;
            //    Cell 
            default:
                cellvalue = " ";
            }
        } else {
            cellvalue = "";
        }
        return cellvalue;
    }