poi xssf 怎么实现插入一行
2017-02-08
展开全部
主要是用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前后对比的截图。
展开全部
主要是用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();
}
/**
* 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();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询