11 Mayıs 2014 Pazar

Apache POI - Excel Kenarlik İşlemleri

Apache POI kütüphanesini kullanarak oluşturacağımız excel dosyalarında hücrelere kenarlık eklemek için HSSFCellStyle(xlsx için XSSFCellStyle) sınıfından yararlanılır.

import java.io.FileOutputStream;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
 
public class BorderDemo {
 
   public static void main(String[] args) {
      
      HSSFWorkbook wb = new HSSFWorkbook();
      HSSFSheet sheet = wb.createSheet("Sayfa1");

      Row row = sheet.createRow(0);

      Cell cell = row.createCell(1);
      cell.setCellValue("JAVA 4 FUN");
      
      //Hucrenin bulundugu sutunun genisligini icerige gore otomatik olarak genislet
      sheet.autoSizeColumn(cell.getColumnIndex());
      
      // Bicim nesnesini olustur ve kenarlik ozellikleri ekle.
      HSSFCellStyle style = wb.createCellStyle();
      
      //Ust Kenarlik
      style.setBorderTop(HSSFCellStyle.BORDER_DASHED);
      style.setTopBorderColor(IndexedColors.GREEN.getIndex());
      
      //Sag Kenarlik
      style.setBorderRight(HSSFCellStyle.BORDER_THIN);
      style.setRightBorderColor(IndexedColors.BLACK.getIndex());
      
      //Alt kenarlik
      style.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM_DASH_DOT);
      style.setBottomBorderColor(IndexedColors.BLUE.getIndex());
      
      //Sol kenarlik
      style.setBorderLeft(HSSFCellStyle.BORDER_THICK);
      style.setLeftBorderColor(IndexedColors.RED.getIndex());
      
      //Hucreye bicim nesnesini ekle
      cell.setCellStyle(style);

      //Dosyaya yaz
      try(FileOutputStream file = new FileOutputStream("k://test.xls"))
      {
          wb.write(file);
          file.close();
      }
      catch (Exception e) {
          e.printStackTrace();
      }
   }
   
}
#ÇIKTI :

#KAYNAK :
http://poi.apache.org/spreadsheet/quick-guide.html#Borders

0 yorum:

Yorum Gönder

Java4Fun Copyright © 2011 | Template created by O Pregador | Powered by Templates de luxo