Package jxl.write

Examples of jxl.write.WritableWorkbook



   private boolean writeXLS(File file, boolean includeHeaders, int nbrSelCols, int[] selCols, int nbrSelRows, TableExportCsvController ctrl, int[] selRows)
      throws IOException, WriteException
   {
      WritableWorkbook workbook = Workbook.createWorkbook(file);
      WritableSheet sheet = workbook.createSheet("Squirrel SQL Export", 0);


      int curRow= 0;
      if (includeHeaders)
      {
         for (int colIdx = 0; colIdx < nbrSelCols; ++colIdx)
         {
            String columnName = _table.getColumnName(selCols[colIdx]);
            jxl.write.Label label = new jxl.write.Label(colIdx, curRow, columnName);
            sheet.addCell(label);
         }
         curRow++;
      }


      for (int rowIdx = 0; rowIdx < nbrSelRows; ++rowIdx)
      {
         for (int colIdx = 0; colIdx < nbrSelCols; ++colIdx)
         {
            WritableCell xlsCell;
            if(ctrl.useGloablPrefsFormatting() && _table.getColumnModel().getColumn(colIdx) instanceof ExtTableColumn)
            {
               ExtTableColumn col = (ExtTableColumn) _table.getColumnModel().getColumn(colIdx);
               xlsCell = getXlsCell(col, colIdx, curRow, _table.getValueAt(selRows[rowIdx], selCols[colIdx]));
            }
            else
            {
               xlsCell = getXlsCell(null, colIdx, curRow, _table.getValueAt(selRows[rowIdx], selCols[colIdx]));
            }
            sheet.addCell(xlsCell);

         }
         curRow++;
      }

      // All sheets and cells added. Now write out the workbook
      workbook.write();
      workbook.close();

      return true;
   }
View Full Code Here


        OutputStream os = null;
        try
        {
            os = new FileOutputStream(tmpfile);

            WritableWorkbook workbook = Workbook.createWorkbook(os);

            int r = 0;
            WritableSheet sheet = null;
            int sheetNumber = 0;

            int oldType = -1;
            for (StatSubscription statSub : statSubscriptions)
            {
                boolean changedType = false;
                if (oldType != statSub.getTypeDef())
                {
                    changedType = true;
                    r = 0;
                }
                oldType = statSub.getTypeDef();
                SummaryStatBean statdetails = getStatBean(context, service,
                        statSub);

                r++;
                if (statdetails == null)
                    continue;

                SummaryStatBean.StatDataBean statDataBean = statdetails
                        .getData().get(0);

                if (changedType)
                {
                    sheet = workbook.createSheet(I18nUtil.getMessage("it.cilea.hku.statistics.Subscribe.sheet." + statdetails.getType()),
                            sheetNumber);
                    WritableFont labelFont = new WritableFont(
                            WritableFont.ARIAL, 10, WritableFont.BOLD);
                    WritableCellFormat cfobj = new WritableCellFormat(labelFont);
                    sheet.addCell(new Label(
                            0,
                            0,
                            I18nUtil.getMessage("it.cilea.hku.statistics.Subscribe.type"),
                            cfobj));
                    sheet.addCell(new Label(
                            1,
                            0,
                            I18nUtil.getMessage("it.cilea.hku.statistics.Subscribe.name"),
                            cfobj));
                    sheet.addCell(new Label(
                            2,
                            0,
                            I18nUtil.getMessage("it.cilea.hku.statistics.Subscribe.url"),
                            cfobj));
                    sheet.addCell(new Label(
                            3,
                            0,
                            I18nUtil.getMessage("it.cilea.hku.statistics.Subscribe.totalView"),
                            cfobj));
                    sheet.addCell(new Label(4, 0, I18nUtil
                            .getMessage("it.cilea.hku.statistics.Subscribe."
                                    + sfreq + "View"), cfobj));

                    int headerCell = 4;
                    if (statDataBean.isShowSelectedObjectDownload())
                    {
                        sheet.addCell(new Label(
                                ++headerCell,
                                0,
                                I18nUtil.getMessage("it.cilea.hku.statistics.Subscribe.totalDownload"),
                                cfobj));
                        sheet.addCell(new Label(++headerCell, 0, I18nUtil
                                .getMessage("it.cilea.hku.statistics.Subscribe."
                                        + sfreq + "Download"), cfobj));
                    }
                    for (String topKey : statDataBean
                            .getPeriodAndTotalTopView().keySet())
                    {
                        if (!statDataBean.getPeriodAndTotalTopView()
                                .get(topKey).isEmpty())
                        {
                            sheet.addCell(new Label(
                                    ++headerCell,
                                    0,
                                    I18nUtil.getMessage("it.cilea.hku.statistics.Subscribe.total"
                                            + topKey + "View"), cfobj));
                            sheet.addCell(new Label(
                                    ++headerCell,
                                    0,
                                    I18nUtil.getMessage("it.cilea.hku.statistics.Subscribe."
                                            + sfreq + topKey + "View"), cfobj));
                        }
                    }
                    for (String topKey : statDataBean
                            .getPeriodAndTotalTopDownload().keySet())
                    {
                        if (!statDataBean.getPeriodAndTotalTopDownload()
                                .get(topKey).isEmpty())
                        {
                            sheet.addCell(new Label(
                                    ++headerCell,
                                    0,
                                    I18nUtil.getMessage("it.cilea.hku.statistics.Subscribe.total"
                                            + topKey + "Download"), cfobj));
                            sheet.addCell(new Label(
                                    ++headerCell,
                                    0,
                                    I18nUtil.getMessage("it.cilea.hku.statistics.Subscribe."
                                            + sfreq + topKey + "Download"), cfobj));
                        }
                    }
                    for (int i = 0; i < headerCell; i++)
                    {
                        final CellView view = sheet.getColumnView(i);
                        view.setAutosize(true);
                        sheet.setColumnView(i, view);
                    }
                    sheetNumber++;
                }
                sheet.addCell(new Label(0, r, I18nUtil
                        .getMessage("it.cilea.hku.statistics.Subscribe.type."
                                + statdetails.getType())));
                sheet.addCell(new Label(1, r, statdetails.getObjectName()));
                sheet.addCell(new Label(2, r, statdetails.getObjectURL()));
                if (statDataBean.getTotalSelectedView() == -1)
                {
                    sheet.addCell(new Label(3, r, I18nUtil
                            .getMessage("it.cilea.hku.statistics.Subscribe.na")));
                }
                else
                {
                    sheet.addCell(new Label(3, r, new Long(statDataBean
                            .getTotalSelectedView()).toString()));
                }

                if (statDataBean.getPeriodSelectedView() == -1)
                {
                    sheet.addCell(new Label(4, r, I18nUtil
                            .getMessage("it.cilea.hku.statistics.Subscribe.na")));
                }
                else
                {
                    sheet.addCell(new Label(4, r, new Long(statDataBean
                            .getPeriodSelectedView()).toString()));
                }

                int countTopCell = 4;
                if (statDataBean.isShowSelectedObjectDownload())
                {
                    if (statDataBean.getTotalSelectedDownload() == -1)
                    {
                        sheet.addCell(new Label(
                                ++countTopCell,
                                r,
                                I18nUtil.getMessage("it.cilea.hku.statistics.Subscribe.na")));
                    }
                    else
                    {
                        sheet.addCell(new Label(++countTopCell, r, new Long(
                                statDataBean.getTotalSelectedDownload())
                                .toString()));
                    }
                    if (statDataBean.getPeriodSelectedDownload() == -1)
                    {
                        sheet.addCell(new Label(
                                ++countTopCell,
                                r,
                                I18nUtil.getMessage("it.cilea.hku.statistics.Subscribe.na")));
                    }
                    else
                    {
                        sheet.addCell(new Label(++countTopCell, r, new Long(
                                statDataBean.getPeriodSelectedDownload())
                                .toString()));
                    }

                }
                for (String topKey : statDataBean.getPeriodAndTotalTopView()
                        .keySet())
                {
                    List<Long> tmpList = statDataBean
                            .getPeriodAndTotalTopView().get(topKey);
                    if (!tmpList.isEmpty())
                    {
                        if (tmpList.get(1) == null)
                        {
                            sheet.addCell(new Label(
                                    ++countTopCell,
                                    r,
                                    I18nUtil.getMessage("it.cilea.hku.statistics.Subscribe.na")));
                        }
                        else
                        {
                            sheet.addCell(new Label(++countTopCell, r,
                                    new Long(tmpList.get(1)).toString()));
                        }
                    }
                    if (!tmpList.isEmpty())
                    {
                        if (tmpList.get(0) == null)
                        {
                            sheet.addCell(new Label(
                                    ++countTopCell,
                                    r,
                                    I18nUtil.getMessage("it.cilea.hku.statistics.Subscribe.na")));
                        }
                        else
                        {
                            sheet.addCell(new Label(++countTopCell, r,
                                    new Long(tmpList.get(0)).toString()));
                        }
                    }
                }
                for (String topKey : statDataBean
                        .getPeriodAndTotalTopDownload().keySet())
                {
                    List<Long> tmpList = statDataBean
                            .getPeriodAndTotalTopDownload().get(topKey);
                    if (!tmpList.isEmpty())
                    {
                        if (tmpList.get(1) == null)
                        {
                            sheet.addCell(new Label(
                                    ++countTopCell,
                                    r,
                                    I18nUtil.getMessage("it.cilea.hku.statistics.Subscribe.na")));
                        }
                        else
                        {
                            sheet.addCell(new Label(++countTopCell, r,
                                    new Long(tmpList.get(1)).toString()));
                        }
                    }
                    if (!tmpList.isEmpty())
                    {
                        if (tmpList.get(0) == null)
                        {
                            sheet.addCell(new Label(
                                    ++countTopCell,
                                    r,
                                    I18nUtil.getMessage("it.cilea.hku.statistics.Subscribe.na")));
                        }
                        else
                        {
                            sheet.addCell(new Label(++countTopCell, r,
                                    new Long(tmpList.get(0)).toString()));
                        }
                    }
                }

            }
            workbook.write();
            workbook.close();
        }
        catch (Exception e)
        {
            log.error(e.getMessage(), e);
            throw new MessagingException(
View Full Code Here

            List<IContainable> metadata) throws IOException, WriteException,
            IllegalArgumentException, IllegalAccessException,
            InvocationTargetException
    {

        WritableWorkbook workbook = Workbook.createWorkbook(os);
        WritableSheet sheet = workbook.createSheet("Sheet", 0);

        // create initial caption (other caption could be write field together)
        int x = 0;
        sheet.addCell(new Label(x++, 0, "staffNo"));
        sheet.addCell(new Label(x++, 0, "rp"));
        sheet.addCell(new Label(x++, 0, "rp url"));

        // row index
        int i = 1;
        for (ResearcherPage rp : rps)
        {
            int y = 0;
            sheet.addCell(new Label(0, i, ""));
            Label label = (Label) sheet.getCell(0, i);
            label.setString(rp.getSourceID());
            y++;
            sheet.addCell(new Label(1, i, ""));
            label = (Label) sheet.getCell(1, i);
            label.setString(ResearcherPageUtils.getPersistentIdentifier(rp));
            y++;
            sheet.addCell(new Label(2, i, ""));
            label = (Label) sheet.getCell(2, i);
            label.setString(ConfigurationManager.getProperty("dspace.url")
                    + "/cris/" + rp.getPublicPath() + "/"
                    + ResearcherPageUtils.getPersistentIdentifier(rp));

            for (IContainable containable : metadata)
            {
                if (containable instanceof DecoratorRPPropertiesDefinition)
                {
                    y = UtilsXLS.createCell(applicationService, y, i,
                            (DecoratorRPPropertiesDefinition) containable, rp,
                            sheet);
                }
                if (containable instanceof DecoratorRestrictedField)
                {
                    y = UtilsXLS.createCell(applicationService, y, i,
                            (DecoratorRestrictedField) containable, rp, sheet);
                }
            }

            i++;
        }
        // All sheets and cells added. Now write out the workbook
        workbook.write();
        workbook.close();
    }
View Full Code Here

  }

  public void writeExcel(Map<Point, String> toWrite, String filename, int rowBegin, int rowEnd, int colBegin,
      int colEnd) throws IOException, RowsExceededException, WriteException {
    OutputStream os = new FileOutputStream(filename);
    WritableWorkbook wwb = Workbook.createWorkbook(os);
    WritableSheet ws = wwb.createSheet("fuck sf", 0);
    int x, y;// д����У�������
    int i, j;
    for (i = rowBegin, x = 0; i < rowEnd; i++, x++) {
      for (j = colBegin, y = 0; j < colEnd; j++, y++) {
        String content = toWrite.get(new Point(j, i));
        if (!isInteger(content) && j == 0) {
          x--;
          break;
        }
        jxl.write.Label labelC = new jxl.write.Label(y, x, content);
        ws.addCell(labelC);
      }
    }
    wwb.write();
    wwb.close();
    os.close();
  }
View Full Code Here

   
    // Stream containing excel data
      ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
 
      // Create Excel WorkBook and Sheet
      WritableWorkbook workBook;
      try {
        workBook = Workbook.createWorkbook(outputStream);
      } catch (IOException e) {
        throw new RuntimeException(e);
      }
      WritableSheet sheet = workBook.createSheet("Results", 0);
 
      // Generates Headers Cells
      WritableFont headerFont = new WritableFont(WritableFont.TAHOMA, 12, WritableFont.BOLD);
      WritableCellFormat headerCellFormat = new WritableCellFormat(headerFont);
     // headerCellFormat.setBackground(Colour.PALE_BLUE);
     
      int currentRow = 0;
      int currentColumn = 0;
     
      List<String> headers = getHeaders(selectedReport, reportedMethods);
     
      for(String header : headers)
      try {
        sheet.addCell(new Label(currentColumn++, currentRow, header, headerCellFormat));
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
   
 
      // Generates Data Cells
      WritableFont dataFont = new WritableFont(WritableFont.TAHOMA, 12);
      WritableCellFormat dataCellFormat = new WritableCellFormat(dataFont);
      WritableCellFormat dateCellFormat = new WritableCellFormat(dataFont, DateFormats.FORMAT2);
     
     
      for(Object row : getRows(selectedReport))
      {
        List<Object> rowData = renderRow(reportedMethods, row);
        if(rowData.size() != headers.size())
          throw new RuntimeException( String.format(
                      "Wrong number of columns returned when rendering row %s, against headers %s",
                      row.toString(), headers.toString()));
        currentRow++;
        currentColumn = 0;
        for(Object cellData : rowData)
        {
         if (cellData != null )
         {
           WritableCell cell = null;
         
           if (cellData instanceof NamedEntity)
             cellData = ((NamedEntity)cellData).getName();
           else if (cellData instanceof Text)
             cellData = ((Text)cellData).getValue();
          
           else if (cellData instanceof Email)
             cellData = ((Email)cellData).getEmail();
          
           if (cellData instanceof Number)
           {
             cell = new jxl.write.Number(currentColumn, currentRow, ((Number)cellData).doubleValue(), dataCellFormat);
           } else if (cellData instanceof Date)
           {
             cell = new DateTime(currentColumn, currentRow, (Date)cellData, dateCellFormat);
           } else
           {
             if (!(cellData instanceof String) )
               cellData = cellData.toString();
           
             cell = new Label(currentColumn, currentRow, (String)cellData, dataCellFormat);
            
           }      
          
          
           try {
            sheet.addCell(cell);
          } catch (Exception e) {
            throw new RuntimeException(e);
          }
         }
         currentColumn++;
        }
       
      }
    
      // Write & Close Excel WorkBook
      try {
        workBook.write();
        workBook.close();
      } catch (Exception e)
      {
        throw new RuntimeException(e);
      }
 
View Full Code Here

   * @param index    保存工作表的位置(工作表
   * @param rows    工作表的行数
   * @param contents  要保存的内容数组
   */
  public void writeXLS(String savaPath, String sheetName, int index,int rows, String[] contents) {
    WritableWorkbook workbook = null;
      try {
        workbook = Workbook.createWorkbook(new File(savaPath));    // 创建可写入的Excel工作薄
        WritableSheet ws = workbook.createSheet(sheetName, index)// 创建可写入的Excel工作表
        int cols = contents.length / rows;              // 根据行数得出列数,在数据库里比较容易得到行数
        for (int row = 0; row < rows; row++) {
          for (int col = 0; col < cols; col++) {
            Label label = new Label(col, row,contents[col + row * cols]);
            ws.addCell(label); // 添加Label对象,单元格内容
          }
        }
        workbook.write(); // 写入excel
        workbook.close(); // 关闭对象,释放资源
      } catch (FileNotFoundException e) {
        System.out.println("ExcelHandle.writeXLS()" + e);
      } catch (IOException e) {
        System.out.println("ExcelHandle.writeXLS()" + e);
      } catch (RowsExceededException e) {
View Full Code Here

   * @param os  指向客户端的输出流
   * @param rows  excel的总行数(就是从数据库读出来记录的总条数)
   * @param datas 所有数据(不要存对象,是对象中的具体信息值)
   */
  public static void writeXLS(OutputStream os,int rows, List datas) {
    WritableWorkbook workbook = null;
    try {
      workbook = Workbook.createWorkbook(os);          // 创建可写入的Excel工作薄
      WritableSheet ws = workbook.createSheet("Sheet1", 0)// 创建可写入的Excel工作表
      int cols = datas.size() / rows;              // 根据行数得出列数,在数据库里比较容易得到行数
      for (int row = 0; row < rows; row++) {
        for (int col = 0; col < cols; col++) {
          Label label = new Label(col, row,(String) datas.get(col + row * cols));
          ws.addCell(label); // 添加Label对象,单元格内容
        }
      }
      workbook.write(); // 写入excel
      workbook.close(); // 关闭对象,释放资源
    } catch (IOException e) {
      System.out.println("ExcelHandle.writeXLS() - IOException" + e);
    } catch (RowsExceededException e) {
      System.out.println("ExcelHandle.writeXLS() - RowsExceededException" + e);
    } catch (WriteException e) {
View Full Code Here

  public static WritableWorkbook createWorkbook(java.io.File file,
                                                WorkbookSettings ws)
    throws IOException
  {
    FileOutputStream fos = new FileOutputStream(file);
    WritableWorkbook w = new WritableWorkbookImpl(fos, true, ws);
    return w;
  }
View Full Code Here

                                                Workbook in,
                                                WorkbookSettings ws)
    throws IOException
  {
    FileOutputStream fos = new FileOutputStream(file);
    WritableWorkbook w = new WritableWorkbookImpl(fos, in, true, ws);
    return w;
  }
View Full Code Here

  public static WritableWorkbook createWorkbook(OutputStream os,
                                                Workbook in,
                                                WorkbookSettings ws)
    throws IOException
  {
    WritableWorkbook w = new WritableWorkbookImpl(os, in, false, ws);
    return w;
  }
View Full Code Here

TOP

Related Classes of jxl.write.WritableWorkbook

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.