我的步骤是:1、复制模板excel文件为新文件new.xsl:
if (!thefile.exists()) {
//copy excel
FileInputStream fis = new FileInputStream(moulepathinfo);
FileOutputStream fos = new FileOutputStream(thepathinfo);
byte[] buf = new byte[1024];
int i = 0;
while ((i = fis.read(buf)) != -1) {
fos.write(buf, 0, i);
}
fis.close();
fos.close();
}
2、在new.xsl中更新cell值:
String outputFile = thepathinfo;
HSSFWorkbook workbook =
new HSSFWorkbook(new FileInputStream(outputFile));
HSSFSheet sheet = workbook.getSheet("Sheet1");
int rows = sheet.getPhysicalNumberOfRows();
for (int i = 1; i < rows; i++) {
HSSFRow row = sheet.getRow(i);
for (int j = 0; j < 3; j++) {
if (row == null) {
System.err.println("row is null.");
}
HSSFCell cell = row.getCell((short) j);
if (cell == null) {
System.err.println("cell is null.");
}
String CellValue = cell.getStringCellValue();
if (CellValue.equals("测试")) {
HSSFRow CountRow = sheet.getRow(i);
HSSFCell CountCell = CountRow.getCell((short) 3);
String CountValue = CountCell.getStringCellValue();
if (CountValue.equals(""))
CountValue = "0";
String Count =
Integer.toString(Integer.parseInt(CountValue) + 1);
CountCell.setCellValue(Count); //更新数据
FileOutputStream fOut = new FileOutputStream(outputFile);
workbook.write(fOut);
fOut.close();
break;
}
}
}
问题:有时候会出现getRow或者getCell出现NULL,但是excel文件打开正常,而且如果在出现NULL的那一行或者那个cell中手动输入空格,保存后再次执行就没有问题,这个究竟是什么原因,怎么解决。poi版本是jakarta-poi-1.10.0-dev-20030222.jar
if (!thefile.exists()) {
//copy excel
FileInputStream fis = new FileInputStream(moulepathinfo);
FileOutputStream fos = new FileOutputStream(thepathinfo);
byte[] buf = new byte[1024];
int i = 0;
while ((i = fis.read(buf)) != -1) {
fos.write(buf, 0, i);
}
fis.close();
fos.close();
}
2、在new.xsl中更新cell值:
String outputFile = thepathinfo;
HSSFWorkbook workbook =
new HSSFWorkbook(new FileInputStream(outputFile));
HSSFSheet sheet = workbook.getSheet("Sheet1");
int rows = sheet.getPhysicalNumberOfRows();
for (int i = 1; i < rows; i++) {
HSSFRow row = sheet.getRow(i);
for (int j = 0; j < 3; j++) {
if (row == null) {
System.err.println("row is null.");
}
HSSFCell cell = row.getCell((short) j);
if (cell == null) {
System.err.println("cell is null.");
}
String CellValue = cell.getStringCellValue();
if (CellValue.equals("测试")) {
HSSFRow CountRow = sheet.getRow(i);
HSSFCell CountCell = CountRow.getCell((short) 3);
String CountValue = CountCell.getStringCellValue();
if (CountValue.equals(""))
CountValue = "0";
String Count =
Integer.toString(Integer.parseInt(CountValue) + 1);
CountCell.setCellValue(Count); //更新数据
FileOutputStream fOut = new FileOutputStream(outputFile);
workbook.write(fOut);
fOut.close();
break;
}
}
}
问题:有时候会出现getRow或者getCell出现NULL,但是excel文件打开正常,而且如果在出现NULL的那一行或者那个cell中手动输入空格,保存后再次执行就没有问题,这个究竟是什么原因,怎么解决。poi版本是jakarta-poi-1.10.0-dev-20030222.jar
getRow() 这个函数是 0-based
同理,因为你处理的行,全部单元格都未设置值,所以getRow返回null。
就是说,空格和null是不同的。poi并没有出错,是你理解的有些问题,呵呵。
0-based 是 getRow(i) 中的这个 i 是从 0 开始的-----------------------------------------------------
楼主怎么视而不见?~!
----row----
for (Iterator it = sheet.rowIterator(); it.hasNext();) {
HSSFRow row = (HSSFRow) it.next();----col------
for (Iterator cit = row.cellIterator(); cit.hasNext();) {
HSSFCell cell = (HSSFCell) cit.next();