刚刚涉及POI工具包,读写xls文件,但用的不是很明白。
想用他来向原有的user.xls文件中写入数据,但不破坏原有数据。可是总抛出空指针异常。希望达人帮我看看怎么回事儿。
--------------------------------------------------------------------------------------------
package utils; import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell; import java.io.FileInputStream;
import java.io.FileOutputStream; public class TestXlsFile {
public static String outputFile = "user.xls"; public static void main(String argv[]) { try {
// 创建新的Excel 工作簿 HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(
"user.xls"));
System.out.println("ok 1");
HSSFSheet sheet = workbook.getSheet("sheet1");
System.out.println("ok 2");
HSSFRow row = sheet.getRow((short) 0);
System.out.println("ok 3");
// 在索引0的位置创建单元格(左上端)
HSSFCell cell = row.getCell((short) 0);
// 定义单元格为字符串类型
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
// 在单元格中输入一些内容
HSSFRichTextString content = new HSSFRichTextString("增加值");
cell.setCellValue(content);
// 新建一输出文件流
FileOutputStream fOut = new FileOutputStream(outputFile);
// 把相应的Excel 工作簿存盘
workbook.write(fOut);
fOut.flush();
// 操作结束,关闭文件
fOut.close();
System.out.println("文件生成..."); } catch (Exception e) {
System.out.println("已运行 xlCreate() : " + e);
}
}
}
-----------------------------------------------------------------------------------------------
代码输出:
ok 1
ok 2
已运行 xlCreate() : java.lang.NullPointerException
想用他来向原有的user.xls文件中写入数据,但不破坏原有数据。可是总抛出空指针异常。希望达人帮我看看怎么回事儿。
--------------------------------------------------------------------------------------------
package utils; import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell; import java.io.FileInputStream;
import java.io.FileOutputStream; public class TestXlsFile {
public static String outputFile = "user.xls"; public static void main(String argv[]) { try {
// 创建新的Excel 工作簿 HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(
"user.xls"));
System.out.println("ok 1");
HSSFSheet sheet = workbook.getSheet("sheet1");
System.out.println("ok 2");
HSSFRow row = sheet.getRow((short) 0);
System.out.println("ok 3");
// 在索引0的位置创建单元格(左上端)
HSSFCell cell = row.getCell((short) 0);
// 定义单元格为字符串类型
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
// 在单元格中输入一些内容
HSSFRichTextString content = new HSSFRichTextString("增加值");
cell.setCellValue(content);
// 新建一输出文件流
FileOutputStream fOut = new FileOutputStream(outputFile);
// 把相应的Excel 工作簿存盘
workbook.write(fOut);
fOut.flush();
// 操作结束,关闭文件
fOut.close();
System.out.println("文件生成..."); } catch (Exception e) {
System.out.println("已运行 xlCreate() : " + e);
}
}
}
-----------------------------------------------------------------------------------------------
代码输出:
ok 1
ok 2
已运行 xlCreate() : java.lang.NullPointerException
HSSFRow row = sheet.getRow((short) 0);
HSSFCell cell = row.getCell((short) 0);
--------------------------------------
get改为create
/*
* ExcelReader.java
*
* Created on 2008年3月24日, 下午1:38
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/package ExcelOption;import java.io.*;
import java.util.Date;
import java.util.Vector;
import org.apache.poi.hssf.usermodel.*;
/**
*
* @author heavens
*/
public class ExcelReader
{
static java.text.DecimalFormat myformat = new java.text.DecimalFormat("#0.00");
/** Creates a new instance of ExcelReader */
public static Vector ExcelReader(String inputfile)
{
Vector myRow = new Vector();
Vector myCol = new Vector();
try
{
InputStream is = new FileInputStream(inputfile);
//创建对Excel工作簿文件的引用
HSSFWorkbook workbook = new HSSFWorkbook(is);
//创建对工作表的引用
//HSSFSheet sheet = workbook.getSheet("Sheet1");
HSSFSheet sheet = workbook.getSheetAt(0);
//该表的行数
int rowcount = sheet.getLastRowNum();
for(int i=0;i<rowcount;i++)
{
HSSFRow row = sheet.getRow(i);
//第i行的列数
int colcount = row.getLastCellNum();
for(int j=0;j<colcount;j++)
{
HSSFCell cell = row.getCell((short)j);
String value = getCellFormatValue(cell);
//输出单元内容,cell.getStringCellValue()就是取所在单元的值
myCol.addElement(value);
}
myRow.addElement(myCol.toArray());
myCol.removeAllElements();
}
is.close();
}
catch(IOException e)
{
e.printStackTrace();
}
return myRow;
}
private static String getCellFormatValue(HSSFCell cell)
{
String cellvalue = "";
if (cell != null)
{
// 判断当前Cell的Type
switch (cell.getCellType())
{
// 如果当前Cell的Type为NUMERIC
case HSSFCell.CELL_TYPE_NUMERIC:
case HSSFCell.CELL_TYPE_FORMULA:
{
// 判断当前的cell是否为Date
if (HSSFDateUtil.isCellDateFormatted(cell))
{
// 如果是Date类型则,取得该Cell的Date值
Date date = cell.getDateCellValue();
// 把Date转换成本地格式的字符串
cellvalue = cell.getDateCellValue().toLocaleString();
}
// 如果是纯数字
else
{
// 取得当前Cell的数值
double num = new Double((double)cell.getNumericCellValue());
cellvalue = String.valueOf(myformat.format(num));
}
break;
}
// 如果当前Cell的Type为STRIN
case HSSFCell.CELL_TYPE_STRING:
// 取得当前的Cell字符串
cellvalue = cell.getStringCellValue().replaceAll("'", "''");
break;
// 默认的Cell值
default:
cellvalue = " ";
}
}
else
{
cellvalue = "";
}
return cellvalue;
}
}
是我放置user.xls文件的路径不对。
程序没有问题。 不过看了你的程序,真让我学到不少,再次十分感谢哈!
cellvalue = cell.getStringCellValue().replaceAll("'", "''");
这句话用replaceAll(),是什么意思?
为什么要把单引替换成双单引呢?有什麽用啊?