2015-04-20 10 views
7

Używam Apache POI do generowania pliku Excel. Muszę usunąć wszystkie krawędzie z mojego arkusza roboczego. Jak mogę to zrobić za pomocą Apache PIO 3.11 i Microsoft Excel 2007?Usuń wszystkie granice określonego arkusza programu Excel za pomocą Apache POI

Oto kod mam tak daleko:

package models; 

import org.apache.poi.hssf.usermodel.HSSFWorkbook; 
import org.apache.poi.ss.usermodel.*; 
import org.apache.poi.ss.util.CellRangeAddress; 

import java.io.FileOutputStream; 
import java.util.List; 
public class Excel { 

    public static void writeDocument() { 
     Workbook workbook = new HSSFWorkbook(); 
     Sheet sheet = workbook.createSheet("sheet"); 
     //first font 
     Font font1 = workbook.createFont(); 
     font1.setBoldweight(Font.BOLDWEIGHT_BOLD); 
     //first style 
     CellStyle style1 = workbook.createCellStyle(); 
     style1.setBorderLeft(CellStyle.BORDER_NONE); 
     style1.setBorderRight(CellStyle.BORDER_NONE); 
     style1.setBorderBottom(CellStyle.BORDER_NONE); 
     style1.setBorderTop(CellStyle.BORDER_NONE); 

     //second style 
     CellStyle style2 = workbook.createCellStyle(); 
     style2.setFont(font1); 
     style2.setVerticalAlignment(CellStyle.VERTICAL_CENTER); 
     style2.setAlignment(CellStyle.ALIGN_CENTER); 
     style2.setFillForegroundColor(IndexedColors.ORANGE.getIndex()); 
     style2.setFillPattern(CellStyle.SOLID_FOREGROUND); 
     style2.setBorderBottom(CellStyle.BORDER_THIN); 
     style2.setBottomBorderColor(IndexedColors.GREY_25_PERCENT.getIndex()); 
     style2.setBorderLeft(CellStyle.BORDER_THIN); 
     style2.setLeftBorderColor(IndexedColors.GREY_25_PERCENT.getIndex()); 
     style2.setBorderRight(CellStyle.BORDER_THIN); 
     style2.setRightBorderColor(IndexedColors.GREY_25_PERCENT.getIndex()); 
     style2.setBorderTop(IndexedColors.GREY_25_PERCENT.getIndex()); 

     for(int i=0; i< 100 ; i++){ 
      for(int j=0; j< 100; j++){ 
       Cell cell = sheet.createRow(i).createCell(j); 
       cell.setCellStyle(style1); 
      } 
     } 


     CellRangeAddress region = new CellRangeAddress(0, 10, 0, 10); 
     cleanBeforeMergeOnValidCells(sheet, region, style2); 
     sheet.addMergedRegion(region); 

     try { 
      FileOutputStream output = new FileOutputStream("tmp/rapport.xls"); 
      workbook.write(output); 
      output.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    private static void cleanBeforeMergeOnValidCells(Sheet sheet, CellRangeAddress region, CellStyle cellStyle) { 
     for (int rowNum = region.getFirstRow(); rowNum <= region.getLastRow(); rowNum++) { 
      Row row = sheet.getRow(rowNum); 
      if (row == null) { 
       sheet.createRow(rowNum); 
      } 
      for (int colNum = region.getFirstColumn(); colNum <= region.getLastColumn(); colNum++) { 
       Cell currentCell = row.getCell(colNum); 
       if (currentCell == null) { 
        currentCell = row.createCell(colNum); 
       } 

       currentCell.setCellStyle(cellStyle); 

      } 
     } 

    } 
} 
+0

Co robi nie działa? – Mirco

+0

Po pierwsze, nadal widzę granice w moim arkuszu. Po drugie, gdy próbowałem wstawić 2 lub więcej wartości w tym samym wierszu, tylko pierwsza wartość została poprawnie wstawiona. –

+0

sposób, w jaki dodawałem komórki w tym samym wierszu: sheet.createRow (5) .createCell (6) .setCellValue ("a"); sheet.createRow (5) .createCell (7) .setCellValue ("b"); sheet.createRow (5) .createCell (8) .setCellValue ("c"); –

Odpowiedz

18

myślę, że oznacza to, że nie trzeba linie siatki, w tym przypadku należy użyć metody setDisplayGridlines jak:

sheet.setDisplayGridlines(false); 
+0

Bardzo ładne ...... –

Powiązane problemy