輸入到電子表格中的任何數(shù)據(jù)總是存儲在一個單元中。我們使用的行和列的標(biāo)簽來識別單元格。本章介紹了如何使用Java編程操縱單元電子表格的數(shù)據(jù)。
需要創(chuàng)建一個單元之前創(chuàng)建一個行。行是什么?只不過是單元的集合。
下面的代碼片段用于創(chuàng)建一個單元格。
//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);
單元格類型指定單元格是否可以包含字符串,數(shù)值,或公式。字符串單元不能持有數(shù)值和數(shù)值單元格無法容納字符串。下面給出是單元格值和類型的語法。
單元格的值類型 | 類型語法 |
---|---|
Blank cell value | XSSFCell.CELL_TYPE_BLANK |
Boolean cell value | XSSFCell.CELL.TYPE_BOOLEAN |
Error cell value | XSSFCell.CELL_TYPE_ERROR |
Numeric cell value | XSSFCell.CELL_TYPE_NUMERIC |
String cell value | XSSFCell.CELL_TYPE_STRING |
以下代碼是用于在電子表格創(chuàng)建不同類型的單元格。
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 ERROR"); row.createCell(1).setCellValue(XSSFCell.CELL_TYPE_ERROR ); row = spreadsheet.createRow((short) 6); row.createCell(0).setCellValue("set cell type date"); row.createCell(1).setCellValue(new Date()); row = spreadsheet.createRow((short) 7); row上一篇:POI教程下一篇:Apache POI環(huán)境設(shè)置