本篇使用xlsx表格如果要用xls表格将所有的XSSF换成HSSF就行
我又找到了国内的一个excel工具包hutool [http://hutool.mydoc.io/] 有以下优点:
1.国内的开发文档容易看懂(毕竟是中文的吗不多做详解,不过感觉虽然这个比较容易学习但是必须也要会apache的吗,毕竟这个是全球顶尖的,几乎也是最全的,并且hutool的poi也是在apache的基础上写的)
2.入门建议最好还是先用这个,代码少不易出现问题
进入正题:
1.我使用的jar包是apache的poi系列的jar包,我引入的jar包有(版本我就不写了百度搜索maven公共库自己用最新版本)
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
</dependency>
2.详细代码。在updatexlsx方法中有行workbook.getCreationHelper().createFormulaEvaluator().evaluateAll()用于公式计算,如果xlsx中有中有公式就加上
public class Test {
/**
* Excel 文件要存放的位置,假定在D盘下
*/
public static String outputFile = "C:\\Users\\Qitain\\Desktop\\公式测试.xlsx";
public static void main(String argv[]) {
Test test = new Test();
try {
test.updatexlsx();
test.getxlsx();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 修改
* @throws Exception
*/
public void updatexlsx() throws Exception {
// 创建 Excel 文件的输入流对象
FileInputStream excelFileInputStream = new FileInputStream(outputFile);
// XSSFWorkbook 就代表一个 Excel 文件
// 创建其对象,就打开这个 Excel 文件
XSSFWorkbook workbook = new XSSFWorkbook(excelFileInputStream);
// 输入流使用后,及时关闭!这是文件流操作中极好的一个习惯!
excelFileInputStream.close();
// XSSFSheet 代表 Excel 文件中的一张表格
// 我们通过 getSheetAt(0) 指定表格索引来获取对应表格
// 注意表格索引从 0 开始!
XSSFSheet sheet = workbook.getSheetAt(0);
// ------ 创建一行新的数据 ----------//
// 指定行索引,创建一行数据, 行索引为当前最后一行的行索引 + 1
int currentLastRowIndex = sheet.getLastRowNum();
int newRowIndex = currentLastRowIndex + 1;
XSSFRow newRow = sheet.createRow(newRowIndex);
// 开始创建并设置该行每一单元格的信息,该行单元格的索引从 0 开始
int cellIndex = 0;
// 创建一个单元格,设置其内的数据格式为字符串,并填充内容,其余单元格类同
XSSFCell newNameCell = sheet.getRow(2).getCell(1);
newNameCell.setCellValue(2);
XSSFCell newGenderCell = sheet.getRow(2).getCell(2);
newGenderCell.setCellValue(2);
XSSFCell newAgeCell = sheet.getRow(2).getCell(3);
newAgeCell.setCellValue(2);
XSSFCell newWeightCell = sheet.getRow(2).getCell(4);
newWeightCell.setCellValue(2);
XSSFCell newSalaryCell = sheet.getRow(2).getCell(5);
newSalaryCell.setCellValue(2);
//公式在计算
workbook.getCreationHelper().createFormulaEvaluator().evaluateAll();
// 将最新的 Excel 数据写回到原始 Excel 文件(就是D盘那个 Excel 文件)中
// 首先要创建一个原始Excel文件的输出流对象!
FileOutputStream excelFileOutPutStream = new FileOutputStream(outputFile);
// 将最新的 Excel 文件写入到文件输出流中,更新文件信息!
workbook.write(excelFileOutPutStream);
// 执行 flush 操作, 将缓存区内的信息更新到文件上
excelFileOutPutStream.flush();
// 使用后,及时关闭这个输出流对象, 好习惯,再强调一遍!
excelFileOutPutStream.close();
}
/**
* 读取excel
*/
public void getxlsx() throws Exception {
// 创建 Excel 文件的输入流对象
FileInputStream excelFileInputStream = new FileInputStream(outputFile);
// XSSFWorkbook 就代表一个 Excel 文件
// 创建其对象,就打开这个 Excel 文件
XSSFWorkbook workbook = new XSSFWorkbook(excelFileInputStream);
// 输入流使用后,及时关闭!这是文件流操作中极好的一个习惯!
excelFileInputStream.close();
// XSSFSheet 代表 Excel 文件中的一张表格
// 我们通过 getSheetAt(0) 指定表格索引来获取对应表格
// 注意表格索引从 0 开始!
XSSFSheet sheet = workbook.getSheetAt(0);
// 开始循环表格数据,表格的行索引从 0 开始
// employees.xlsx 第一行是标题行,我们从第二行开始, 对应的行索引是 1
// sheet.getLastRowNum() : 获取当前表格中最后一行数据对应的行索引
System.out.println(sheet.getLastRowNum());
for (int rowIndex = 1; rowIndex <= sheet.getLastRowNum(); rowIndex++) {
// XSSFRow 代表一行数据
XSSFRow row = sheet.getRow(rowIndex);
if (row == null)
continue;
XSSFCell nameCell = row.getCell(13); // 姓名列
XSSFCell genderCell = row.getCell(14); // 性别列
XSSFCell ageCell = row.getCell(15); // 年龄列
//nameCell.setCellType(CellType.STRING);
//genderCell.setCellType(CellType.STRING);
//ageCell.setCellType(CellType.STRING);
//weightCell.setCellType(CellType.STRING);
//salaryCell.setCellType(CellType.STRING);
StringBuilder employeeInfoBuilder = new StringBuilder();
employeeInfoBuilder.append(rowIndex + 1 + "员工信息 --> ")
.append("姓名 : ").append(nameCell.getNumericCellValue())
.append(" , 性别 : ").append(genderCell.getNumericCellValue())
.append(" , 年龄 : ").append(ageCell.getNumericCellValue());
System.out.println(employeeInfoBuilder.toString());
}
workbook.close();
}
/**
* 创建xlsx
*/
public void createxlsx() {
try {
// 创建新的Excel 工作簿
XSSFWorkbook workbook = new XSSFWorkbook();
// 在Excel工作簿中建一工作表,其名为缺省值
// 如要新建一名为"效益指标"的工作表,其语句为:
// HSSFSheet sheet = workbook.createSheet("sheet1");
XSSFSheet sheet = workbook.createSheet();
// 在索引0的位置创建行(第一行)
XSSFRow row = sheet.createRow((short) 0);
//在索引0的位置创建单元格(第一列)
XSSFCell cell = row.createCell((short) 0);
// 定义单元格为字符串类型(Excel-设置单元格格式-数字-文本;不设置默认为“常规”,也可以设置成其他的,具体设置参考相关文档)
// cell.setCellType(XSSFCell.CELL_TYPE_STRING);
// 在单元格中输入一些内容
cell.setCellValue(123);
// 新建一输出文件流
FileOutputStream fOut = new FileOutputStream(outputFile);
// 把相应的Excel 工作簿存盘
workbook.write(fOut);
fOut.flush();
// 操作结束,关闭文件
fOut.close();
System.out.println("文件生成");
} catch (Exception e) {
System.out.println("已运行 xlCreate() : " + e);
}
}
}