开源项目
知识点
相关文章
更多最近更新
更多POI Cells单元格数据处理
2019-04-24 09:28|来源: 网路
输入到电子表格中的任何数据总是存储在一个单元中。我们使用的行和列的标签来识别单元格。本章介绍了如何使用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);
单元格类型
单元格类型指定单元格是否可以包含字符串,数值,或公式。字符串单元不能持有数值和数值单元格无法容纳字符串。下面给出是单元格值和类型的语法。
单元格的值类型 | 类型语法 |
---|---|
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 |
以下代码是用于在电子表格创建不同类型的单元格。
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.createCell(0).setCellValue("set cell type numeric" ); row.createCell(1).setCellValue(20 ); row = spreadsheet.createRow((short) 8); 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.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 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(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("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
cellstyle.xlsx文件如下所示。
相关问答
更多-
你可以使用sheet.addMergedRegion(rowFrom,rowTo,colFrom,colTo); 示例sheet.addMergedRegion(new CellRangeAddress(1,1,4,1)); 将从B2合并到E2。 记住它是基于零的索引。 有关详细信息,请参阅BusyDeveloper指南 You can use sheet.addMergedRegion(rowFrom,rowTo,colFrom,colTo); example sheet.addMergedRegion( ...
-
一旦你问了一个让我试了一段时间代码的问题。 你的问题是这一行出现3次,每次评论前一次。 patr = sheet.createDrawingPatriarch(); 从这个方法的文档 , 创建顶级绘图族长。 这将删除此工作表上的任何现有图纸。 因此,每次创建DrawingPatriarch时都会删除您之前的注释。 所以在comment1之前comment1一次。 其他2次,删除或注释掉这一行。 For once you've asked a question which had me trying out ...
-
实际上我完全不了解你的问题,但我有一些关键点,我认为这对你会有所帮助。 合并区域的第一个Cell保留合并单元格的值,其余单元格保持空白值。 如果单元格A2到A5与值“Test”合并,则在迭代A2中将显示“测试”,而其余部分显示为空白。 所以如果你想读取合并的单元格值,你只需要读取它的第一个单元格,同样如果你想写入合并的单元格,你只需要写入第一个单元格。 sheet.getNumMergedRegions(); 返回整数,该整数将是工作表中合并区域的总数。 你也可以使用循环遍历它。 CellRangeAddr ...
-
您需要“评估”单元格以获得公式的结果。 这不是由POI自动完成的,因为它可能是一项繁重的操作,往往不是必需的。 有关详细信息,请参阅http://poi.apache.org/spreadsheet/eval.html ,基本上,您将创建一个FormulaEvaluator并为有问题的单元检索CellValue FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator(); ... CellValue cellVal ...
-
请参阅POI公式评估文档 。 完成对电子表格的更改后,您需要重新计算公式评估,因为文件格式会缓存最后一个值以快速加载。 See the POI Formula Evaluation Documentation. Once you are done making changes to the spreadsheet, you'll need to recalculate the formula evaluations, as the file format caches the last value to m ...
-
根据此版本3.17发行说明,它说“将单元格对齐常量从CellStyle迁移到HorizontalAlignment和VerticalAlignment枚举”使用下面的代码代替 style.setVerticalAlignment(VerticalAlignment.TOP); cellStyle.setBorderBottom(BorderStyle.THIN); cellStyle.setBorderTop(BorderStyle.THIN); cellStyle.setBorde ...
-
对于任何公式单元格,使用poi3.8。 Workbook xlsWorkbook = null; Cell cell = null; FormulaEvaluator formulaEval = xlsWorkbook.getCreationHelper().createFormulaEvaluator(); String value=formulaEval.evaluate(cell).formatAsString(); System.out.println("Formula Cell value : ...
-
如何使用apache POI java库在电子表格中取消合并单元格(How to unmerge cells in a spreadsheet using apache POI java library)[2023-09-14]
促进对答案的评论... 你想要的方法是Sheet.removeMergedRegion(int) 这将获取您添加它时获得的区域索引。 否则,您可以使用getNumMergedRegions()和getMergedRegion(int)迭代区域以查找要删除的索引 Promoting a comment to an answer... The method you want is Sheet.removeMergedRegion(int) That takes the index of the region, ... -
在将单元格添加到List之前,您可以检查单元格值。 就像是: if (cell.getStringCellValue() != null && cell.getStringCellValue().length() != 0) { cellVector.add(cell); } 如果有某些内容,您只需添加单元格。 Before adding the cell to the List, you can check the cell value. Something like: if (cel ...
-
在您的代码中,您正在使用cellIterator Iterator
cells = row.cellIterator(); cellIterator不包含null的单元格。 您必须从第一个单元格开始循环到最后一个单元格,然后检查单元格是否为空,而不是使用cellIterator。 您可以参考下面的示例代码 - for (int colNum = 0; colNum < row.getLastCellNum(); colNum++) { Cell cell = row.getCell(c ... |