
- Apache POI 教程
- Apache POI - 主頁
- Apache POI - 概覽
- Apache POI - Java Excel API
- Apache POI - 環境
- Apache POI - 核心類
- Apache POI - 工作簿
- Apache POI - 電子表格
- Apache POI - 單元格
- Apache POI - 字型
- Apache POI - 公式
- Apache POI - 超連結
- Apache POI - 列印區域
- Apache POI - 資料庫
- Apache POI 資源
- Apache POI - 問題與解答
- Apache POI - 快速指南
- Apache POI - 有用資源
- Apache POI - 討論
Apache POI - 單元格
任何你輸入到電子表格中的資料總是儲存在一個單元格中。我們使用行和列的標籤來標識單元格。本章將說明如何透過 Java 程式設計操作電子表格中的單元格資料。
新建單元格
在新建單元格前,你需要新建一行。一行的本質就是單元格的集合。
以下程式碼片段可用於新建單元格。
//create new workbook XSSFWorkbook workbook = new XSSFWorkbook(); //create spreadsheet with a name XSSFSheet spreadsheet = workbook.createSheet("new sheet"); //create first row on a created spreadsheet XSSFRow row = spreadsheet.createRow(0); //create first cell on created row XSSFCell cell = row.createCell(0);
單元格的型別
單元格型別指定單元格可以包含字串、數值或公式。字串單元格不能儲存數值,數值單元格不能儲存字串。
以下程式碼用於在電子表格內新建不同型別的單元格。
import java.io.File; import java.io.FileOutputStream; import java.util.Date; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class TypesofCells { public static void main(String[] args)throws Exception { XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet spreadsheet = workbook.createSheet("cell types"); XSSFRow row = spreadsheet.createRow((short) 2); row.createCell(0).setCellValue("Type of Cell"); row.createCell(1).setCellValue("cell value"); row = spreadsheet.createRow((short) 3); row.createCell(0).setCellValue("set cell type BLANK"); row.createCell(1); row = spreadsheet.createRow((short) 4); row.createCell(0).setCellValue("set cell type BOOLEAN"); row.createCell(1).setCellValue(true); row = spreadsheet.createRow((short) 5); row.createCell(0).setCellValue("set cell type date"); row.createCell(1).setCellValue(new Date()); row = spreadsheet.createRow((short) 6); row.createCell(0).setCellValue("set cell type numeric"); row.createCell(1).setCellValue(20 ); row = spreadsheet.createRow((short) 7); row.createCell(0).setCellValue("set cell type string"); row.createCell(1).setCellValue("A String"); FileOutputStream out = new FileOutputStream(new File("typesofcells.xlsx")); workbook.write(out); out.close(); System.out.println("typesofcells.xlsx written successfully"); } }
將以上程式碼儲存到名為TypesofCells.java 的檔案中,從命令提示符編譯並執行如下操作 −
$javac TypesofCells.java $java TypesofCells
如果你的系統配置了 POI 庫,它將編譯並執行,在你的當前目錄中生成名為typesofcells.xlsx 的 Excel 檔案,並顯示以下輸出。
typesofcells.xlsx written successfully
typesofcells.xlsx 檔案如下所示 −

單元格樣式
這裡你可以學習如何進行單元格格式化並應用不同樣式,如合併鄰接單元格、新增邊框、設定單元格對齊方式和填充顏色。
以下程式碼用於透過 Java 程式設計向單元格應用不同樣式。
import java.io.File; import java.io.FileOutputStream; import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.ss.usermodel.BorderStyle; import org.apache.poi.ss.usermodel.HorizontalAlignment; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.VerticalAlignment; 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.XSSFColor; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.ss.usermodel.Color; import org.apache.poi.ss.usermodel.FillPatternType; public class CellStyle { public static void main(String[] args)throws Exception { XSSFWorkbook workbook = new XSSFWorkbook(); 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(HorizontalAlignment.LEFT); style1.setVerticalAlignment(VerticalAlignment.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(HorizontalAlignment.CENTER); style2.setVerticalAlignment(VerticalAlignment.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(HorizontalAlignment.RIGHT); style3.setVerticalAlignment(VerticalAlignment.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(HorizontalAlignment.JUSTIFY); style4.setVerticalAlignment(VerticalAlignment.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(BorderStyle.THICK); style5.setBottomBorderColor(IndexedColors.BLUE.getIndex()); style5.setBorderLeft(BorderStyle.DOUBLE); style5.setLeftBorderColor(IndexedColors.GREEN.getIndex()); style5.setBorderRight(BorderStyle.HAIR); style5.setRightBorderColor(IndexedColors.RED.getIndex()); style5.setBorderTop(BorderStyle.DOTTED); 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(IndexedColors.LIME.index); style6.setFillPattern(FillPatternType.LESS_DOTS); style6.setAlignment(HorizontalAlignment.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(IndexedColors.BLUE.index); style7.setFillPattern( FillPatternType.LESS_DOTS); style7.setAlignment(HorizontalAlignment.FILL); cell.setCellValue("FILL FOREGROUND/FILL PATTERN"); cell.setCellStyle(style7); FileOutputStream out = new FileOutputStream(new File("cellstyle.xlsx")); workbook.write(out); out.close(); System.out.println("cellstyle.xlsx written successfully"); } }
將以上程式碼儲存到名為CellStyle.java 的檔案中,從命令提示符編譯並執行如下操作 −
$javac CellStyle.java $java CellStyle
它將在你的當前目錄中生成一個名為cellstyle.xlsx 的 Excel 檔案並顯示以下輸出。
cellstyle.xlsx written successfully
廣告