multipartResolver

1. <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />

public class CardSecretForm {
private Long id;
private String cardsecret;
private Long upperid;
private MultipartFile fdata;
private String iscover;

 ................................

}

 @RequestMapping(params="method=importCardSecretListExcel")
   public void importCardSecretListExcel(CardSecretForm  cardSecretForm,HttpServletRequest request, HttpServletResponse response) throws IOException 
   {
String user=(String) request.getSession(true).getAttribute(MgrConstant.SESSION_KEY);
String iscover = cardSecretForm.getIscover();
  boolean cover = "1".equals(iscover);
    int ignoreRows = 1;
    int ignoreSheets = -1;
    Long upperid=cardSecretForm.getUpperid();
    InputStream in = null;
    if(cardSecretForm.getUpperid()!=null)
    {
    try 
    {
    in = cardSecretForm.getFdata().getInputStream();
    List<String[]> result = ExcelUtil.importExcel(new BufferedInputStream(in), ignoreRows, ignoreSheets);
    for(String[] array:result)
   
    String cardSecret=array[0];
    if(cardSecret!=null)
    {
   
    CardSecretCondition   condition=new CardSecretCondition();
    condition.setCardsecret(cardSecret);
    condition.setUpperid(upperid);
    List<FcCardSecret> list=new ArrayList<FcCardSecret>();
try {
list = fcCardSecretService.getAllFcCardSecrets(condition,null);
} catch (Exception e2) {

e2.printStackTrace();
}
    if(list!=null&&list.size()>0)
    {
    if(cover)
    {
    //edit
    if(list!=null&&list.size()>0){
    for(FcCardSecret  secret:list){
    secret.setCardsecret(secret.getCardsecret());
    try 
    {
    fcCardSecretService.saveFcCardSecret(secret);

    catch (Exception e) 
    {
    logger.error("edit fcBlackRoster failed!",e);
}
   
    }
    }
   
    }
    }
    else
    {
    //new
    FcOrderUpperInfo fcOrderUpperInfo=new FcOrderUpperInfo();
try {
fcOrderUpperInfo = fcOrderUpperInfoService.getById(upperid);
} catch (Exception e1) {

e1.printStackTrace();
}
    FcCardSecret fcCardSecret = new FcCardSecret();
    fcCardSecret.setCardsecret(cardSecret);
    fcCardSecret.setUpperid(upperid);
    fcCardSecret.setCreateTime(new Date());
    fcCardSecret.setUpdateTime(new Date());
    fcCardSecret.setModifier(user);
    fcCardSecret.setStatus(1);
    fcCardSecret.setUppername(fcOrderUpperInfo.getName());
    fcCardSecret.setStatus(0);//0启用
                try 
                {
       
        fcCardSecretService.saveFcCardSecret(fcCardSecret);
}
                catch (Exception e) 
                {
logger.error("add importCardSecretListExcel failed!",e);
}
    }
    }
    }
   

    catch (IOException e) 
    {
    logger.error("import list failed!",e);
}
    finally
    {
    if(in!=null)
    {
    try 
    {
in.close();

    catch (IOException e) {}
    }
    }
    }
    response.sendRedirect("cardSecret.do?method=manage");
   }


3.

@SuppressWarnings({ "deprecation" })
public static List<String[]> importExcel(BufferedInputStream in, int ignoreRows, int ignoreSheets)
throws FileNotFoundException, IOException 
{
List<String[]> result = new ArrayList<String[]>();
int rowSize = 0;
// 打开HSSFWorkbook
POIFSFileSystem fs = null;
HSSFWorkbook wb = null;
HSSFCell cell = null;
try {
fs = new POIFSFileSystem(in);
wb = new HSSFWorkbook(fs);
for (int sheetIndex = 0; sheetIndex < wb.getNumberOfSheets(); sheetIndex++)
{
if(ignoreSheets!=-1&&sheetIndex + 1 > ignoreSheets)
break;

HSSFSheet st = wb.getSheetAt(sheetIndex);
// 第一行为标题,不取
for (int rowIndex = ignoreRows; rowIndex <= st.getLastRowNum(); rowIndex++) 
{
HSSFRow row = st.getRow(rowIndex);
if (row == null)
{
//空白行也记录
result.add(new String[rowSize]);
continue;
}
//没有指定索引的那一行,则不记录
if (row.getLastCellNum() == -1)
{
result.add(null);
continue;
}
int tempRowSize = row.getLastCellNum() + 1;
if (tempRowSize > rowSize) 
{
rowSize = tempRowSize;
}
String[] values = new String[rowSize];
Arrays.fill(values, "");
for (short columnIndex = 0; columnIndex <= row.getLastCellNum(); columnIndex++) 
{
String value = "";
cell = row.getCell(columnIndex);
if (cell != null) 
{
switch (cell.getCellType()) 
{
case HSSFCell.CELL_TYPE_STRING:
value = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_NUMERIC:
if (HSSFDateUtil.isCellDateFormatted(cell)) 
{
Date date = cell.getDateCellValue();
if (date != null)
{
value = new SimpleDateFormat("yyyy-MM-dd")
.format(date);
}
else 
{
value = "";
}
}
else 
{
value = new DecimalFormat("0").format(cell
.getNumericCellValue());
}
break;
case HSSFCell.CELL_TYPE_FORMULA:
// 导入时如果为公式生成的数据则无值
if (!cell.getStringCellValue().equals(""))
{
value = cell.getStringCellValue();

else
{
value = cell.getNumericCellValue() + "";
}
break;
case HSSFCell.CELL_TYPE_BLANK:
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
value = (cell.getBooleanCellValue() == true ? "Y"
: "N");
break;
default:
value = "";
}
}
values[columnIndex] = rightTrim(value);
}
result.add(values);
}
}
} catch (Exception e) {
e.printStackTrace();
}
finally
{
wb = null;
fs = null;
if(in!=null)
in.close();
}
return result;
}


4.

<!-- 上传excel表单 -->
    <div id="uploadWindow" class="mini-window" title="导入卡密信息" style="width:580px;" showModal="true" allowResize="true" allowDrag="true">
        <form id="fcCardSecretExcelForm" method="post" 
        action="cardSecret.do?method=importCardSecretListExcel"
        enctype="multipart/form-data"> 
        <table style="width:100%;">
                <tr>
                <td style="width:100px;"><font class="required">*</font>Excel文件:</td>
                <td style="width:150px;">
                <input name="fdata" style="width:400px;" class="mini-htmlfile"  limitType="*.xls;*.xlsx" required="true" />
                </td>
                 </tr>
                 <tr>
                <td style="width:100px;"><font class="required">*</font>上游ID:</td>
                    <td style="width:150px;">
                        <input name="upperid" 
                        class="mini-text" style="width:400px;"  required="true"/>
                    </td>
                </tr>
                <tr>
                <td style="width:100px;"><font class="required">*</font>如果卡密重复:</td>
                    <td style="width:150px;">
                    <input name="iscover" style="width:400px;" class="mini-radiobuttonlist" 
              data="[{id: '0', text: '忽略'}, {id: '1', text: '覆盖'}]" required="true"/>
              </td>
              </tr>
                <tr>
                <td><input type="submit"  value="上传"/></td>
                <td><input type="button"  οnclick="onUploadCancel()" value="取消"/></td>
                </tr>
            </table>
        </form>
</div>

5.

<script type="text/javascript">
    mini.parse();
    var uploadWindow = mini.get("uploadWindow");
    var fcCardSecretExcelForm = new mini.Form("#fcCardSecretExcelForm");
    function importExcel() 
    {
    uploadWindow.show();
   
    }


    function onUploadCancel(){
        uploadWindow.hide();
   }     
</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ygl6150373

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值