本文概述
要将行从顶部移到底部,反之亦然,Apache POI提供了shiftRows()方法来执行行移动。此方法需要开始行,结束行和要移动的行数。
让我们看一个例子来了解整个变速过程。
Apache POI移位行示例
package poiexample;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
public class ShiftRowsExample {
public static void main(String[] args) {
try (OutputStream os = new FileOutputStream("srcmini.xls")) {
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("Sheet");
Row row = sheet.createRow(1);
Row row2 = sheet.createRow(2);
Cell cell = row.createCell(1);
Cell cell2 = row2.createCell(1);
cell2.setCellValue("Hello, srcmini");
sheet.shiftRows(1, 2, -1);
wb.write(os);
}catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
输出:
上班前
移到顶部后一行。
评论前必须登录!
注册