3个回答
展开全部
(POI3.9和Excel2007)
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class TestExcel {
private static final Logger LOG = Logger.getLogger(TestExcel.class.getName());
public static void main(String[] args) {
//excel file location
String filePath = "C:\\A.xls";
HSSFWorkbook book;
HSSFSheet sheet;
try {
book = new HSSFWorkbook(new FileInputStream(filePath));
sheet = book.getSheetAt(0);
//row is 4 and cell is 2.
for (int row = 0; row < 4; row++) {
for (int cell = 0; cell < 2; cell++) {
HSSFCell hssfCell = sheet.getRow(row).getCell(cell);
if(hssfCell != null){
try{
double value = hssfCell.getNumericCellValue();
System.out.println("[row" + (row+1) + ",cell" + (cell+1) + "]:" + value);
}
catch (IllegalStateException e) {
LOG.warning("[row" + (row+1) + ",cell" + (cell+1) + "]:not a number, and the value is:" + hssfCell.toString());
}
}
else{
LOG.warning("[row" + (row+1) + ",cell" + (cell+1) + "]:null");
}
}
}
}
catch (FileNotFoundException e) {
LOG.info("can't find the file:" + filePath);
}
catch (IOException e) {
LOG.info("IOException:" + e.getMessage());
}
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class TestExcel {
private static final Logger LOG = Logger.getLogger(TestExcel.class.getName());
public static void main(String[] args) {
//excel file location
String filePath = "C:\\A.xls";
HSSFWorkbook book;
HSSFSheet sheet;
try {
book = new HSSFWorkbook(new FileInputStream(filePath));
sheet = book.getSheetAt(0);
//row is 4 and cell is 2.
for (int row = 0; row < 4; row++) {
for (int cell = 0; cell < 2; cell++) {
HSSFCell hssfCell = sheet.getRow(row).getCell(cell);
if(hssfCell != null){
try{
double value = hssfCell.getNumericCellValue();
System.out.println("[row" + (row+1) + ",cell" + (cell+1) + "]:" + value);
}
catch (IllegalStateException e) {
LOG.warning("[row" + (row+1) + ",cell" + (cell+1) + "]:not a number, and the value is:" + hssfCell.toString());
}
}
else{
LOG.warning("[row" + (row+1) + ",cell" + (cell+1) + "]:null");
}
}
}
}
catch (FileNotFoundException e) {
LOG.info("can't find the file:" + filePath);
}
catch (IOException e) {
LOG.info("IOException:" + e.getMessage());
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询