我是java新手,在将动态数据插入excel文件时遇到问题。下面是代码。如果我删除我的excel文件并重新运行我的程序。然后,它将创建一个新文件并插入以下数据。(hello,goodbye,true,date)。在第一次运行期间,程序仍然可以插入以下数据,但当我执行下一次运行时,数据无法存储到下一行。下面是检查文件是否存在的代码。我希望我能有人来帮助我,因为我挣扎了几天这个代码。
if (file.exists()) {
try{
fout = new FileOutputStream(fileName, true);
fin = new FileInputStream(fileName);
lPOIfs = new POIFSFileSystem(fin);
workbook = new HSSFWorkbook(lPOIfs);
worksheet = workbook.getSheet("POI Worksheet");
for (int i=0; i<workbook.getNumberOfSheets(); i++) {
System.out.println( workbook.getSheetName(i) );
}
HSSFSheet sheet = workbook.getSheetAt(0);
last = sheet.getLastRowNum();
}catch (IOException e) {
e.printStackTrace();
}catch (NullPointerException e){
e.printStackTrace();
}
} else {
//create new file
try{
fout = new FileOutputStream(file);
}catch (IOException e) {
e.printStackTrace();
}
workbook = new HSSFWorkbook();
worksheet = workbook.createSheet("POI Worksheet");
}
这是检查行中最后一个数字的代码:
if(last != 0){
last = worksheet.getLastRowNum()+1;
}else{
last = worksheet.getLastRowNum();
}
这是write函数的完整代码:
public void writeExcel(){
String fileName = "C:\\Users\\blslyeoh\\Documents\\NetBeansProjects\\JavaApplication1\\poi-test.xls";
try {
int last=0;
File file = new File(fileName);
FileInputStream fin = null;
HSSFWorkbook workbook = null;
HSSFSheet worksheet = null;
FileOutputStream fout = null;
POIFSFileSystem lPOIfs = null;
if (file.exists()) {
try{
fout = new FileOutputStream(fileName, true);
fin = new FileInputStream(fileName);
lPOIfs = new POIFSFileSystem(fin);
workbook = new HSSFWorkbook(lPOIfs);
worksheet = workbook.getSheet("POI Worksheet");
for (int i=0; i<workbook.getNumberOfSheets(); i++) {
System.out.println( workbook.getSheetName(i) );
}
HSSFSheet sheet = workbook.getSheetAt(0);
last = sheet.getLastRowNum();
}catch (IOException e) {
e.printStackTrace();
}catch (NullPointerException e){
e.printStackTrace();
}
} else {
//create new file
try{
fout = new FileOutputStream(file);
}catch (IOException e) {
e.printStackTrace();
}
workbook = new HSSFWorkbook();
worksheet = workbook.createSheet("POI Worksheet");
}
// index from 0,0... cell A1 is cell(0,0)
if(last != 0){
last = worksheet.getLastRowNum()+1;
}else{
last = worksheet.getLastRowNum();
}
HSSFRow row = worksheet.createRow(last);
HSSFCell cellA1 = row.createCell((short)0);
cellA1.setCellValue("hello");
HSSFCellStyle cellStyle = workbook.createCellStyle();
//cellStyle.setFillForegroundColor(HSSFColor.GOLD.index);
//cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
//cellA1.setCellStyle(cellStyle);
HSSFCell cellB1 = row.createCell((short) 1);
cellB1.setCellValue("goodbye");
//cellStyle = workbook.createCellStyle();
//cellStyle.setFillForegroundColor(HSSFColor.LIGHT_CORNFLOWER_BLUE.index);
//cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
//cellB1.setCellStyle(cellStyle);
HSSFCell cellC1 = row.createCell((short) 2);
cellC1.setCellValue(true);
HSSFCell cellD1 = row.createCell((short) 3);
cellD1.setCellValue(new Date());
cellStyle = workbook.createCellStyle();
cellStyle.setDataFormat(HSSFDataFormat
.getBuiltinFormat("m/d/yy h:mm"));
cellD1.setCellStyle(cellStyle);
workbook.write(fout);
fout.flush();
fout.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
1条答案
按热度按时间taor4pac1#
先读取excel文件!
如果你顺利通过了
worksheet=workbook.getsheet(“poi工作表”);
然后关闭文件输入流。
如果你找不到工作表,那就创建一个
工作簿=新建HSSF工作簿();
worksheet=workbook.createsheet(“poi工作表”);
你有一个新的或现有的低音片。
附加行并将其写入文件,然后通过关闭文件输出流
fout=新文件输出流(文件);
练习册。写作(fout);
fout.close();
我希望这能帮助你。祝你今天愉快:-)