日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java模拟excel排序_Apache-POI在excel中排序行

發布時間:2023/12/8 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java模拟excel排序_Apache-POI在excel中排序行 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

要對行進行排序,您需要:

將所有行復制到temp

在temp中排序行

從工作表中刪除所有行

使用temp中已排序行的值創建新行

public static void sortSheet(Workbook workbook, Sheet sheet) {

List rows = Lists.newArrayList(sheet.rowIterator());

rows.sort(Comparator.comparing(cells -> cells.getCell(0).getStringCellValue()));

removeAllRows(sheet);

for (int i = 0; i < rows.size(); i++) {

Row newRow = sheet.createRow(i);

Row sourceRow = rows.get(i);

// Loop through source columns to add to new row

for (int j = 0; j < sourceRow.getLastCellNum(); j++) {

// Grab a copy of the old/new cell

Cell oldCell = sourceRow.getCell(j);

Cell newCell = newRow.createCell(j);

// If the old cell is null jump to next cell

if (oldCell == null) {

newCell = null;

continue;

}

// Copy style from old cell and apply to new cell

CellStyle newCellStyle = workbook.createCellStyle();

newCellStyle.cloneStyleFrom(oldCell.getCellStyle());

newCell.setCellStyle(newCellStyle);

// If there is a cell comment, copy

if (oldCell.getCellComment() != null) {

newCell.setCellComment(oldCell.getCellComment());

}

// If there is a cell hyperlink, copy

if (oldCell.getHyperlink() != null) {

newCell.setHyperlink(oldCell.getHyperlink());

}

// Set the cell data type

newCell.setCellType(oldCell.getCellType());

// Set the cell data value

switch (oldCell.getCellType()) {

case BLANK:

newCell.setCellValue(oldCell.getStringCellValue());

break;

case BOOLEAN:

newCell.setCellValue(oldCell.getBooleanCellValue());

break;

case ERROR:

newCell.setCellErrorValue(oldCell.getErrorCellValue());

break;

case FORMULA:

newCell.setCellFormula(oldCell.getCellFormula());

break;

case NUMERIC:

newCell.setCellValue(oldCell.getNumericCellValue());

break;

case STRING:

newCell.setCellValue(oldCell.getRichStringCellValue());

break;

}

}

// If there are are any merged regions in the source row, copy to new row

for (int j = 0; j < sheet.getNumMergedRegions(); j++) {

CellRangeAddress cellRangeAddress = sheet.getMergedRegion(j);

if (cellRangeAddress.getFirstRow() == sourceRow.getRowNum()) {

CellRangeAddress newCellRangeAddress = new CellRangeAddress(newRow.getRowNum(),

(newRow.getRowNum() +

(cellRangeAddress.getLastRow() - cellRangeAddress.getFirstRow()

)),

cellRangeAddress.getFirstColumn(),

cellRangeAddress.getLastColumn());

sheet.addMergedRegion(newCellRangeAddress);

}

}

}

}

private static void removeAllRows(Sheet sheet) {

for (int i = 0; i < sheet.getLastRowNum(); i++) {

sheet.removeRow(sheet.getRow(i));

}}

總結

以上是生活随笔為你收集整理的java模拟excel排序_Apache-POI在excel中排序行的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。