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

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

Introduction

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

Prototype

public byte[] getBytes() 

Source Link

Document

Method getBytes - get the bytes of just the HSSF portions of the XLS file.

Usage

From source file:br.com.hslife.orcamento.controller.LancamentoContaController.java

License:Open Source License

@SuppressWarnings("resource")
public void exportarLancamentos() {
    if (listEntity == null || listEntity.isEmpty()) {
        warnMessage("Listagem vazio. Nada a exportar.");
    }//from  ww w.  jav  a 2s  . c  o  m

    try {

        HSSFWorkbook excel = new HSSFWorkbook();
        HSSFSheet planilha = excel.createSheet("lancamentoConta");

        HSSFRow linha = planilha.createRow(0);

        HSSFCell celula = linha.createCell(0);
        celula.setCellValue("Data");
        celula = linha.createCell(1);
        celula.setCellValue("Histrico");
        celula = linha.createCell(2);
        celula.setCellValue("Valor");

        int linhaIndex = 1;
        for (LancamentoConta l : listEntity) {
            linha = planilha.createRow(linhaIndex);

            celula = linha.createCell(0);
            celula.setCellValue(Util.formataDataHora(l.getDataPagamento(), Util.DATA));

            celula = linha.createCell(1);
            celula.setCellValue(l.getDescricao());

            celula = linha.createCell(2);
            celula.setCellValue(l.getValorPago());

            linhaIndex++;
        }

        HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance()
                .getExternalContext().getResponse();
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment; filename=lancamentoConta.xls");
        response.setContentLength(excel.getBytes().length);
        ServletOutputStream output = response.getOutputStream();
        output.write(excel.getBytes(), 0, excel.getBytes().length);
        FacesContext.getCurrentInstance().responseComplete();

    } catch (IOException e) {
        errorMessage(e.getMessage());
    }
}

From source file:com.eryansky.modules.disk.utils.DiskUtils.java

License:Apache License

/**
 * ?/*from  w  w w  . j  a v a 2 s  .  c  om*/
 *
 * @param folderCode
 *            ?
 * @param sessionInfo
 *            session? ??null
 *
 * @param workbook
 * @param fileName
 *
 * @return
 * @throws InvalidExtensionException
 * @throws FileUploadBase.FileSizeLimitExceededException
 * @throws FileNameLengthLimitExceededException
 * @throws IOException
 */
public static File saveExcelFile(String folderCode, SessionInfo sessionInfo, HSSFWorkbook workbook,
        String fileName) throws InvalidExtensionException, FileUploadBase.FileSizeLimitExceededException,
        FileNameLengthLimitExceededException, IOException {
    String userId = null;
    if (sessionInfo != null && sessionInfo.getUserId() != null) {
        userId = sessionInfo.getUserId();
    }

    String code = FileUploadUtils.encodingFilenamePrefix(userId + "", fileName);
    Folder folder = getSystemFolderByCode(folderCode, userId);
    String storeFilePath = iFileManager.getStorePath(folder, userId, fileName);
    File file = new File();
    file.setFolder(folder);
    file.setCode(code);
    file.setUserId(userId);
    file.setName(fileName);
    file.setFilePath(storeFilePath);
    file.setFileSize(Long.valueOf(workbook.getBytes().length));
    file.setFileSuffix(FilenameUtils.getExtension(fileName));
    FileOutputStream fileOut = new FileOutputStream(storeFilePath);
    workbook.write(fileOut);
    fileOut.close();
    //        iFileManager.saveFile(file.getFilePath(),multipartFile.getInputStream(), true);
    diskManager.saveFile(file);
    return file;
}