如,excle格式如下,如果输出sheet的行数的话现在是3行,但是假如第四行我进行了单元格合并,即使我再取消该单元格合并的话,也会显示4行,我的是Excel2003,大家可以试试的。
A B C D ....
1 1 2 3 4
2 5 6 7
3 8 9 0
4
5
...
因为我想读取边框的类型,如上所属我对第4行进行了单元格合并即使取消的话也会报java.lang.NullPointerException,我的代码如下:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;import jxl.Sheet;
import jxl.Workbook;
import jxl.format.Border;
import jxl.read.biff.BiffException;public class ReadExcel {
public static void main(String[] args) {
try {
InputStream f=new FileInputStream("e:/my.xls");
Workbook workbook = null;
try {
workbook = Workbook.getWorkbook(f);
} catch (BiffException e) {
e.printStackTrace();
}
Sheet sheet = workbook.getSheet(0);
System.out.println(sheet.getRows());
for (int i = 0; i < sheet.getRows(); i++) {
for (int j = 0; j < sheet.getColumns(); j++) {
jxl.Cell c2;
Border b = null;
c2 = sheet.getCell(j, i);
System.out.println("Row:"+i+"<-->Col:"+j);
String bline=null;
//bline=c2.getCellFormat().getBorder(Border.RIGHT)
//.getDescription();
bline=c2.getCellFormat().getBorderLine(b.TOP).getDescription().toString();
//上面两行都可以读取边框类型,有什么区别吗?
System.out.println(bline);
String content=c2.getContents().toString();
System.out.println(content);
}
}
workbook.close();
f.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
A B C D ....
1 1 2 3 4
2 5 6 7
3 8 9 0
4
5
...
因为我想读取边框的类型,如上所属我对第4行进行了单元格合并即使取消的话也会报java.lang.NullPointerException,我的代码如下:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;import jxl.Sheet;
import jxl.Workbook;
import jxl.format.Border;
import jxl.read.biff.BiffException;public class ReadExcel {
public static void main(String[] args) {
try {
InputStream f=new FileInputStream("e:/my.xls");
Workbook workbook = null;
try {
workbook = Workbook.getWorkbook(f);
} catch (BiffException e) {
e.printStackTrace();
}
Sheet sheet = workbook.getSheet(0);
System.out.println(sheet.getRows());
for (int i = 0; i < sheet.getRows(); i++) {
for (int j = 0; j < sheet.getColumns(); j++) {
jxl.Cell c2;
Border b = null;
c2 = sheet.getCell(j, i);
System.out.println("Row:"+i+"<-->Col:"+j);
String bline=null;
//bline=c2.getCellFormat().getBorder(Border.RIGHT)
//.getDescription();
bline=c2.getCellFormat().getBorderLine(b.TOP).getDescription().toString();
//上面两行都可以读取边框类型,有什么区别吗?
System.out.println(bline);
String content=c2.getContents().toString();
System.out.println(content);
}
}
workbook.close();
f.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
简单地说,总结我所遇到的问题。
1、就是最后一行进行了单元格合并,如果你取消单元格合并的话,计算行数的时候也会算上改行。
2、最后进行了单元格合并的行,如果是最后一行并且改行没有数据值的话就会报错,有值就不会报错。
感兴趣的朋友试试,是我程序用的方法不对,还是jxl的bug