乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > HSSFWorkbook导入导出的时候导入和导出的<em>excel</em>...-poi e

HSSFWorkbook导入导出的时候导入和导出的<em>excel</em>...-poi e

作者:乔山办公网日期:

返回目录:excel表格制作


两个原因:

1.你的excel模版本身有问题,可以尝试新建一个模版。

2.你的excel使用了一些POI不支持的函数。

解决办法:

另存是由excel重写了完整的文件,可以解决问题。

关闭文件例子:

FileOutputStream os = new FileOutputStream("workbook.xls");

wb.write(os);

os.close();



安装一个office2007插件就行了
一、简单说明
excel2003和excel2007区别比较大,最直观的感受就是扩展名不一样。
不过,使用POI的API都是面向接口编程的,实际使用起来区别其实不大。
代码最直观,直接看代码。

二、范例

String EXTENSION_XLS = "xls"

String EXTENSION_XLSX = "xlsx"

Workbook getWorkbook(String filePath)

=

=

=

=

preReadCheck(String filePath)

File file =

(!

FileNotFoundException("传入的文件不存在:" +

(!(filePath.endsWith(EXTENSION_XLS) ||

FileFormatException("传入的文件不是excel"

readExcel(String filePath)

Workbook workbook =

=

(
numSheet = 0; numSheet < workbook.getNumberOfSheets(); numSheet++
=

(sheet ==

"=======================" + sheet.getSheetName() + "========================="

firstRowIndex =

lastRowIndex =

Row firstRow =

(
i = firstRow.getFirstCellNum(); i <= firstRow.getLastCellNum(); i++
=
=
.getCellValue(cell,

" " + cellValue + "\t"
""

(
rowIndex = firstRowIndex + 1; rowIndex <= lastRowIndex; rowIndex++
= sheet.getRow(rowIndex);

firstColumnIndex = currentRow.getFirstCellNum();

lastColumnIndex = currentRow.getLastCellNum();

(
columnIndex = firstColumnIndex; columnIndex <= lastColumnIndex; columnIndex++
= currentRow.getCell(columnIndex);

String currentCellValue =
.getCellValue(currentCell,
);

System.out.print(currentCellValue + "\t"
""
"======================================================"

(workbook !=

String getCellValue(Cell cell,

(cell ==

""

(cell.getCellType() ==

(cell.getCellType() ==
三、其他
在Excel2010上试的时候,excel中明明设置的都是文本类型,但是读取的时候,把数e69da5e6ba90e79fa5e98193330字“1”读成“1.0”。
所以在getCellValue()方法中,把cell单元格的式样改了一下,确保数字“1”文本读出来还是“1”。

2007版本的需要使用XSSFWorkbook来处理,如果zhidao不确定读取的excel版本的话可以写的if判断下如果后缀为".xls"创建HSSFWorkbook 否则创建 XSSFWorkbook来对文件进行操作即可.
Workbook excelWB = null;
Sheet oneSheet = null;
Row oneRow = null;
Cell oneCell = null;
.....
if("xls".equals(excFilSuff)){
excelWB = new HSSFWorkbook(excelFile);
}else{
excelWB = new XSSFWorkbook(excelFile);
}
//获取第0个sheet对象
oneSheet = excelWB.getSheetAt(0);
//获取每行对象
oneRow = oneSheet.getRow(rowNum);
// 获取第几列对象
oneCell = oneRow.getCell(cellNum);

相关阅读

关键词不能为空

ppt怎么做_excel表格制作_office365_word文档_365办公网