📄 workbookoper.java
字号:
package excel;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
public class WorkbookOper {
/*
* COPY 一行
*/
public static void copyRow(HSSFSheet sheet, HSSFRow row, int RowNum,
HSSFCellStyle style) {
HSSFRow rowNew = sheet.createRow(RowNum);
if (row != null) {
for (int i = 0; i <= row.getLastCellNum(); i++) {
HSSFCell cellNew = rowNew.createCell((short) i);
HSSFCell cell = row.getCell((short) i);
if (cell != null) {
cellNew.setCellType(cell.getCellType());
cellNew.setCellValue(getStrValueFromCell(cell));
cellNew.setCellStyle(style);
}
}
} else {
System.out.println("Error:COPY的数据行为null!");
}
}
public static String getStrValueFromCell(HSSFCell cell) {
if (cell != null) {
int cellType = cell.getCellType();
if (cellType == HSSFCell.CELL_TYPE_STRING) {
return cell.getStringCellValue();
} else if (cellType == HSSFCell.CELL_TYPE_BOOLEAN) {
return cell.getBooleanCellValue() ? "true" : "false";
} else if (cellType == HSSFCell.CELL_TYPE_NUMERIC) {
return String.valueOf((int) cell.getNumericCellValue());
} else {
return null;
}
} else {
System.out.println("CELL 为null!");
return null;
}
}
public static String[] getHeadRowArray(HSSFSheet sheet) {
String[] headRowArray = new String[30];
HSSFRow rowFirst = sheet.getRow(sheet.getFirstRowNum());
for (int i = rowFirst.getFirstCellNum(); i <= rowFirst.getLastCellNum(); i++) {
HSSFCell cell = rowFirst.getCell((short) i);
if (cell != null) {
headRowArray[i] = WorkbookOper.getStrValueFromCell(cell);
}
}
return headRowArray;
}
public static HSSFCellStyle getCellStyle(HSSFSheet sheet, int rowNum,
int colNum) {
HSSFRow row = sheet.getRow(rowNum);
if (row != null) {
HSSFCell cell = row.getCell((short) colNum);
if (cell != null) {
return cell.getCellStyle();
} else {
System.out.println("选定的CELL数据为null!行号:" + rowNum + " 列号:"
+ colNum);
return null;
}
} else {
System.out.println("选定的行数据为null!行号:" + rowNum);
return null;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -