本篇文章主要介绍了"excl 自动化excl学习笔记",主要涉及到excl方面的内容,对于软件工程感兴趣的同学可以参考一下:
学习excl操作import java.io.File;import java.io.FileInputStream;import java.io.FileOu...
Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum);
// 如果单元格的内容为字符串类型,则使用 getStringCellValue 方法获取单元格的内容
// 如果单元格的内容为数字类型,则使用 getNumericCellValue() 方法获取单元格的内容
String CellData = "";
/** 获取单元格类型 */
if (Cell.getCellType() == XSSFCell.CELL_TYPE_STRING) { // 如果是字符串
CellData = Cell.getStringCellValue();
} else if (Cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) { // 如果是数字
DecimalFormat df = new DecimalFormat("0");
CellData = df.format(Cell.getNumericCellValue());
}
return CellData;
} catch (Exception e) {
e.printStackTrace();
return ""; //如果异常返回空
}
}
/**在Excel文件的执行单元格中写输入数据,此函数只支持后缀为xlsx的文件写入
* @param result 结果
* @param rowNum 行数
* @param cellNum 列数
* */
public void setCellData(int RowNum, int ColNum, String result) throws Exception {
try {
// 获取 excel文件中的行对象
Row = ExcelWSheet.getRow(RowNum);
// 如果单元格为空,则返回 Null
Cell = Row.getCell(ColNum, Row.RETURN_BLANK_AS_NULL);
if (Cell == null ) {
// 当单元格对象是 null 的时候,则创建单元格
// 如果单元格为空,无法直接调用单元格对象的 setCellValue 方法设定单元格的值
Cell = Row.createCell(ColNum);
// 创建单元格后可以调用单元格对象的 setCellValue 方法设定单元格的值
Cell.setCellValue(result);
} else {
// 单元格中有内容,则可以直接调用单元格对象的 setCellValue 方法设定单元格的值
Cell.setCellValue(result);
System.out.println("执行完成");
}
// 实例化写入 excel 文件的文件输出流对象
FileOutputStream fileOut = new FileOutputStream(filePath);
// 将内容写入 excel 文件中
ExcelWBook.write(fileOut);
// 调用flush 方法强制刷新写入文件
fileOut.flush();
// 关闭文件输出流对象
fileOut.close();
} catch (Exception e) {
e.printStackTrace();
throw (e);
}
}
/**从 excel 文件获取测试数据的静态方法*/