2017-09-13 3 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); 
} 

그리고 난 다른 문 경우 모든 행 함수의 시작 부분에 통과 것을 알 수있다. 그러나 그들은 엑셀 파일을 볼 때 어떤 속성도없는 것 같습니다./셀 크게

+1

에서 파일 확장자를 변경 (HTTPS ://support.office.com/en-us/article/Excel-specifications-and-limits-1672b34d-7043-467e-8e27-269d656771c3) 통합 문서의 고유 한 셀 서식/셀 스타일의 최대 수를 나타냅니다. 그래서 ** 각 단일 행에 대해 셀 스타일을 만들지 마십시오 **. 내가보기에는 ** 두 개의 다른 셀 스타일 만 필요합니다. 따라서이 두 메서드를 메서드 외부에서 'cellStyle1'과 'cellStyle2'로 만들고 메서드 내에서 두 메서드 중 하나만 사용하십시오. –

+0

어떻게 그 한계를 뛰어 넘을 수 있습니까? 그리고 어떤 이유로 2 가지 스타일을 만들었고 이제는 16 번째 줄에서 끝납니다 : D –

+1

그 한계를 뛰어 넘을 수는 없습니다. 그리고 당신이 제공 한 방법 밖에 ** 두 개의 셀 스타일 **을 만들어야합니다. 하지만 [최소, 완전하고 검증 가능한 예제] (https://stackoverflow.com/help/mcve)를 제공하지 않아서 정말 좋은 대답은 불가능합니다. –

답변

1

고유 셀 서식의 최대 수에 대한 Excel limit이 감상 할 수 있도록 도움이 일어나고있는 무슨 나는 아무 생각도 없어

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

: 그리고 이것은 내가이 함수를 호출하는 부분입니다 통합 문서의 스타일.

그래서 은 각 단일 행에 셀 스타일을 만듭니다. 내가보기에는 두 개의 다른 셀 스타일 만 있으면됩니다. 다음

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

사용 방법 내 두 : 그래서 방법 외부 그 cellStyle1 등 두와 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 대답,가 한계, 좋아,하지만 당신이 엑셀 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); 
    } 

} 

내가이 [엑셀 제한]이 두 개체 및 .xslx

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

대신

HSSFWorkbook wb = new HSSFWorkbook(is); 
     HSSFSheet sheet = wb.getSheet("Sheet1");