2017-09-13 14 views
1

41行目以降のタイトルからわかるように、デバッグを行ってもコードは正常に動作していますが、私のスタイルは適用されません。Apache POI Cellstyleは何らかの理由で41行目以降に適用されません

private void writeTable(Table table,Row row,Workbook wb){ 
    CellStyle cellStyle = wb.createCellStyle(); 
    if(row.getRowNum() % 2 == 0) { 

     cellStyle.setFillForegroundColor(IndexedColors.LIGHT_TURQUOISE.getIndex()); 
     cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); 
     cellStyle.setAlignment(CellStyle.ALIGN_CENTER); 
     cellStyle.setBorderTop(CellStyle.BORDER_THIN); 
     cellStyle.setWrapText(true); 
     cellStyle.setTopBorderColor(IndexedColors.GREY_80_PERCENT.getIndex()); 
     cellStyle.setBorderBottom(CellStyle.BORDER_THIN); 
     cellStyle.setBottomBorderColor(IndexedColors.GREY_80_PERCENT.getIndex()); 
     cellStyle.setBorderRight(CellStyle.BORDER_THIN); 
     cellStyle.setRightBorderColor(IndexedColors.GREY_80_PERCENT.getIndex()); 
     cellStyle.setBorderLeft(CellStyle.BORDER_THIN); 
     cellStyle.setLeftBorderColor(IndexedColors.GREY_80_PERCENT.getIndex()); 
    }else{ 

     cellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex()); 
     cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); 
     cellStyle.setAlignment(CellStyle.ALIGN_CENTER); 
     cellStyle.setBorderTop(CellStyle.BORDER_THIN); 
     cellStyle.setWrapText(true); 
     cellStyle.setTopBorderColor(IndexedColors.GREY_80_PERCENT.getIndex()); 
     cellStyle.setBorderBottom(CellStyle.BORDER_THIN); 
     cellStyle.setBottomBorderColor(IndexedColors.GREY_80_PERCENT.getIndex()); 
     cellStyle.setBorderRight(CellStyle.BORDER_THIN); 
     cellStyle.setRightBorderColor(IndexedColors.GREY_80_PERCENT.getIndex()); 
     cellStyle.setBorderLeft(CellStyle.BORDER_THIN); 
     cellStyle.setLeftBorderColor(IndexedColors.GREY_80_PERCENT.getIndex()); 
    } 

    Cell cell = row.createCell(0); 
    cell.setCellValue(Table.index); 
    cell.setCellStyle(cellStyle); 


    cell = row.createCell(1); 
    cell.setCellValue(strCorrecter(Table.Name).isEmpty() ? "-" : strCorrecter(Table.Name)); 
    cell.setCellStyle(cellStyle); 

    cell = row.createCell(2); 
    cell.setCellValue(strCorrecter(Table.Surname.toString()).isEmpty() ? "-" : strCorrecter(Table.Surname.toString())); 
    cell.setCellStyle(cellStyle); 

    cell = row.createCell(3); 
    cell.setCellValue("TODO"); 
    cell.setCellStyle(cellStyle); 

    cell = row.createCell(4); 
    cell.setCellValue(strCorrecter(Table.Age.toString()).isEmpty() ? "-" : strCorrecter(Table.Age.toString())); 
    cell.setCellStyle(cellStyle); 

    cell = row.createCell(5); 
    cell.setCellValue("TODO"); 
    cell.setCellStyle(cellStyle); 

    cell =row.createCell(6); 
    cell.setCellValue("TODO"); 
    cell.setCellStyle(cellStyle); 

    cell = row.createCell(7); 
    cell.setCellValue("TODO"); 
    cell.setCellStyle(cellStyle); 

    cell = row.createCell(8); 
    cell.setCellValue("TODO"); 
    cell.setCellStyle(cellStyle); 

    cell = row.createCell(9); 
    cell.setCellValue("TODO"); 
    cell.setCellStyle(cellStyle); 

    cell = row.createCell(10); 
    cell.setCellValue("TODO"); 
    cell.setCellStyle(cellStyle); 

    cell = row.createCell(11); 
    cell.setCellValue("TODO"); 
    cell.setCellStyle(cellStyle); 

    cell = row.createCell(12); 
    cell.setCellValue("TODO"); 
    cell.setCellStyle(cellStyle); 

    cell = row.createCell(13); 
    cell.setCellValue("TODO"); 
    cell.setCellStyle(cellStyle); 

    cell = row.createCell(14); 
    cell.setCellValue("TODO"); 
    cell.setCellStyle(cellStyle); 
} 

と私はすべての行が関数の先頭であればelse文を通過することを参照してください。

私の関数です。しかし、私はExcelファイルを見ても属性がないようです。 /セル大幅

+1

でファイルの拡張子を変更(HTTPS:/ /support.office.com/en-us/article/Excel-specifications-and-limits-1672b34d-7043-467e-8e27-269d656771c3)ブックのユニークなセルフォーマット/セルスタイルの最大数。だから**各単一の行にセルスタイルを作成しないでください**。私が見る限り、あなたは** 2つの異なるセルスタイルしか必要としません。したがって、これらの2つをメソッドの外に 'cellStyle1'と' cellStyle2'として作成し、そのメソッドの中でどちらか一方のみを使用します。 –

+0

どのようにそのエクセルの限界を超えて行くことができますか?そして何らかの理由で私は2つのスタイルを作成し、今度は16行目で終了します:D –

+1

あなたはその優れた限界を越えることはできません。また、提供したメソッドの外に** 2つのセルスタイルを作成する必要があります。しかし、あなたが[最小限の、完全で、かつ証明可能な例](https://stackoverflow.com/help/mcve)を提供していないので、本当に良い答えはできません。 –

答えて

1

が一意のセル書式の最大数のExcel limitありいただければ幸いですので、任意の助けが起こっていただきました!私はさっぱりだが

int rowCount = 3; 
for (Table table : tableList){ 
     Row row = sheet.createRow(++rowCount); 
     writeInterlock(table,row,workbook); 
    } 

:そして、これは私は、この関数を呼び出す部分でありますブック内のスタイル。

したがっては、1行ごとにセルスタイルを作成します。私が見る限り、あなたは2つの異なるセルスタイルしか必要としません。その後、

... 
CellStyle cellStyle1 = wb.createCellStyle(); 
//set all the needed settings 
CellStyle cellStyle2 = wb.createCellStyle(); 
//set all the needed settings 
... 

のみ使用メソッド内2::だから方法外のcellStyle1として2とcellStyle2を作成

private void writeTable(Table table,Row row,Workbook wb) { 
... 
if(row.getRowNum() % 2 == 0) { 
    //here use cellStyle1 
} else { 
    //here use cellStyle2 
} 
... 
} 
+0

とにかく静的な方法でこれをやっている – Frank

0

あなたは全く正しいイマイチaccpeted答えがあり、あなたはExcel 97( - 2007)の実装を使用しているので、ここではlinkがあります。

あなたは本当にあなたが受け入れ答えにそうであるように静的な方法で変数を管理する必要はありません(あなたに似た)作業例以下.XLS形式のファイルを、必要としない場合。あなたが見ることができるように

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 

import org.apache.poi.ss.usermodel.Cell; 
import org.apache.poi.ss.usermodel.CellStyle; 
import org.apache.poi.ss.usermodel.IndexedColors; 
import org.apache.poi.ss.usermodel.Row; 
import org.apache.poi.ss.usermodel.Workbook; 
import org.apache.poi.xssf.usermodel.XSSFSheet; 
import org.apache.poi.xssf.usermodel.XSSFWorkbook; 


public class MainWriteCells { 

    public MainWriteCells() { 
     // TODO Auto-generated constructor stub 
    } 

    public static void main(String[] args) throws IOException { 
     // TODO Auto-generated method stub 

     FileInputStream is = new FileInputStream(new File("C:\\ook.xlsx")); 
     XSSFWorkbook wb = new XSSFWorkbook(is); 
     XSSFSheet sheet = wb.getSheet("Sheet1"); 

     for(int i=0;i<1000;i++){ 
      writeTable(sheet.createRow(i),wb); 
     } 
     wb.write(new FileOutputStream(new File("C:\\ook.xlsx"))); 

    } 

    private static void writeTable(Row row,Workbook wb){ 
     CellStyle cellStyle = wb.createCellStyle(); 
     if(row.getRowNum() % 2 == 0) { 

      cellStyle.setFillForegroundColor(IndexedColors.LIGHT_TURQUOISE.getIndex()); 
      cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); 
      cellStyle.setAlignment(CellStyle.ALIGN_CENTER); 
      cellStyle.setBorderTop(CellStyle.BORDER_THIN); 
      cellStyle.setWrapText(true); 
      cellStyle.setTopBorderColor(IndexedColors.GREY_80_PERCENT.getIndex()); 
      cellStyle.setBorderBottom(CellStyle.BORDER_THIN); 
      cellStyle.setBottomBorderColor(IndexedColors.GREY_80_PERCENT.getIndex()); 
      cellStyle.setBorderRight(CellStyle.BORDER_THIN); 
      cellStyle.setRightBorderColor(IndexedColors.GREY_80_PERCENT.getIndex()); 
      cellStyle.setBorderLeft(CellStyle.BORDER_THIN); 
      cellStyle.setLeftBorderColor(IndexedColors.GREY_80_PERCENT.getIndex()); 
     }else{ 

      cellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex()); 
      cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); 
      cellStyle.setAlignment(CellStyle.ALIGN_CENTER); 
      cellStyle.setBorderTop(CellStyle.BORDER_THIN); 
      cellStyle.setWrapText(true); 
      cellStyle.setTopBorderColor(IndexedColors.GREY_80_PERCENT.getIndex()); 
      cellStyle.setBorderBottom(CellStyle.BORDER_THIN); 
      cellStyle.setBottomBorderColor(IndexedColors.GREY_80_PERCENT.getIndex()); 
      cellStyle.setBorderRight(CellStyle.BORDER_THIN); 
      cellStyle.setRightBorderColor(IndexedColors.GREY_80_PERCENT.getIndex()); 
      cellStyle.setBorderLeft(CellStyle.BORDER_THIN); 
      cellStyle.setLeftBorderColor(IndexedColors.GREY_80_PERCENT.getIndex()); 
     } 

     Cell cell = row.createCell(0); 
     cell.setCellValue("TODO"); 
     cell.setCellStyle(cellStyle); 


     cell = row.createCell(1); 
     cell.setCellValue("TODO"); 
     cell.setCellStyle(cellStyle); 

     cell = row.createCell(2); 
     cell.setCellValue("TODO"); 
     cell.setCellStyle(cellStyle); 

     cell = row.createCell(3); 
     cell.setCellValue("TODO"); 
     cell.setCellStyle(cellStyle); 

     cell = row.createCell(4); 
     cell.setCellValue("TODO"); 
     cell.setCellStyle(cellStyle); 

     cell = row.createCell(5); 
     cell.setCellValue("TODO"); 
     cell.setCellStyle(cellStyle); 

     cell =row.createCell(6); 
     cell.setCellValue("TODO"); 
     cell.setCellStyle(cellStyle); 

     cell = row.createCell(7); 
     cell.setCellValue("TODO"); 
     cell.setCellStyle(cellStyle); 

     cell = row.createCell(8); 
     cell.setCellValue("TODO"); 
     cell.setCellStyle(cellStyle); 

     cell = row.createCell(9); 
     cell.setCellValue("TODO"); 
     cell.setCellStyle(cellStyle); 

     cell = row.createCell(10); 
     cell.setCellValue("TODO"); 
     cell.setCellStyle(cellStyle); 

     cell = row.createCell(11); 
     cell.setCellValue("TODO"); 
     cell.setCellStyle(cellStyle); 

     cell = row.createCell(12); 
     cell.setCellValue("TODO"); 
     cell.setCellStyle(cellStyle); 

     cell = row.createCell(13); 
     cell.setCellValue("TODO"); 
     cell.setCellStyle(cellStyle); 

     cell = row.createCell(14); 
     cell.setCellValue("TODO"); 
     cell.setCellStyle(cellStyle); 
    } 

} 

iは、[Excelの制限]があり二つのオブジェクトと.xslx

XSSFWorkbook wb = new XSSFWorkbook(is); 
    XSSFSheet sheet = wb.getSheet("Sheet1"); 

代わりの

HSSFWorkbook wb = new HSSFWorkbook(is); 
     HSSFSheet sheet = wb.getSheet("Sheet1"); 
関連する問題