2017-09-18 3 views
0

私はHSSFWorkbookためのセルスタイルを作成灰色です:HSSF Apacheのセルスタイルは、すべての細胞が

private static HSSFCellStyle createNewColorCellStyle(Map<Color, HSSFCellStyle> cellStylesMap, HSSFWorkbook workbook, Color color) { 

    if (cellStylesMap.get(color) == null) { 
     HSSFCellStyle cellStyle = workbook.createCellStyle(); 
     HSSFColor hssfColor = setColor(workbook, (byte) color.getRed(), (byte) color.getGreen(), (byte) color.getBlue()); 
     cellStyle.setFillForegroundColor(hssfColor.getIndex()); 
     cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); 
     cellStyle.setBorderTop(CellStyle.BORDER_THIN); 
     cellStyle.setBorderLeft(CellStyle.BORDER_THIN); 
     cellStyle.setBorderBottom(CellStyle.BORDER_THIN); 
     cellStyle.setBorderRight(CellStyle.BORDER_THIN); 

     cellStylesMap.put(color, cellStyle); 
    } 

    return cellStylesMap.get(color); 
} 

私はセルスタイル

private static HSSFColor setColor(HSSFWorkbook workbook, byte r, byte g, byte b) { 
    HSSFPalette palette = workbook.getCustomPalette(); 
    HSSFColor hssfColor = null; 
    try { 
     hssfColor = palette.findColor(r, g, b); 
     if (hssfColor == null) { 
      palette.setColorAtIndex(HSSFColor.BLUE_GREY.index, r, g, b); 
      HSSFColor hssfColor = palette.getColor(HSSFColor.BLUE_GREY.index); 
     } 
    } catch (Exception e) { 
     LOGGER.info(String.valueOf(e)); 
    } 

    return hssfColor; 
} 

に色を設定する機能があり、私はその中でそれを使用します方法、私は自分のスタイルをHSSFCellに設定しました。 マップスタイルを使用するための関数。ループ内に行とセルを作成します。

void excell(Path path, JTable table) { 
    try { 
     HSSFWorkbook fWorkbook = new HSSFWorkbook(); 
     HSSFSheet fSheet = fWorkbook.createSheet("the sheet"); 
     Map<Color, HSSFCellStyle> cellStylesMap = new HashMap<>(); 
     TableModel model = table.getModel(); 
      for (int i = 0; i < model.getRowCount(); i++) { 
       HSSFRow fRow = fSheet.createRow((short) i); 
       for (int j = 0; j < table.getColumnModel().getColumnCount(); j++) { 
         HSSFCell cell = fRow.createCell(j); 
         cell.setCellValue(model.getValueAt(i, j)); 
         Component c = table.getCellRenderer(i, j).getTableCellRendererComponent(table, cell, table.isCellSelected(i, j), table.hasFocus(), i, j); 
         Color color = c.getBackground() != null ? c.getBackground() : table.getBackground(); 
         cell.setCellStyle(createNewColorCellStyle(cellStylesMap, fWorkbook, color)); 
        } 
       } 
     } 

     FileOutputStream fileOutputStream; 
     fileOutputStream = new FileOutputStream(path.toString()); 
     BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream); 
     fWorkbook.write(bos); 
     bos.close(); 
     fileOutputStream.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

すべてのセルは1色です。異なる機種では色違いです。 どのようなエラーがありますか?なぜ私のすべての色が優れているのですか?私は何が間違っているのか分かりません。おかげさまで

+0

ワークブックとその中のセルの作成方法の簡潔で完全なバージョンを教えてください。 – JensS

+0

@JenS - 私の投稿を編集しました –

答えて

0

基本的には、テーブルのコンポーネントの背景色をExcelにコピーします。デバッグする必要があります/どの色があるかを見つける。

私はそれらがすべて同じ色であると想定しています。そのため、あなたはExcelで色を得るのです。

場合 - 例えば - あなたは

Color color = c.getBackground() != null ? c.getBackground() : table.getBackground(); 

Color color = (i % 2 == 0 ? Color.RED : Color.BLUE); 

に変更するには、赤と青の行のミックスを取得 - ので、あなたのコードは作業を行います。基本的には、どの色がcreateNewColorCellStyleに入っているかを確認してから、それを変更する必要があります。

また、setColorでhssfColorを2回定義します。行内

HSSFColor hssfColor = palette.getColor(HSSFColor.BLUE_GREY.index); 

宣言HSSFColorを削除する必要があります。

+0

前回の問題をありがとうございました。私はそれを修正しましたが、ここでコードを更新するのを忘れました。そして、createNewColorCellの私の色について - それらは異なっていて、私はマップに同じ数のスタイルを持っています。私のケースでは4. –

+0

しかし、私はまた、ブックの塗りつぶしのために、マップからの最後のスタイルを使用することがわかりました。 –

+0

c.getBackground()!= nullの後にどの値の色があるかチェックしましたか? c.getBackground():table.getBackground()? – JensS