import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;public class ExcelRead {
private HSSFWorkbook workbook;
private POIFSFileSystem fs;
private File file;
private HSSFSheet sheet;
private HSSFRow row;
private HSSFCell cell;
public void read() throws IOException{
String path = "D:\\08年度变电站电能表数及电量.xls";
file = new File(path);
fs = new POIFSFileSystem(new FileInputStream(file));//可能是LZ粘贴时漏掉了,至少应将Excel文档读到HSSFWorkbook 对象中;
workbook = new HSSFWorkbook(fs);
String aa = workbook.getSheetName(0);
sheet = workbook.getSheetAt(0);// 估计是错在这里,如果只有一个Sheet页的话,起始索引应为0;
System.out.println(aa); if(sheet==null){
System.out.println("null");
}
int i = sheet.getLastRowNum();
for(int k=0;k <i;k++){
row = sheet.getRow(k);
for(int j = 0; j < row.getLastCellNum(); j++){
cell = row.getCell((short)j);
String cellValue = "";
if (cell != null) {
if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {// 最好做一下转换,否则格式不一致时会抛转换异常
cellValue = cell.getStringCellValue();
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
double d = cell.getNumericCellValue();
if ((long) d < d) {
cellValue = String.valueOf(cell.getNumericCellValue());
} else {
cellValue = String.valueOf((long) cell.getNumericCellValue());
}
} else {
try {
cellValue = cell.getStringCellValue();
} catch (Exception e) {
cellValue = "";
}
}
}
System.out.println(cellValue);
}
}
} public static void main(String []args){
ExcelRead read = new ExcelRead();
try{
read.read();
}catch(IOException e){
e.printStackTrace();
}
}
}
import java.io.FileInputStream;
import java.io.IOException;import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;public class ExcelRead {
private HSSFWorkbook workbook;
private POIFSFileSystem fs;
private File file;
private HSSFSheet sheet;
private HSSFRow row;
private HSSFCell cell;
public void read() throws IOException{
String path = "D:\\08年度变电站电能表数及电量.xls";
file = new File(path);
fs = new POIFSFileSystem(new FileInputStream(file));//可能是LZ粘贴时漏掉了,至少应将Excel文档读到HSSFWorkbook 对象中;
workbook = new HSSFWorkbook(fs);
String aa = workbook.getSheetName(0);
sheet = workbook.getSheetAt(0);// 估计是错在这里,如果只有一个Sheet页的话,起始索引应为0;
System.out.println(aa); if(sheet==null){
System.out.println("null");
}
int i = sheet.getLastRowNum();
for(int k=0;k <i;k++){
row = sheet.getRow(k);
for(int j = 0; j < row.getLastCellNum(); j++){
cell = row.getCell((short)j);
String cellValue = "";
if (cell != null) {
if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {// 最好做一下转换,否则格式不一致时会抛转换异常
cellValue = cell.getStringCellValue();
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
double d = cell.getNumericCellValue();
if ((long) d < d) {
cellValue = String.valueOf(cell.getNumericCellValue());
} else {
cellValue = String.valueOf((long) cell.getNumericCellValue());
}
} else {
try {
cellValue = cell.getStringCellValue();
} catch (Exception e) {
cellValue = "";
}
}
}
System.out.println(cellValue);
}
}
} public static void main(String []args){
ExcelRead read = new ExcelRead();
try{
read.read();
}catch(IOException e){
e.printStackTrace();
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货