2017-04-15 8 views
0

sCurrentLineという文字列をExcelの行に変換します。行がX、Y、またはZで始まっているかどうかによって、行に色付けされます。HSSFCell setCellStyleは、選択したセルの代わりにすべてのシートのセルにスタイルを適用します。

 row = sheet.createRow(lines); 
     String[] parts = sCurrentLine.split("\\|"); 

     if (sCurrentLine.contains("X")) { 
      bgColorIndex = HSSFColor.RED.index; 
     } else if (sCurrentLine.contains("Y")){ 
      bgColorIndex = HSSFColor.LIGHT_BLUE.index; 
     } else if (sCurrentLine.contains("Z")) { 
      bgColorIndex = HSSFColor.YELLOW.index; 
     } else { 
      bgColorIndex = HSSFColor.BROWN.index; 
     } 

だから私はbgColorIndexと呼ばれるこの変数を持つと私が午前問題はsetCellStyleは、すべてのスタイルを適用しているある行のすべてのセル

 for (short i = 0; i < parts.length; i++) { 
      row.createCell(i).setCellValue(parts[i]); 

      HSSFCell curCell = row.getCell(i); 
      HSSFCellStyle curStyle = curCell.getCellStyle(); 
      curStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); 
      curStyle.setFillForegroundColor(bgColorIndex); 
      curStyle.setFillBackgroundColor(bgColorIndex); 
      System.out.println("Color is: " + bgColorIndex); 

      curCell.setCellStyle(curStyle); 
     } 
     lines++; 
    } 

の色を設定するためにそれを使用しています行とシートがあるので、最後に検出した色はドキュメント全体に適用されます。

セルごとに個別に色付けするにはどうすればよいですか?

EDITED: 全体コード:

private void generateCSVFile() { 
    String filename = "excel.xls" ; 
    HSSFWorkbook workbook = new HSSFWorkbook(); 
    HSSFSheet sheet = workbook.createSheet("FirstSheet"); 

    HSSFRow rowhead = sheet.createRow((short)0); 

    // header 
    rowhead.createCell((short)0).setCellValue("AXIS"); 
    rowhead.createCell((short)1).setCellValue("INIT"); 
    rowhead.createCell((short)2).setCellValue("MID"); 
    rowhead.createCell((short)3).setCellValue("END");  

    // Set columns width 
    for (short i = 0; i < 3; i++) { 
     sheet.setColumnWidth(i, (short)(20*300)); 
    } 

    BufferedReader br = null; 
    FileReader fr = null; 

    try { 

     fr = new FileReader("data.txt"); 
     br = new BufferedReader(fr); 

     String sCurrentLine; 

     br = new BufferedReader(new FileReader("data.txt")); 
     short lines = 1; 
     HSSFRow row = null; 

     while ((sCurrentLine = br.readLine()) != null) { 
      if (sCurrentLine.contains("AXIS")) { 
       lines = 0; 
       sheet = workbook.createSheet("SecondSheet"); 
       // Set columns width 
       for (short i = 0; i < 7; i++) { 
        sheet.setColumnWidth(i, (short)(20*256)); 
       } 

       row = sheet.createRow(lines); 
      } 

      row = sheet.createRow(lines); 
      String[] parts = sCurrentLine.split("\\|"); 

      short bgColorIndex = 0; 
      // Check the first cell to set color for X, Y or Z 
      if (sCurrentLine.contains("X")) { 
       bgColorIndex = HSSFColor.RED.index; 
      } else if (sCurrentLine.contains("Y")){ 
       bgColorIndex = HSSFColor.LIGHT_BLUE.index; 
      } else if (sCurrentLine.contains("Z")) { 
       bgColorIndex = HSSFColor.YELLOW.index; 
      } else { 
       bgColorIndex = HSSFColor.BROWN.index; 
      } 

      for (short i = 0; i < parts.length; i++) { 

       row.createCell(i).setCellValue(parts[i]); 

       HSSFCell curCell = row.getCell(i); 
       HSSFCellStyle curStyle = curCell.getCellStyle(); 
       curStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); 
       curStyle.setFillForegroundColor(bgColorIndex); 
       curStyle.setFillBackgroundColor(bgColorIndex); 
       System.out.println("Color is: " + bgColorIndex); 

       curCell.setCellStyle(curStyle); 
       curStyle = null; 
      } 
      lines++; 
     } 

    } catch (IOException e) { 

     e.printStackTrace(); 

    } finally { 

     try { 

      if (br != null) 
       br.close(); 

      if (fr != null) 
       fr.close(); 

     } catch (IOException ex) { 

      ex.printStackTrace(); 

     } 

    } 
} 
+0

あなたが提供することができますコード全体のブロック?あなたは最初に色を見つけているので、最後の色を覚えていて、それを細胞に設定しているようです。 – NiVeR

答えて

2

あなたは、ブック内の4つの異なるCellStyleを作成する必要があります。

CellStyle styleX = wb.createCellStyle(); 
styleX.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); 
styleX.setFillForegroundColor(HSSFColor.RED.index); 
styleX.setFillBackgroundColor(HSSFColor.RED.index); 
CellStyle styleY = wb.createCellStyle(); 
styleY.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); 
styleY.setFillForegroundColor(HSSFColor.LIGHT_BLUE.index); 
styleY.setFillBackgroundColor(HSSFColor.LIGHT_BLUE.index); 
CellStyle styleZ = wb.createCellStyle(); 
styleZ.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); 
styleZ.setFillForegroundColor(HSSFColor.YELLOW.index); 
styleZ.setFillBackgroundColor(HSSFColor.YELLOW.index); 
CellStyle styleOther = wb.createCellStyle(); 
styleOther .setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); 
styleOther .setFillForegroundColor(HSSFColor.BROWN.index); 
styleOther .setFillBackgroundColor(HSSFColor.BROWN.index); 

そして、各行について:

CellStyle style = null; 
row = sheet.createRow(lines); 
String[] parts = sCurrentLine.split("\\|"); 

if (sCurrentLine.contains("X")) { 
    style = styleX; 
} else if (sCurrentLine.contains("Y")){ 
    style = styleY; 
} else if (sCurrentLine.contains("Z")) { 
    style = styleZ; 
} else { 
    style = styleOther; 
} 
for (short i = 0; i < parts.length; i++) { 
    row.createCell(i).setCellValue(parts[i]); 
    HSSFCell curCell = row.getCell(i); 
    currCell.setCellStyle(style); 
} 
+0

ああ、感謝する、ありがとう – lapinkoira

関連する問題