首先导入了一个poi.jar 网上有很多这个资源可以下载
XSSFSheet sheet=null;
XSSFWorkbook book=null;
一:查 (查找本地指定位置的excel表格,在控制台输出)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public
void
print_excel(){
//获取excel表格的行数
int
lastrownumber = sheet.getLastRowNum();
String ret=
" "
;
//获取数据
for
(a=
0
;a<lastrownumber;a++){
XSSFRow row=sheet.getRow(a);
//获取excel表格的列数
int
lastcellnum=row.getLastCellNum();
for
(b=
0
;b<lastcellnum;b++){
XSSFCell cell =row.getCell(b);
//判断cell返回的类型并赋值给ret
ret=excel_operation.getExcelCellValue(cell);
System.out.print(ret+
" "
);
}
System.out.println();
}
}
|
二:改 (修改excel表格中某一单元格的内容)
1
2
3
4
5
6
7
8
9
10
11
|
public
void
set_excelcell(
int
i,
int
j,String str){
//获取行的信息
XSSFRow row=sheet.getRow(i-
1
);
//获取列的信息
XSSFCell cell =row.getCell(j-
1
);
//获取被修改单元格的内容
String string = excel_operation.getExcelCellValue(cell);
//修改单元格的内容为str
cell.setCellValue(str);
System.out.println(
"已将"
+string+
"改为"
+str);
}
|
三:增 (在excel表格中插入一行内容到指定位置)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
public
void
insert(
int
rowIndex, String[] objs) {
if
(rowIndex ==
0
) {
throw
new
IllegalArgumentException(
"不能插在第0行,第0行是用来定义的!"
);
}
if
(rowIndex > sheet.getLastRowNum() +
1
) {
throw
new
IllegalArgumentException(
"最多只能插入在最后一行的后面。"
);
}
int
referRowIndex = -
1
;
//参考行的行号。
if
(sheet.getPhysicalNumberOfRows() <=
1
) {
referRowIndex = rowIndex -
1
;
}
else
{
referRowIndex = rowIndex -
1
;
if
(rowIndex == sheet.getLastRowNum() +
1
) {
//是插入最后一行
//不做任何处理
}
else
{
//往下移动一位
sheet.shiftRows(rowIndex, sheet.getLastRowNum(),
1
,
true
,
false
);
}
}
Row targetRow = sheet.createRow(rowIndex);
Row referRow = sheet.getRow(referRowIndex);
// 参考行
Cell targetCell, referCell;
for
(
int
i =
0
; i < objs.length; i++) {
targetCell = targetRow.createCell(i);
referCell = referRow.getCell(i);
targetCell.setCellStyle(referCell.getCellStyle());
targetCell.setCellType(referCell.getCellType());
targetCell.setCellValue(objs[i]);
// 设置值
}
}
|
四: 删 (删除指定行的内容)
1
2
3
4
5
6
7
8
9
10
11
12
13
|
// 删除一行数据(Excel表中,行是从0起算的)
public
void
delete(
int
rowIndex) {
//删除的是最后一行
if
(rowIndex == sheet.getLastRowNum()) {
sheet.removeRow(sheet.getRow(sheet.getLastRowNum()));
//删除的不是最后一行
}
else
{
sheet.shiftRows(rowIndex +
1
, sheet.getLastRowNum(), -
1
,
true
,
false
);
sheet.removeRow(sheet.getRow(sheet.getLastRowNum() +
1
));
}
}
|
五: 判断返回类型 (因为excel表格中的内容不同,有字符型的,有整数型的等等,必须进行判断其类型才能进行输出)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
private
static
String getExcelCellValue(XSSFCell cell) {
String ret=
" "
;
try
{
//当返回值的类型为空返回空格
if
(cell ==
null
) {
ret =
" "
;
//当返回值的类型为字符串类型
}
else
if
(cell.getCellType() == XSSFCell.CELL_TYPE_STRING) {
ret = cell.getStringCellValue();
//当返回值的类型为数值类型
}
else
if
(cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {
ret =
""
+ cell.getNumericCellValue();
//当返回值的类型为表达式类型
}
else
if
(cell.getCellType() == XSSFCell.CELL_TYPE_FORMULA) {
ret = cell.getCellFormula();
//当返回值的类型为异常类型
}
else
if
(cell.getCellType() == XSSFCell.CELL_TYPE_ERROR) {
ret =
" "
+ cell.getErrorCellValue();
//当返回值的类型为布尔类型
}
else
if
(cell.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN) {
ret =
" "
+ cell.getBooleanCellValue();
//当返回值的类型为空的时候
}
else
if
(cell.getCellType() == XSSFCell.CELL_TYPE_BLANK) {
ret =
" "
;
}
}
catch
(Exception ex) {
ex.printStackTrace();
ret =
" "
;
}
return
ret;
}
|