如何用java读取excel中的数据?下面的例子不能读出自己写的excel数据
publicclassPOIreadExcel2003{publicPOIreadExcel2003(){try{//读取要导出的excel文件POIFSFileSyst...
public class POIreadExcel2003 {
public POIreadExcel2003() {
try {
// 读取要导出的excel文件
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(
"e://workbook2.xls"));
HSSFWorkbook wb = new HSSFWorkbook(fs);
ExcelExtractor extractor = new ExcelExtractor(wb);
extractor.setFormulasNotResults(false);
extractor.setIncludeSheetNames(false);
// 工作表的序号
Sheet sheet1 = (Sheet) wb.getSheetAt(0);
// 开始遍历整个工作表
Iterator<Row> it = sheet1.rowIterator();
while (it.hasNext()) {
Row row = (Row) it.next();
Iterator<Cell> cit = row.cellIterator();
while (cit.hasNext()) {
// 输出数据
if (cit.next().getCellType() == Cell.CELL_TYPE_FORMULA) {
Cell cell = cit.next();
FormulaEvaluator evaluator = wb.getCreationHelper()
.createFormulaEvaluator();
// 把公式行的数据计算出来
CellValue cellValue = evaluator.evaluate(cell);
System.out.println(cellValue.toString() + ",");
} else {
System.out.print(cit.next().toString() + ",");
}
}
}
} catch (Exception e) {
System.out.println("阅读失败...");
}
}
/**
* @param args
*/
public static void main(String[] args) {
new POIreadExcel2003();
}
} 展开
public POIreadExcel2003() {
try {
// 读取要导出的excel文件
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(
"e://workbook2.xls"));
HSSFWorkbook wb = new HSSFWorkbook(fs);
ExcelExtractor extractor = new ExcelExtractor(wb);
extractor.setFormulasNotResults(false);
extractor.setIncludeSheetNames(false);
// 工作表的序号
Sheet sheet1 = (Sheet) wb.getSheetAt(0);
// 开始遍历整个工作表
Iterator<Row> it = sheet1.rowIterator();
while (it.hasNext()) {
Row row = (Row) it.next();
Iterator<Cell> cit = row.cellIterator();
while (cit.hasNext()) {
// 输出数据
if (cit.next().getCellType() == Cell.CELL_TYPE_FORMULA) {
Cell cell = cit.next();
FormulaEvaluator evaluator = wb.getCreationHelper()
.createFormulaEvaluator();
// 把公式行的数据计算出来
CellValue cellValue = evaluator.evaluate(cell);
System.out.println(cellValue.toString() + ",");
} else {
System.out.print(cit.next().toString() + ",");
}
}
}
} catch (Exception e) {
System.out.println("阅读失败...");
}
}
/**
* @param args
*/
public static void main(String[] args) {
new POIreadExcel2003();
}
} 展开
2个回答
展开全部
import java.io.FileInputStream;
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;
public class ExcelTo {
public static void main(String[] args) {
String fileToBeRead = "g:\\Salary\\Salary.xls";
try {
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(fileToBeRead)); // 创建对Excel工作簿文件的引用
HSSFSheet sheet = workbook.getSheet("员工表"); // 创建对工作表的引用
int rows = sheet.getPhysicalNumberOfRows();//获取表格的
for (int r = 0; r < rows; r++) { //循环遍历表格的行
String value = "";
HSSFRow row = sheet.getRow(r); //获取单元格中指定的行对象
if (row != null) {
int cells = row.getPhysicalNumberOfCells();//获取单元格中指定列对象
for (short c = 0; c < cells; c++) { //循环遍历单元格中的列
HSSFCell cell = row.getCell((short) c); //获取指定单元格中的列
if (cell != null) {
if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) { //判断单元格的值是否为字符串类型
value += cell.getStringCellValue() + ",";
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) { //判断单元格的值是否为数字类型
value += cell.getNumericCellValue() + ",";
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) { //判断单元格的值是否为布尔类型
value += cell.getStringCellValue() + ",";
}
}
}
}
String[] str = value.split(",");
System.out.println(value);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
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;
public class ExcelTo {
public static void main(String[] args) {
String fileToBeRead = "g:\\Salary\\Salary.xls";
try {
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(fileToBeRead)); // 创建对Excel工作簿文件的引用
HSSFSheet sheet = workbook.getSheet("员工表"); // 创建对工作表的引用
int rows = sheet.getPhysicalNumberOfRows();//获取表格的
for (int r = 0; r < rows; r++) { //循环遍历表格的行
String value = "";
HSSFRow row = sheet.getRow(r); //获取单元格中指定的行对象
if (row != null) {
int cells = row.getPhysicalNumberOfCells();//获取单元格中指定列对象
for (short c = 0; c < cells; c++) { //循环遍历单元格中的列
HSSFCell cell = row.getCell((short) c); //获取指定单元格中的列
if (cell != null) {
if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) { //判断单元格的值是否为字符串类型
value += cell.getStringCellValue() + ",";
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) { //判断单元格的值是否为数字类型
value += cell.getNumericCellValue() + ",";
} else if (cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) { //判断单元格的值是否为布尔类型
value += cell.getStringCellValue() + ",";
}
}
}
}
String[] str = value.split(",");
System.out.println(value);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询