Example usage for org.apache.poi.hssf.usermodel HSSFWorkbook createSheet

List of usage examples for org.apache.poi.hssf.usermodel HSSFWorkbook createSheet

Introduction

In this page you can find the example usage for org.apache.poi.hssf.usermodel HSSFWorkbook createSheet.

Prototype

@Override
public HSSFSheet createSheet(String sheetname) 

Source Link

Document

Create a new sheet for this Workbook and return the high level representation.

Usage

From source file:br.ufpa.psi.comportamente.labgame.mbeans.RelatoriosMB.java

License:Open Source License

public StreamedContent geraRelatorioJogadasExperimento()
        throws ParsePropertyException, IOException, InvalidFormatException {
    HSSFWorkbook workbook = new HSSFWorkbook();
    HSSFSheet sheet = workbook.createSheet("Relatrio das Jogadas");

    sheet.setColumnWidth(sheet.getFirstRowNum(), (14 * 256) + 200);
    sheet.setColumnWidth(1, (14 * 256) + 200);
    sheet.setColumnWidth(4, (17 * 256) + 200);
    sheet.setColumnWidth(5, (16 * 256) + 200);

    HSSFCellStyle cs1 = workbook.createCellStyle();
    cs1.setDataFormat(HSSFDataFormat.getBuiltinFormat("d-mmm-yy"));

    HSSFCellStyle cs2 = workbook.createCellStyle();
    cs2.setDataFormat(HSSFDataFormat.getBuiltinFormat("h:mm:ss"));

    JogadaDAO jogadaDAO = new JogadaDAO();
    jogadaDAO.beginTransaction();//from w  w w  .j  a v a 2 s. c om
    List<Jogada> jogadas = jogadaDAO.encontrarPorExperimento(experimentoSelecionado.getId());

    int countRow = 0;
    Row row1 = sheet.createRow(countRow++);
    Cell cell = row1.createCell(0);
    cell.setCellValue("Experimento: " + experimentoSelecionado.getNome());

    Row row = sheet.createRow(countRow++);

    row.createCell(0).setCellValue("Data da Jogada");
    row.createCell(1).setCellValue("Hora da Jogada");
    row.createCell(2).setCellValue("Coluna");
    row.createCell(3).setCellValue("Linha");
    row.createCell(4).setCellValue("Pontuacao Individual");
    row.createCell(5).setCellValue("Pontuacao Coletiva");
    row.createCell(6).setCellValue("Participante");
    row.createCell(7).setCellValue("Condio");

    for (Jogada jogada : jogadas) {

        Row nrow = sheet.createRow(countRow++);

        //Data
        Cell ncell0 = nrow.createCell(0);
        ncell0.setCellValue(jogada.getMomento());
        ncell0.setCellStyle(cs1);

        //Hora
        Cell ncell1 = nrow.createCell(1);
        ncell1.setCellValue(jogada.getMomento());
        ncell1.setCellStyle(cs2);

        //Coluna
        Cell ncell2 = nrow.createCell(2);
        ncell2.setCellValue(jogada.getColunaSelecionada());

        //Linha
        Cell ncell3 = nrow.createCell(3);
        ncell3.setCellValue(jogada.getLinhaSelecionada());

        //Pontuao Individual
        Cell ncell4 = nrow.createCell(4);
        ncell4.setCellValue(jogada.getPontuacaoIndividual());

        //Pontuao Coletiva
        Cell ncell5 = nrow.createCell(5);
        ncell5.setCellValue(jogada.getPontuacaoCultural());

        //Jogador
        Cell ncell6 = nrow.createCell(6);
        ncell6.setCellValue(jogada.getJogador().getNome());

        //Id da Condio
        Cell ncell7 = nrow.createCell(7);
        ncell7.setCellValue(jogada.getIdCondicao());

    }

    jogadaDAO.stopOperation(false);

    byte[] bytes;
    try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        workbook.write(out);
        bytes = out.toByteArray();
    }

    InputStream ioStream = new ByteArrayInputStream(bytes);
    file = new DefaultStreamedContent(ioStream, "application/vnd.ms-excel", "Relatrio_Jogadas.xls");
    return file;
}

From source file:chocanproject.ServiceDetailsGUI.java

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    // TODO add your handling code here:       

    try {//from   w  w  w.j  a  v  a2s  .  c  o  m
        Connection con = DriverManager.getConnection(connectionUrl);
        Statement st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        String sql = "ChocAn.dbo.GetProviderDirectory_Select";
        ResultSet rs = st.executeQuery(sql);

        //Create a blank workbook and sheet
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("NewSheet");
        HSSFRow rowhead = sheet.createRow((short) 0);
        rowhead.createCell((short) 0).setCellValue("CellHeadName1");
        rowhead.createCell((short) 1).setCellValue("CellHeadName2");
        rowhead.createCell((short) 2).setCellValue("CellHeadName3");
        int i = 1;
        while (rs.next()) {
            HSSFRow row = sheet.createRow((short) i);
            row.createCell((short) 0).setCellValue(rs.getString("ServiceDesc"));
            row.createCell((short) 1).setCellValue(Integer.toString(rs.getInt("ServiceID")));
            row.createCell((short) 2).setCellValue(rs.getString("Cost"));
            i++;
        }
        String ProviderDirectory = "D:/Excel/ProviderDirectory.xls";
        FileOutputStream fileOut = new FileOutputStream(ProviderDirectory);
        workbook.write(fileOut);
        JOptionPane.showMessageDialog(null,
                "Directory file has been sent to your email address successfully and saved a .xlsx file at local path D:/Excel/");
        fileOut.close();
    } catch (SQLException e1) {
        e1.printStackTrace();
    } catch (FileNotFoundException e1) {
        e1.printStackTrace();
    } catch (IOException e1) {
        e1.printStackTrace();
    }
}

From source file:Clavis.Windows.WShedule.java

public void createXLSDocument(String nome, String[][] valores) {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet(nome);
    HSSFRow row;//from  w  w w . j  a  va  2s . c o  m
    HSSFCell cell;
    for (int i = 0; i < valores.length; i++) {
        row = sheet.createRow(i);
        for (int j = 0; j < valores[i].length; j++) {
            cell = row.createCell(j);
            cell.setCellValue(valores[i][j]);
            if (valores[i][j] != null) {
                sheet.setColumnWidth(j, 255 * valores[i][j].length() + 4);
                CellUtil.setAlignment(cell, wb, CellStyle.ALIGN_CENTER);
            }
        }
    }
    wb.setPrintArea(0, 0, valores[0].length, 0, valores.length);
    sheet.getPrintSetup().setPaperSize(XSSFPrintSetup.A4_PAPERSIZE);
    FileOutputStream out;
    String sfile = new File("").getAbsolutePath() + System.getProperty("file.separator") + nome;
    File file = new File(sfile);
    if (!file.exists()) {
        try {
            file.createNewFile();
        } catch (IOException ex) {
            Logger.getLogger(WShedule.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    if (file.canWrite()) {
        try {
            out = new FileOutputStream(file);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(WShedule.class.getName()).log(Level.SEVERE, null, ex);
            out = null;
        }
        if (out != null) {
            try {
                wb.write(out);
                out.close();
                if (Desktop.isDesktopSupported()) {
                    Desktop.getDesktop().open(file);
                } else {
                    Components.MessagePane mensagem = new Components.MessagePane(this,
                            Components.MessagePane.INFORMACAO, painelcor, lingua.translate("Nota"), 400, 200,
                            lingua.translate("O documento \"doc.xls\" foi criado na pasta raiz do programa")
                                    + ".",
                            new String[] { lingua.translate("Voltar") });
                    mensagem.showMessage();
                }
            } catch (IOException ex) {
                Logger.getLogger(WShedule.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}

From source file:cn.edu.henu.rjxy.lms.controller.Acdemic.java

@RequestMapping("daochuxuesheng")
public void daochuxuesheng(HttpServletRequest request, HttpServletResponse response) throws IOException {
    HttpSession session = request.getSession();
    session.setAttribute("state", null);
    // ????  // w  w  w.  ja  v  a 2s  . co m
    response.setContentType("application/vnd.ms-excel");
    String codedFileName;
    codedFileName = "??";
    OutputStream fOut = null;
    try {
        // ????  
        codedFileName = java.net.URLEncoder.encode("", "UTF-8");
        response.setHeader("content-disposition", "attachment;filename=" + "student_messsage" + ".xls");
        HSSFWorkbook wb = new HSSFWorkbook();
        // webbooksheet,Excelsheet  
        HSSFSheet sheet = wb.createSheet("");
        // sheet0,??poiExcel?short  
        HSSFRow row = sheet.createRow((int) 0);
        // ?   
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // ? 
        HSSFCell cell = row.createCell((short) 0);
        cell.setCellValue("?");
        cell.setCellStyle(style);
        cell = row.createCell((short) 1);
        cell.setCellValue("??");
        cell.setCellStyle(style);
        cell = row.createCell((short) 2);
        cell.setCellValue("??");
        cell.setCellStyle(style);
        cell = row.createCell((short) 3);
        cell.setCellValue("");
        cell.setCellStyle(style);
        cell = row.createCell((short) 4);
        cell.setCellValue("");
        cell.setCellStyle(style);
        cell = row.createCell((short) 5);
        cell.setCellValue("");
        cell.setCellStyle(style);
        cell = row.createCell((short) 6);
        cell.setCellValue("?");
        cell.setCellStyle(style);
        cell = row.createCell((short) 7);
        cell.setCellValue("qq");
        cell.setCellStyle(style);

        List allstudent = StudentDao.getAllStudent();

        for (int i = 0; i < allstudent.size(); i++) {
            StudentWithoutPwd tmp = (StudentWithoutPwd) allstudent.get(i);
            row = sheet.createRow((int) i + 1);
            row.createCell((short) 0).setCellValue(tmp.getStudentSn());
            row.createCell((short) 1).setCellValue(tmp.getStudentName());
            row.createCell((short) 2).setCellValue(tmp.getStudentIdcard());
            row.createCell((short) 3).setCellValue(tmp.getStudentGrade());
            row.createCell((short) 4).setCellValue((tmp.isStudentSex()) ? ("") : (""));
            row.createCell((short) 5).setCellValue(tmp.getStudentCollege());
            row.createCell((short) 6).setCellValue(tmp.getStudentTel());
            row.createCell((short) 7).setCellValue(tmp.getStudentQq());

        }

        fOut = response.getOutputStream();
        wb.write(fOut);
    } catch (UnsupportedEncodingException e1) {
    } catch (Exception e) {
    } finally {
        fOut.flush();
        fOut.close();
        session.setAttribute("state", "open");
    }
    System.out.println("?...");

}

From source file:cn.edu.henu.rjxy.lms.controller.Acdemic.java

@RequestMapping("daochualltoxls")
public void exportExcel(HttpServletRequest request, HttpServletResponse response) throws IOException {
    HttpSession session = request.getSession();
    System.out.println("1...");
    session.setAttribute("state", null);
    // ????  // w w  w  .  java 2s.  c om
    response.setContentType("application/vnd.ms-excel");
    String codedFileName;
    codedFileName = "??";
    OutputStream fOut = null;
    try {
        // ????  
        codedFileName = java.net.URLEncoder.encode("", "UTF-8");
        response.setHeader("content-disposition", "attachment;filename=" + "teacher_messsage" + ".xls");
        HSSFWorkbook wb = new HSSFWorkbook();
        // webbooksheet,Excelsheet  
        HSSFSheet sheet = wb.createSheet("");
        // sheet0,??poiExcel?short  
        HSSFRow row = sheet.createRow((int) 0);
        // ?   
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // ? 
        HSSFCell cell = row.createCell((short) 0);
        cell.setCellValue("?");
        cell.setCellStyle(style);
        cell = row.createCell((short) 1);
        cell.setCellValue("??");
        cell.setCellStyle(style);
        cell = row.createCell((short) 2);
        cell.setCellValue("??");
        cell.setCellStyle(style);
        cell = row.createCell((short) 3);
        cell.setCellValue("?");
        cell.setCellStyle(style);
        cell = row.createCell((short) 4);
        cell.setCellValue("");
        cell.setCellStyle(style);
        cell = row.createCell((short) 5);
        cell.setCellValue("qq");
        cell.setCellStyle(style);
        cell = row.createCell((short) 6);
        cell.setCellValue("?");
        cell.setCellStyle(style);
        cell = row.createCell((short) 7);
        cell.setCellValue("");
        cell.setCellStyle(style);

        List allteacher = TeacherDao.getAllTeacher();

        for (int i = 0; i < allteacher.size(); i++) {
            TempTeacherWithoutPwd tmp = (TempTeacherWithoutPwd) allteacher.get(i);
            row = sheet.createRow((int) i + 1);
            row.createCell((short) 0).setCellValue(tmp.getTeacherSn());
            row.createCell((short) 1).setCellValue(tmp.getTeacherName());
            row.createCell((short) 2).setCellValue(tmp.getTeacherIdcard());
            row.createCell((short) 3).setCellValue(tmp.getTeacherPosition());
            row.createCell((short) 4).setCellValue(tmp.getTeacherCollege());
            row.createCell((short) 5).setCellValue(tmp.getTeacherQq());
            row.createCell((short) 6).setCellValue(tmp.getTeacherTel());
            row.createCell((short) 7).setCellValue((tmp.getTeacherSex()) ? ("") : (""));

        }

        fOut = response.getOutputStream();
        wb.write(fOut);
    } catch (UnsupportedEncodingException e1) {
    } catch (Exception e) {
    } finally {
        fOut.flush();
        fOut.close();
        session.setAttribute("state", "open");
    }
    System.out.println("?...");
}

From source file:cn.edu.henu.rjxy.lms.controller.DeanController.java

@RequestMapping("dean/daochuxuesheng")
public void daochuxuesheng(HttpServletRequest request, HttpServletResponse response) throws IOException {
    HttpSession session = request.getSession();
    session.setAttribute("state", null);
    // ????  //from  ww w. j  a  va 2  s .c  om
    response.setContentType("application/vnd.ms-excel");
    String codedFileName;
    codedFileName = "??";
    OutputStream fOut = null;
    try {
        // ????  
        codedFileName = java.net.URLEncoder.encode("", "UTF-8");
        response.setHeader("content-disposition", "attachment;filename=" + "student_messsage" + ".xls");
        HSSFWorkbook wb = new HSSFWorkbook();
        // webbooksheet,Excelsheet  
        HSSFSheet sheet = wb.createSheet("");
        // sheet0,??poiExcel?short  
        HSSFRow row = sheet.createRow((int) 0);
        // ?   
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // ? 
        HSSFCell cell = row.createCell((short) 0);
        cell.setCellValue("?");
        cell.setCellStyle(style);
        cell = row.createCell((short) 1);
        cell.setCellValue("??");
        cell.setCellStyle(style);
        cell = row.createCell((short) 2);
        cell.setCellValue("??");
        cell.setCellStyle(style);
        cell = row.createCell((short) 3);
        cell.setCellValue("");
        cell.setCellStyle(style);
        cell = row.createCell((short) 4);
        cell.setCellValue("");
        cell.setCellStyle(style);
        cell = row.createCell((short) 5);
        cell.setCellValue("");
        cell.setCellStyle(style);
        cell = row.createCell((short) 6);
        cell.setCellValue("?");
        cell.setCellStyle(style);
        cell = row.createCell((short) 7);
        cell.setCellValue("qq");
        cell.setCellStyle(style);

        List allstudent = StudentDao.getAllStudent();

        for (int i = 0; i < allstudent.size(); i++) {
            StudentWithoutPwd tmp = (StudentWithoutPwd) allstudent.get(i);
            row = sheet.createRow((int) i + 1);
            row.createCell((short) 0).setCellValue(tmp.getStudentSn());
            row.createCell((short) 1).setCellValue(tmp.getStudentName());
            row.createCell((short) 2).setCellValue(tmp.getStudentIdcard());
            row.createCell((short) 3).setCellValue(tmp.getStudentGrade());
            row.createCell((short) 4).setCellValue((tmp.isStudentSex()) ? ("") : (""));
            row.createCell((short) 5).setCellValue(tmp.getStudentCollege());
            row.createCell((short) 6).setCellValue(tmp.getStudentTel());
            row.createCell((short) 7).setCellValue(tmp.getStudentQq());

        }

        fOut = response.getOutputStream();
        wb.write(fOut);
    } catch (UnsupportedEncodingException e1) {
    } catch (Exception e) {
    } finally {
        fOut.flush();
        fOut.close();
        session.setAttribute("state", "open");
    }
    System.out.println("?...");

}

From source file:cn.edu.henu.rjxy.lms.controller.DeanController.java

@RequestMapping("dean/daochualltoxls")
public void exportExcel(HttpServletRequest request, HttpServletResponse response) throws IOException {
    HttpSession session = request.getSession();
    System.out.println("1...");
    session.setAttribute("state", null);
    // ????  /*from  w  w w. j a va  2  s.  co m*/
    response.setContentType("application/vnd.ms-excel");
    String codedFileName;
    codedFileName = "??";
    OutputStream fOut = null;
    try {
        // ????  
        codedFileName = java.net.URLEncoder.encode("", "UTF-8");
        response.setHeader("content-disposition", "attachment;filename=" + "teacher_messsage" + ".xls");
        HSSFWorkbook wb = new HSSFWorkbook();
        // webbooksheet,Excelsheet  
        HSSFSheet sheet = wb.createSheet("");
        // sheet0,??poiExcel?short  
        HSSFRow row = sheet.createRow((int) 0);
        // ?   
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // ? 
        HSSFCell cell = row.createCell((short) 0);
        cell.setCellValue("?");
        cell.setCellStyle(style);
        cell = row.createCell((short) 1);
        cell.setCellValue("??");
        cell.setCellStyle(style);
        cell = row.createCell((short) 2);
        cell.setCellValue("??");
        cell.setCellStyle(style);
        cell = row.createCell((short) 3);
        cell.setCellValue("?");
        cell.setCellStyle(style);
        cell = row.createCell((short) 4);
        cell.setCellValue("");
        cell.setCellStyle(style);
        cell = row.createCell((short) 5);
        cell.setCellValue("qq");
        cell.setCellStyle(style);
        cell = row.createCell((short) 6);
        cell.setCellValue("?");
        cell.setCellStyle(style);
        cell = row.createCell((short) 7);
        cell.setCellValue("");
        cell.setCellStyle(style);

        List allteacher = TeacherDao.getAllTeacher();

        for (int i = 0; i < allteacher.size(); i++) {
            TempTeacherWithoutPwd tmp = (TempTeacherWithoutPwd) allteacher.get(i);
            row = sheet.createRow((int) i + 1);
            row.createCell((short) 0).setCellValue(tmp.getTeacherSn());
            row.createCell((short) 1).setCellValue(tmp.getTeacherName());
            row.createCell((short) 2).setCellValue(tmp.getTeacherIdcard());
            row.createCell((short) 3).setCellValue(tmp.getTeacherPosition());
            row.createCell((short) 4).setCellValue(tmp.getTeacherCollege());
            row.createCell((short) 5).setCellValue(tmp.getTeacherQq());
            row.createCell((short) 6).setCellValue(tmp.getTeacherTel());
            row.createCell((short) 7).setCellValue((tmp.getTeacherSex()) ? ("") : (""));

        }

        fOut = response.getOutputStream();
        wb.write(fOut);
    } catch (UnsupportedEncodingException e1) {
    } catch (Exception e) {
    } finally {
        fOut.flush();
        fOut.close();
        session.setAttribute("state", "open");
    }
    System.out.println("?...");
}

From source file:cn.edu.henu.rjxy.lms.controller.TeaController.java

@RequestMapping("teacher/xz_xs_xx")
public @ResponseBody String daochuxuesheng(HttpServletRequest request, HttpServletResponse response)
        throws IOException {
    int classid = Integer.parseInt(request.getParameter("zjd_id"));
    int course_id = Integer.parseInt(request.getParameter("fjd_id"));
    int term = Integer.parseInt(request.getParameter("term"));
    String sn = getCurrentUsername();
    Teacher tec = TeacherDao.getTeacherBySn(sn);
    int tec_id = tec.getTeacherId();
    int trem_courseid = TermCourseDao.getTermCourseId(term, course_id, classid, tec_id);
    System.out.println(trem_courseid + "trem_courseid");
    PageBean<StuSelectResult> mystudent = TeacherDao.getStuSelectByTermCourseId(trem_courseid, 1, 300);
    HttpSession session = request.getSession();
    session.setAttribute("state", null);
    // ????  /*from w  ww.jav  a2s. c o  m*/
    response.setContentType("application/vnd.ms-excel");
    String codedFileName;
    codedFileName = "??";
    OutputStream fOut = null;
    try {
        // ????  
        codedFileName = java.net.URLEncoder.encode("", "UTF-8");
        response.setHeader("content-disposition", "attachment;filename=" + "student_messsage" + ".xls");
        HSSFWorkbook wb = new HSSFWorkbook();
        // webbooksheet,Excelsheet  
        HSSFSheet sheet = wb.createSheet("");
        // sheet0,??poiExcel?short  
        HSSFRow row = sheet.createRow((int) 0);
        // ?   
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // ? 
        HSSFCell cell = row.createCell((short) 0);
        cell.setCellValue("?");
        cell.setCellStyle(style);
        cell = row.createCell((short) 1);
        cell.setCellValue("??");
        cell.setCellStyle(style);
        cell = row.createCell((short) 2);
        cell.setCellValue("??");
        cell.setCellStyle(style);
        cell = row.createCell((short) 3);
        cell.setCellValue("");
        cell.setCellStyle(style);
        cell = row.createCell((short) 4);
        cell.setCellValue("");
        cell.setCellStyle(style);
        cell = row.createCell((short) 5);
        cell.setCellValue("");
        cell.setCellStyle(style);
        cell = row.createCell((short) 6);
        cell.setCellValue("?");
        cell.setCellStyle(style);
        cell = row.createCell((short) 7);
        cell.setCellValue("qq");
        cell.setCellStyle(style);
        for (int i = 0; i < mystudent.getBeanList().size(); i++) {
            StuSelectResult a = (StuSelectResult) mystudent.getBeanList().get(i);
            System.out.println(getStudentById(a.getStuid()).getStudentSn());
            row = sheet.createRow((int) i + 1);
            row.createCell((short) 0).setCellValue(getStudentById(a.getStuid()).getStudentSn());
            row.createCell((short) 1).setCellValue(getStudentById(a.getStuid()).getStudentName());
            row.createCell((short) 2).setCellValue(getStudentById(a.getStuid()).getStudentIdcard());
            row.createCell((short) 3).setCellValue(getStudentById(a.getStuid()).getStudentGrade());
            row.createCell((short) 4)
                    .setCellValue((getStudentById(a.getStuid()).getStudentSex()) ? ("") : (""));
            row.createCell((short) 5).setCellValue(getStudentById(a.getStuid()).getStudentCollege());
            row.createCell((short) 6).setCellValue(getStudentById(a.getStuid()).getStudentTel());
            row.createCell((short) 7).setCellValue(getStudentById(a.getStuid()).getStudentQq());

        }

        fOut = response.getOutputStream();
        wb.write(fOut);
    } catch (UnsupportedEncodingException e1) {
    } catch (Exception e) {
    } finally {
        fOut.flush();
        fOut.close();
        session.setAttribute("state", "open");
    }
    System.out.println("?...");
    return "1";

}

From source file:cn.edu.henu.rjxy.lms.controller.Tea_Controller.java

@RequestMapping("xz_xs_xx")
public @ResponseBody String daochuxuesheng(HttpServletRequest request, HttpServletResponse response)
        throws IOException {
    int classid = Integer.parseInt(request.getParameter("zjd_id"));
    int course_id = Integer.parseInt(request.getParameter("fjd_id"));
    int term = Integer.parseInt(request.getParameter("term"));
    String sn = getCurrentUsername();
    Teacher tec = TeacherDao.getTeacherBySn(sn);
    int tec_id = tec.getTeacherId();
    int trem_courseid = TermCourseDao.getTermCourseId(term, course_id, classid, tec_id);
    System.out.println(trem_courseid + "trem_courseid");
    PageBean<StuSelectResult> mystudent = TeacherDao.getStuSelectByTermCourseId(trem_courseid, 1, 300);
    HttpSession session = request.getSession();
    session.setAttribute("state", null);
    // ????  /* ww w . j  a v  a2s . c om*/
    response.setContentType("application/vnd.ms-excel");
    String codedFileName;
    codedFileName = "??";
    OutputStream fOut = null;
    try {
        // ????  
        codedFileName = java.net.URLEncoder.encode("", "UTF-8");
        response.setHeader("content-disposition", "attachment;filename=" + "student_messsage" + ".xls");
        HSSFWorkbook wb = new HSSFWorkbook();
        // webbooksheet,Excelsheet  
        HSSFSheet sheet = wb.createSheet("");
        // sheet0,??poiExcel?short  
        HSSFRow row = sheet.createRow((int) 0);
        // ?   
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // ? 
        HSSFCell cell = row.createCell((short) 0);
        cell.setCellValue("?");
        cell.setCellStyle(style);
        cell = row.createCell((short) 1);
        cell.setCellValue("??");
        cell.setCellStyle(style);
        cell = row.createCell((short) 2);
        cell.setCellValue("??");
        cell.setCellStyle(style);
        cell = row.createCell((short) 3);
        cell.setCellValue("");
        cell.setCellStyle(style);
        cell = row.createCell((short) 4);
        cell.setCellValue("");
        cell.setCellStyle(style);
        cell = row.createCell((short) 5);
        cell.setCellValue("");
        cell.setCellStyle(style);
        cell = row.createCell((short) 6);
        cell.setCellValue("?");
        cell.setCellStyle(style);
        cell = row.createCell((short) 7);
        cell.setCellValue("qq");
        cell.setCellStyle(style);
        for (int i = 0; i < mystudent.getBeanList().size(); i++) {
            StuSelectResult a = (StuSelectResult) mystudent.getBeanList().get(i);
            System.out.println(getStudentById(a.getStuid()).getStudentSn());
            row = sheet.createRow((int) i + 1);
            row.createCell((short) 0).setCellValue(getStudentById(a.getStuid()).getStudentSn());
            row.createCell((short) 1).setCellValue(getStudentById(a.getStuid()).getStudentName());
            row.createCell((short) 2).setCellValue(getStudentById(a.getStuid()).getStudentIdcard());
            row.createCell((short) 3).setCellValue(getStudentById(a.getStuid()).getStudentGrade());
            row.createCell((short) 4)
                    .setCellValue((getStudentById(a.getStuid()).getStudentSex()) ? ("") : (""));
            row.createCell((short) 5).setCellValue(getStudentById(a.getStuid()).getStudentCollege());
            row.createCell((short) 6).setCellValue(getStudentById(a.getStuid()).getStudentTel());
            row.createCell((short) 7).setCellValue(getStudentById(a.getStuid()).getStudentQq());

        }

        fOut = response.getOutputStream();
        wb.write(fOut);
    } catch (UnsupportedEncodingException e1) {
    } catch (Exception e) {
    } finally {
        fOut.flush();
        fOut.close();
        session.setAttribute("state", "open");
    }
    System.out.println("?...");
    return "1";

}

From source file:cn.mario256.blog.ExcelView.java

License:Open Source License

/**
 * ?Excel//from w w w.j a  v  a  2  s .  c om
 * 
 * @param model
 *            ?
 * @param workbook
 *            HSSFWorkbook
 * @param request
 *            HttpServletRequest
 * @param response
 *            HttpServletResponse
 */
public void buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    Assert.notEmpty(properties);

    HSSFSheet sheet;
    if (StringUtils.isNotEmpty(sheetName)) {
        sheet = workbook.createSheet(sheetName);
    } else {
        sheet = workbook.createSheet();
    }
    int rowNumber = 0;
    if (titles != null && titles.length > 0) {
        HSSFRow header = sheet.createRow(rowNumber);
        header.setHeight((short) 400);
        for (int i = 0; i < properties.length; i++) {
            HSSFCell cell = header.createCell(i);
            HSSFCellStyle cellStyle = workbook.createCellStyle();
            cellStyle.setFillForegroundColor(HSSFColor.LIGHT_CORNFLOWER_BLUE.index);
            cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
            cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
            cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
            HSSFFont font = workbook.createFont();
            font.setFontHeightInPoints((short) 11);
            font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
            cellStyle.setFont(font);
            cell.setCellStyle(cellStyle);
            if (i == 0) {
                HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
                HSSFComment comment = patriarch
                        .createComment(new HSSFClientAnchor(0, 0, 0, 0, (short) 1, 1, (short) 4, 4));
                comment.setString(new HSSFRichTextString("P" + "o" + "w" + "e" + "r" + "e" + "d" + " " + "B"
                        + "y" + " " + "S" + "H" + "O" + "P" + "+" + "+"));
                cell.setCellComment(comment);
            }
            if (titles.length > i && titles[i] != null) {
                cell.setCellValue(titles[i]);
            } else {
                cell.setCellValue(properties[i]);
            }
            if (widths != null && widths.length > i && widths[i] != null) {
                sheet.setColumnWidth(i, widths[i]);
            } else {
                sheet.autoSizeColumn(i);
            }
        }
        rowNumber++;
    }
    if (data != null) {
        for (Object item : data) {
            HSSFRow row = sheet.createRow(rowNumber);
            for (int i = 0; i < properties.length; i++) {
                HSSFCell cell = row.createCell(i);
                if (converters != null && converters.length > i && converters[i] != null) {
                    Class<?> clazz = PropertyUtils.getPropertyType(item, properties[i]);
                    ConvertUtils.register(converters[i], clazz);
                    cell.setCellValue(BeanUtils.getProperty(item, properties[i]));
                    ConvertUtils.deregister(clazz);
                    if (clazz.equals(Date.class)) {
                        DateConverter dateConverter = new DateConverter();
                        dateConverter.setPattern(DEFAULT_DATE_PATTERN);
                        ConvertUtils.register(dateConverter, Date.class);
                    }
                } else {
                    cell.setCellValue(BeanUtils.getProperty(item, properties[i]));
                }
                if (rowNumber == 0 || rowNumber == 1) {
                    if (widths != null && widths.length > i && widths[i] != null) {
                        sheet.setColumnWidth(i, widths[i]);
                    } else {
                        sheet.autoSizeColumn(i);
                    }
                }
            }
            rowNumber++;
        }
    }
    if (contents != null && contents.length > 0) {
        rowNumber++;
        for (String content : contents) {
            HSSFRow row = sheet.createRow(rowNumber);
            HSSFCell cell = row.createCell(0);
            HSSFCellStyle cellStyle = workbook.createCellStyle();
            HSSFFont font = workbook.createFont();
            font.setColor(HSSFColor.GREY_50_PERCENT.index);
            cellStyle.setFont(font);
            cell.setCellStyle(cellStyle);
            cell.setCellValue(content);
            rowNumber++;
        }
    }
    response.setContentType("application/force-download");
    if (StringUtils.isNotEmpty(filename)) {
        response.setHeader("Content-disposition",
                "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
    } else {
        response.setHeader("Content-disposition", "attachment");
    }
}