poi xssf 怎么实现插入一行
发布网友
发布时间:2022-05-14 03:47
我来回答
共2个回答
热心网友
时间:2024-02-23 13:50
主要是用sheet.shiftRows()方法,可以直接看代码。
/**
* Excel插入一行示例
* Created on 2017/2/8.
*/
public class ExcelDAO {
public void insertRow(String fileName) throws IOException, InvalidFormatException {
File file = new File(fileName);
Workbook workbook = new XSSFWorkbook(new FileInputStream(file));
Sheet sheet = workbook.getSheetAt(0);
sheet.shiftRows(10, sheet.getLastRowNum(), 1); //10-最后一行,向下移动一行
Row newRow = sheet.createRow(10);
Cell newCell = newRow.createCell(0);
newCell.setCellValue("新插入的内容");
workbook.write(new FileOutputStream(fileName));
workbook.close();
}
public static void main(String[] args) throws IOException, InvalidFormatException {
String fileName = "E:\\test.xlsx";
ExcelDAO excelDAO = new ExcelDAO();
excelDAO.insertRow(fileName);
}
}
这是Excel前后对比的截图。
热心网友
时间:2024-02-23 13:51
主要是用sheet.shiftRows()方法,可以直接看代码。
/**
* Excel插入一行示例
* Created on 2017/2/8.
*/
public class ExcelDAO {
public void insertRow(String fileName) throws IOException, InvalidFormatException {
File file = new File(fileName);
Workbook workbook = new XSSFWorkbook(new FileInputStream(file));
Sheet sheet = workbook.getSheetAt(0);
sheet.shiftRows(10, sheet.getLastRowNum(), 1); //10-最后一行,向下移动一行
Row newRow = sheet.createRow(10);
Cell newCell = newRow.createCell(0);
newCell.setCellValue("新插入的内容");
workbook.write(new FileOutputStream(fileName));
workbook.close();
}