Java如何將不同樣式應用於Excel中的單元格?

2019-10-16 22:27:43

在Java程式設計中,如何將不同樣式應用於Excel中的單元格?

注意:需要存取網址:http://poi.apache.org/download.html , 下載一個Apache POI軟體包。這裡下載最新版本:poi-bin-3.17-20170915.tar.gz解壓並將全部.jar檔案匯入 。

需要匯入全部包,如下圖所示 -

參考範例:

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

以下是使用Java如何將不同樣式應用於Excel中的單元格的程式。

import java.io.File;
import java.io.FileOutputStream;

import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.util.CellRangeAddress;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class DifferentStylesToCell {
   public static void main(String[] args)throws Exception {

      //Create a Work Book
      XSSFWorkbook workbook = new XSSFWorkbook();

      //Create a Spread Sheet
      XSSFSheet spreadsheet = workbook.createSheet("cellstyle");
      XSSFRow row = spreadsheet.createRow((short) 1);
      row.setHeight((short) 800);
      XSSFCell cell = (XSSFCell) row.createCell((short) 1);
      cell.setCellValue("test of merging");

      //MEARGING CELLS
      //this statement for merging cells
      spreadsheet.addMergedRegion(new CellRangeAddress(
         1,  //first row (0-based)
         1,  //last row (0-based)
         1,  //first column (0-based)
         4   //last column (0-based)
      )); 

      //CELL Alignment
      row = spreadsheet.createRow(5);

      cell = (XSSFCell) row.createCell(0);
      row.setHeight((short) 800);

      // Top Left alignment
      XSSFCellStyle style1 = workbook.createCellStyle();

      spreadsheet.setColumnWidth(0, 8000);
      style1.setAlignment(XSSFCellStyle.ALIGN_LEFT);
      style1.setVerticalAlignment(XSSFCellStyle.VERTICAL_TOP);

      cell.setCellValue("Top Left");
      cell.setCellStyle(style1);
      row = spreadsheet.createRow(6);
      cell = (XSSFCell) row.createCell(1);
      row.setHeight((short) 800);

      //Center Align Cell Contents
      XSSFCellStyle style2 = workbook.createCellStyle();

      style2.setAlignment(XSSFCellStyle.ALIGN_CENTER);
      style2.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
      cell.setCellValue("Center Aligned");
      cell.setCellStyle(style2);
      row = spreadsheet.createRow(7);
      cell = (XSSFCell) row.createCell(2);
      row.setHeight((short) 800);

      //Bottom Right alignment
      XSSFCellStyle style3 = workbook.createCellStyle();

      style3.setAlignment(XSSFCellStyle.ALIGN_RIGHT);
      style3.setVerticalAlignment(XSSFCellStyle.VERTICAL_BOTTOM);
      cell.setCellValue("Bottom Right");
      cell.setCellStyle(style3);
      row = spreadsheet.createRow(8);
      cell = (XSSFCell) row.createCell(3);

      //Justified Alignment
      XSSFCellStyle style4 = workbook.createCellStyle();

      style4.setAlignment(XSSFCellStyle.ALIGN_JUSTIFY);
      style4.setVerticalAlignment(XSSFCellStyle.VERTICAL_JUSTIFY);
      cell.setCellValue("Contents are Justified in Alignment");
      cell.setCellStyle(style4);

      //CELL BORDER
      row = spreadsheet.createRow((short) 10);

      row.setHeight((short) 800);
      cell = (XSSFCell) row.createCell((short) 1);
      cell.setCellValue("BORDER");
      XSSFCellStyle style5 = workbook.createCellStyle();

      style5.setBorderBottom(XSSFCellStyle.BORDER_THICK);
      style5.setBottomBorderColor(IndexedColors.BLUE.getIndex());
      style5.setBorderLeft(XSSFCellStyle.BORDER_DOUBLE);
      style5.setLeftBorderColor(IndexedColors.GREEN.getIndex());
      style5.setBorderRight(XSSFCellStyle.BORDER_HAIR);
      style5.setRightBorderColor(IndexedColors.RED.getIndex());
      style5.setBorderTop(XSSFCellStyle.BIG_SPOTS);
      style5.setTopBorderColor(IndexedColors.CORAL.getIndex());
      cell.setCellStyle(style5);

      //Fill Colors
      //background color
      row = spreadsheet.createRow((short) 10 );

      cell = (XSSFCell) row.createCell((short) 1);
      XSSFCellStyle style6 = workbook.createCellStyle();

      style6.setFillBackgroundColor(HSSFColor.LEMON_CHIFFON.index );
      style6.setFillPattern(XSSFCellStyle.LESS_DOTS);
      style6.setAlignment(XSSFCellStyle.ALIGN_FILL);

      spreadsheet.setColumnWidth(1,8000);
      cell.setCellValue("FILL BACKGROUNG/FILL PATTERN");
      cell.setCellStyle(style6);

      //Foreground color
      row = spreadsheet.createRow((short) 12);

      cell = (XSSFCell) row.createCell((short) 1);
      XSSFCellStyle style7=workbook.createCellStyle();
      style7.setFillForegroundColor(HSSFColor.BLUE.index);
      style7.setFillPattern( XSSFCellStyle.LESS_DOTS);
      style7.setAlignment(XSSFCellStyle.ALIGN_FILL);

      cell.setCellValue("FILL FOREGROUND/FILL PATTERN");
      cell.setCellStyle(style7);

      FileOutputStream out = new FileOutputStream(
         new File("differentCellStyle.xlsx"));

      workbook.write(out);
      out.close();
      System.out.println("cellstyle.xlsx written successfully");
   }
}

執行上面範例程式碼,得到以下結果 -



建立的Excel檔案內容,如下所示 -