修改excel表格单元格内容
package com.zhb.sysmanage.datadictionary;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import java.io.*;
/**
* @Author:zhb
* @CreateTime: 2018/10/23 16:11
* @Description:
*/
public class TT {
public static void main(String[] args) {
try {
aa();
} catch (Exception e) {
e.printStackTrace();
}
}
static void aa() throws FileNotFoundException,IOException, InvalidFormatException {
String path = "E:\\System\\Desktop\\施工记录表\\强夯施工记录表601-700(已打完2)";
File folder = new File(path);
//list()获取目录下所有文件
//list()获取目录下所有文件及目录的绝对路径
//使用增强for遍历目录下文件(批量处理xml文件)
InputStream excelFileInputStream = null;
Workbook wb = null;
for (File f : folder.listFiles()) {
System.out.println("获取文件:" + f);
// 创建 Excel 文件的输入流对象
excelFileInputStream = new FileInputStream(f);
// FileOutputStream excelFileOutPutStream = new FileOutputStream(f);
wb = WorkbookFactory.create(excelFileInputStream);
excelFileInputStream.close();
//获取Excel的工作表sheet,下标从0开始。
Sheet sheet = wb.getSheetAt(0);
//获取Excel的行,下标从0开始
Row row = sheet.getRow(3);
//获取指定单元格,单元格从左到右下标从0开始
Cell cell = row.getCell(5);
//获取单元格内容,作为String类型
if (cell != null) {
System.out.println(cell);
//给单元格设值
cell.setCellValue("1111111");
System.out.println(cell);
}
OutputStream out = new FileOutputStream(f);
wb.write(out);
}
}
}