利用poi向excle写入数据
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
public class PoiWriteExcel {
public void excelWrite(String filePath,String sheetName,int rowNo,int cellNo,String value) throws Exception, InvalidFormatException, IOException{
//读取excel文件
FileInputStream inputStream = new FileInputStream(filePath);
//以工厂模式创建excel文件对象,支持.xls和.xlsx的所有excel文件
Workbook book = WorkbookFactory.create(inputStream);
//获取指定名称的sheet文本
Sheet sheet = book.getSheet(sheetName);
//获取要写入数据的目标行
Row row = sheet.getRow(rowNo);
//向excel文件中写入数据
FileOutputStream outputStream = new FileOutputStream(filePath);
//向目标行的目标单元格中写入数据
row.getCell(cellNo).setCellValue(value);
outputStream.flush();
book.write(outputStream);
outputStream.close();
}
}