通过工具类完成Excel表的格式转换(先导入后导出)


***一.jsp页面***
-------------
/*引入struts2标签和js*/
<%@ taglib uri="/struts-tags" prefix="s" %>
<script type="text/javascript" src="../script/jquery.js"></script>
<s:form id="form2" name="form1" action="" method="post" 
    enctype="multipart/form-data" theme="simple" target="">
    <table width="80%" border="1" align="center" class="DB_table">
        <tr>
            <td colspan="99" align="left">文件上传</td>
        </tr>
        <tr>
            <td colspan="99">
                <s:file name="uploadFile" label="文件位置" size="60" />
                <input type="hidden" name="importNo" id="importNo" />
                <input type="hidden" name="fileName" id="fileName" />
            </td>
        </tr>
        <tr>
            <td colspan="99" align="right">
                <input type="button" value="转换" onclick="transCateTypeOFYYF()"/>
            </td>
        </tr>
    </table>
</s:form>
/*jsp页面函数:*/
function transCateTypeFBFXOFYYF(){
        var form = document.getElementById("form3");
        form.target="_blank";
        form.action="transCateTypeFBFXOFYYF.do";
        form.submit();
    }


**二.action**
------------

/*注意get  set 方法 开始*/
private File uploadFile;
private String uploadFileFileName;

public File getUploadFile() {
    return uploadFile;
}
public void setUploadFile(File uploadFile) {
    this.uploadFile = uploadFile;
}
public String getUploadFileFileName() {
    return uploadFileFileName;
}
public void setUploadFileFileName(String uploadFileFileName) {
    this.uploadFileFileName = uploadFileFileName;
}
/*注意get  set 方法 结束*/

public void transCateTypeOFYYF() throws ParseException{
/**一、 读取数据*/
log.info("transCateTypeOFYYF.. 开始转换...");
List<Map<String, Object>> list=new ArrayList<Map<String, Object>>();//合同数据
String msg = "success";
Workbook rwb = null;
JSONObject jObj = new JSONObject();
try {
    /*1.1 校验上传文件*/
    /*判断文件名是否为空*/
    if(uploadFileFileName ==null || "".equals(uploadFileFileName)){
        msg="上传文件不能为空,请选择上传文件!";
        jObj.put("msg", msg);
        JSONUtils.printObject(jObj);
        return;
    }
    /*先判断上传的文件类型*/
    String fileEndNameString = uploadFileFileName.substring(uploadFileFileName.indexOf(".")+1);
    if(!"xls".equals(fileEndNameString)){
        msg="上传文件类型不对,请上传03版本的excel文件!";
        jObj.put("msg", msg);
        JSONUtils.printObject(jObj);
        return;
    }

    rwb=Workbook.getWorkbook(uploadFile);
    /*1.2 读取交易记录数据*/
    Sheet rs=rwb.getSheet(0);
    int rows=rs.getRows();//得到所有的行
    //封装excel数据的map
        Map<String, Object> map;
   /*1.3遍历所有的行,读取数据*/
   for (int i = 1; i < rows; i++) {
      //第一个是列数,第二个是行数
String rawNo=rs.getCell(0, i).getContents();//原始数据序号
/* 如果为空行,则直接跳出循环*/
if(StringUtils.isEmpty(rawNo)){
    break;
}else if(StringUtils.isEmpty(rawNo.trim())){
    break;
}
String yearRate = rs.getCell(20,i).getContents();//收益率
String staffName = rs.getCell(21,i).getContents();//客户经理
String staffPhone = rs.getCell(22,i).getContents();//手机

map = new HashMap<String, Object>();
map.put("rowNo", i);
map.put("yearRate", yearRate);
map.put("staffName", staffName);
map.put("staffPhone", staffPhone);

    list.add(map);
  }
    } catch (Exception e) {
        msg="文件格式错误,导入失败!";
    e.printStackTrace();
    jObj.put("msg", msg);
    JSONUtils.printObject(jObj);
    return;
}
/**二、 导出数据*/
if (!list.isEmpty()){
    try {
        HSSFWorkbook wb = null;
        String sheetName = null;
        sheetName = "返息";
        String[] fieldName = new String[]{"序号","收益率","客户经理","手机"};
        String[] field = new String[]{"rowNo","yearRate","staffName","staffPhone"};
        String fileName = "月月丰返息表.xls";
        wb = ExcelUtils.exportExcel(sheetName, list, fieldName,field);
        try {
            this.export(wb, fileName);
        } catch (Exception e) {
            e.printStackTrace();
        } 
    } catch (Exception e) {
        e.printStackTrace();
    } 
}else{
    msg="数据为空,请检查文件!";
    jObj.put("msg", msg);
    JSONUtils.printObject(jObj);
    return;
}
log.info("transCateTypeOFYYF...结束转换...");
return;
}

/*工具类*/
    protected void export(HSSFWorkbook wb, String fileName) throws IOException {
        HttpServletResponse response = response();
        // 设置response的编码方式
        response.setContentType("application/x-msdownload");

        // 写明要下载的文件的大小
        // response.setContentLength((int)fileName.length());

        // 设置附加文件名
//      response.setHeader("Content-Disposition", "attachment;filename="
//              +URLEncoder.encode(fileName,"UTF-8"));

        // 解决中文乱码
        response.setHeader("Content-Disposition","attachment;filename="+new String (fileName.getBytes("gbk"),"iso-8859-1"));
        OutputStream output = response().getOutputStream();
        wb.write(output);

        output.flush();
        output.close();

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值