• 纯java生成excel


    HSSFWorkbook wk = new HSSFWorkbook();
    HSSFSheet sheet = wk.createSheet("考生证书信息");
    this.setSheetColumnWidth(sheet);
    HSSFRow row = sheet.createRow((int) 0);

    HSSFCellStyle style = wk.createCellStyle();
    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

    HSSFCellStyle style2 = wk.createCellStyle();
    style2.setAlignment(HSSFCellStyle.ALIGN_LEFT);
    this.createCell(row, 0, style, HSSFCell.CELL_TYPE_STRING,"考生姓名");
    this.createCell(row, 1, style, HSSFCell.CELL_TYPE_STRING,"性别");
    this.createCell(row, 2, style, HSSFCell.CELL_TYPE_STRING,"出生日期");
    this.createCell(row, 3, style, HSSFCell.CELL_TYPE_STRING,"文化程度");
    this.createCell(row, 4, style, HSSFCell.CELL_TYPE_STRING,"颁证日期");
    this.createCell(row, 5, style, HSSFCell.CELL_TYPE_STRING,"证书号");
    this.createCell(row, 6, style, HSSFCell.CELL_TYPE_STRING,"身份证号");
    this.createCell(row, 7, style, HSSFCell.CELL_TYPE_STRING,"职业(工种)");
    this.createCell(row, 8, style, HSSFCell.CELL_TYPE_STRING,"鉴定级别");
    this.createCell(row, 9, style, HSSFCell.CELL_TYPE_STRING,"理论成绩");
    this.createCell(row, 10, style, HSSFCell.CELL_TYPE_STRING,"实操成绩");
    this.createCell(row, 11, style, HSSFCell.CELL_TYPE_STRING,"综合成绩");
    this.createCell(row, 12, style, HSSFCell.CELL_TYPE_STRING,"外语成绩");
    this.createCell(row, 13, style, HSSFCell.CELL_TYPE_STRING,"评定成绩");
    this.createCell(row, 14, style, HSSFCell.CELL_TYPE_STRING,"鉴定部门");
    this.createCell(row, 15, style, HSSFCell.CELL_TYPE_STRING,"鉴定计划编号");

    for (int i = 0; i < list.size(); i++) {
    KsScoreListVO model=list.get(i);
    HSSFRow row1 = sheet.createRow((short) (i + 1));// 建立新行

    if(StringUtils.isNotBlank(model.getStuname()))

    this.createCell(row1, 0, style,HSSFCell.CELL_TYPE_STRING, model.getStuname());

    if(StringUtils.isNotBlank(model.getSex())){
    if(model.getSex().equals("9")){
    this.createCell(row1, 1, style,HSSFCell.CELL_TYPE_STRING, "未知");
    }else{
    this.createCell(row1, 1, style,HSSFCell.CELL_TYPE_STRING, model.getSex());
    }
    }

    if(StringUtils.isNotBlank(model.getCredennum())){
    String date=model.getCredennum().substring(6, 10)+"-"+model.getCredennum().substring(10, 12)+"-"
    +model.getCredennum().substring(12, 14);
    this.createCell(row1, 2, style,HSSFCell.CELL_TYPE_STRING, date);
    }

    if(StringUtils.isNotBlank(model.getEducation()))

    this.createCell(row1, 3, style,HSSFCell.CELL_TYPE_STRING, model.getEducation());

    if(model.getZsbftime()!=null)

    this.createCell(row1, 4, style,HSSFCell.CELL_TYPE_STRING, DateUtils.format(model.getZsbftime(), "yyyy-MM-dd"));

    if(StringUtils.isNotBlank(model.getZsnum()))

    this.createCell(row1, 5, style,HSSFCell.CELL_TYPE_STRING, model.getZsnum());

    if(StringUtils.isNotBlank(model.getCredennum()))

    this.createCell(row1, 6, style,HSSFCell.CELL_TYPE_STRING, model.getCredennum());

    if(StringUtils.isNotBlank(model.getZygzname()))

    this.createCell(row1, 7, style,HSSFCell.CELL_TYPE_STRING, model.getZygzname());

    if(StringUtils.isNotBlank(model.getJdrank()))

    this.createCell(row1, 8, style,HSSFCell.CELL_TYPE_STRING, model.getJdrank());

    if(StringUtils.isNotBlank(model.getLl_score()))

    this.createCell(row1, 9, style,HSSFCell.CELL_TYPE_STRING, model.getLl_score());

    if(StringUtils.isNotBlank(model.getSc_score()))

    this.createCell(row1, 10, style,HSSFCell.CELL_TYPE_STRING, model.getSc_score());

    if(StringUtils.isNotBlank(model.getZh_score()))

    this.createCell(row1, 11, style,HSSFCell.CELL_TYPE_STRING, model.getZh_score());

    if(StringUtils.isNotBlank(model.getWy_score()))

    this.createCell(row1, 12, style,HSSFCell.CELL_TYPE_STRING, model.getWy_score());

    if(StringUtils.isNotBlank(model.getPd_score()))

    this.createCell(row1, 13, style,HSSFCell.CELL_TYPE_STRING, model.getPd_score());

    if(StringUtils.isNotBlank(model.getBmdw()))

    this.createCell(row1, 14, style2,HSSFCell.CELL_TYPE_STRING, model.getBmdw());

    if(StringUtils.isNotBlank(model.getJdjhnum()))

    this.createCell(row1, 15, style,HSSFCell.CELL_TYPE_STRING, model.getJdjhnum());

    }

    ByteArrayOutputStream out=new ByteArrayOutputStream();
    try {
    wk.write(out);
    servletRequest.setAttribute("excelStream",new ByteArrayInputStream(out.toByteArray()));
    servletRequest.setAttribute("filename", new String(vo.getJdjh().getBytes("GBK"),"ISO-8859-1")+".xls");//设置文件名
    return SUCCESS;
    } catch (IOException e) {
    e.printStackTrace();
    ExceptionLogHandler.saveExceptionLog(e,(String) getSession().get(SessionConstants.WSSIP_OPERATOR_ID));
    return ERROR;
    }finally{
    try {
    out.close();
    } catch (IOException e) {
    e.printStackTrace();
    ExceptionLogHandler.saveExceptionLog(e,(String) getSession().get(SessionConstants.WSSIP_OPERATOR_ID));
    }
    }


    private void setSheetColumnWidth1(HSSFSheet sheet) {

    // 根据你数据里面的记录有多少列,就设置多少列

    sheet.setColumnWidth((short)0, (short)6000);

    sheet.setColumnWidth((short)1, (short)6000);

    sheet.setColumnWidth((short)2, (short)3000);

    sheet.setColumnWidth((short)3, (short)2000);

    sheet.setColumnWidth((short)4, (short)4000);

    sheet.setColumnWidth((short)5, (short)4000);

    sheet.setColumnWidth((short)6, (short)4000);
    sheet.setColumnWidth((short)7,(short) 4000);
    sheet.setColumnWidth((short)8,(short) 4000);
    sheet.setColumnWidth((short)9,(short) 4000);
    sheet.setColumnWidth((short)10,(short) 6000);
    sheet.setColumnWidth((short)11,(short) 4000);
    sheet.setColumnWidth((short)12,(short) 4000);

    }

    private void createCell(HSSFRow row, int column, HSSFCellStyle style,

    int cellType, Object value) {

    HSSFCell cell = row.createCell((short)column);

    if (style != null) {

    cell.setCellStyle(style);
    }
    switch (cellType) {
    case HSSFCell.CELL_TYPE_BLANK: {
    }
    break;
    case HSSFCell.CELL_TYPE_STRING: {
    cell.setCellValue(value.toString());
    }
    break;
    case HSSFCell.CELL_TYPE_NUMERIC: {
    cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
    cell.setCellValue(Double.parseDouble(value.toString()));
    }
    break;
    default:
    break;
    }
    }


    private String fileName;


    public String getFileName() {
    return fileName;
    }

    public void setFileName(String fileName) throws UnsupportedEncodingException {
    this.fileName = new String(fileName.getBytes("iso8859-1"), "utf-8");
    }


    //获取输出流
    public InputStream getInputExcel()
    {
    return (InputStream)servletRequest.getAttribute("excelStream");
    }


    --------------------------------------------------------
    jsp页面

    function zs_export(type){
    var row =grid2.getSelected();
    if(!row){
    mini.alert("请选择一条记录!");
    return;
    }
    $("#id_xls").val(row.id);
    $("#types_xls").val(type);
    $("#jdjh_xls").val(row.jdjh);
    var excelForm = document.getElementById("excelForm");
    excelForm.action="${pageContext.request.contextPath}/osta/zsgl/exportexcel.action";
    excelForm.submit();
    }
    ----------------参数和from表单
    <iframe id="exportIFrame" style="display:none;"></iframe>
    <!--导出Excel相关HTML-->
    <form id="excelForm" method="post" target="excelIFrame">
    <input type="hidden" name="id" id="id_xls" />
    <input type="hidden" name="types" id="types_xls" />
    <input type="hidden" name="jdjh" id="jdjh_xls" />
    </form>
    <iframe id="excelIFrame" name="excelIFrame" style="display:none;"></iframe>

  • 相关阅读:
    让Sendmail和Dovecot使用AD进行用户认证
    在dhcpd.conf中配置静态路由
    IPhone4与Exchange 2010同步失败
    /*从文本中读取文件*/
    EM数据包按规则更新
    Crystal Report Show in Web With ParameterField
    /*读取xml数据*/
    上传文件
    新的网站
    【练习】哥德巴赫猜想验证程序
  • 原文地址:https://www.cnblogs.com/wanghongwei123/p/7168454.html
Copyright © 2020-2023  润新知