本篇文章主要讲的是Excel表格中的单元格Cell。Cell相关的操作包括得到单元格所在的列、行,还可以得到单元格内容的数据格式,也可以得到单元格格式和属性。话不多说下面看看代码是怎么实现的:
这段代码中含有CellFormat、CellFeature是单元格相关的属性,之后的文章中会介绍,这里就不再多说。
import jxl.*;
import java.io.*;
import jxl.read.biff.BiffException;
public class TCell {
public static void main(String [] args) throws IOException, BiffException {
try {
File file = new File("D://JEtest/测试.xls");
Workbook book = Workbook.getWorkbook(file);
Sheet sheet = book.getSheet(0);
Cell cell = sheet.getCell(0, 0);
//返回单元格的行数
int rows = cell.getRow();
System.out.println("该单元格所在的行数: " + rows);
//返回单元格的列数
int col = cell.getColumn();
System.out.println("该单元格所在的列数: " + col);
//返回单元格内容类型
CellType cellType = cell.getType();
System.out.println("该单元格内容类型: " + cellType.toString());
//返回这个单元格是否被隐藏
boolean judgeHidden = cell.isHidden();
System.out.println("该单元格是否被隐藏: " + judgeHidden);
//以字符串的形式返回单元格的内容,对于内容更复杂的操作,把它转换成相应的子接口是有必要的。
String cellContent = cell.getContents();
System.out.println("该单元格的内容: " + cellContent);
//得到应用于这个单元格的格式,如果该单元格的类型是EMPTY,则返null。
//一些空的单元格(比如:在模板电子表格上)可能有单元格类型为EMPTY,但是它实际上包含了格式信息。
jxl.format.CellFormat cellFormat = cell.getCellFormat();
//得到任何特殊单元格属性,例如注释,或者单元格校验当前单元格。如果没有特殊属性,则返回null.
CellFeatures cellFeatures = cell.getCellFeatures();
book.close();
}catch (IOException | BiffException | IndexOutOfBoundsException e) {
System.out.println("Exception: " + e);
throw e;
}
}
}
这段代码中含有CellFormat、CellFeature是单元格相关的属性,之后的文章中会介绍,这里就不再多说。