java中读取文件 第一行为空 提示空指针异常 怎么解决?
/**Tochangethislicenseheader,chooseLicenseHeadersinProjectProperties.*Tochangethistem...
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package poidemo1;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
/**
*
* @author Administrator
*/
public class PoiDemo5 {
public static void main(String[] args) throws IOException {
InputStream inputStream=new FileInputStream("f:/设置字体.xls");
POIFSFileSystem fSFileSystem=new POIFSFileSystem(inputStream);
Workbook wb=new HSSFWorkbook(fSFileSystem);
Sheet sheet= wb.getSheetAt(0);//获取一个sheet页
Row row=sheet.getRow(0);//获取第一行
Cell cell=row.getCell(0);//获取单元格
if (cell==null) {
cell=row.createCell(3);
}
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue("测试单元格");
FileOutputStream fileOutputStream=new FileOutputStream("f:/设置字体.xls");
wb.write(fileOutputStream);
fileOutputStream.close();
}
} 展开
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package poidemo1;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
/**
*
* @author Administrator
*/
public class PoiDemo5 {
public static void main(String[] args) throws IOException {
InputStream inputStream=new FileInputStream("f:/设置字体.xls");
POIFSFileSystem fSFileSystem=new POIFSFileSystem(inputStream);
Workbook wb=new HSSFWorkbook(fSFileSystem);
Sheet sheet= wb.getSheetAt(0);//获取一个sheet页
Row row=sheet.getRow(0);//获取第一行
Cell cell=row.getCell(0);//获取单元格
if (cell==null) {
cell=row.createCell(3);
}
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue("测试单元格");
FileOutputStream fileOutputStream=new FileOutputStream("f:/设置字体.xls");
wb.write(fileOutputStream);
fileOutputStream.close();
}
} 展开
2个回答
展开全部
package poidemo1;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
/**
*
* @author Administrator
*/
public class PoiDemo5 {
public static void main(String[] args) throws IOException {
InputStream inputStream=new FileInputStream(new File("f:/设置字体.xls"));
POIFSFileSystem fSFileSystem=new POIFSFileSystem(inputStream);
Workbook wb=new HSSFWorkbook(fSFileSystem);
Sheet sheet= wb.getSheetAt(0);//获取一个sheet页
Row row=sheet.getRow(0);//获取第一行
Cell cell=row.getCell(0);//获取单元格
if (cell==null) {
cell=row.createCell(3);
}
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue("测试单元格");
FileOutputStream fileOutputStream=new FileOutputStream(new File("f:/设置字体.xls"));
wb.write(fileOutputStream);
fileOutputStream.close();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询