Java解析excel 各位大侠给点意见 用poi包还是用jxl包 ?麻烦谁拿有jxl和log4j的jar包给我发一个 [email protected] 谢谢各位啦!!!!!!!!!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://poi.apache.org/ http://jexcelapi.sourceforge.net/ 我家里的电脑有一个jxl的,过几天回家顺便拷贝了传你。不过,网上有很多啊。下载后把.jar放在"C:\Program Files\Java\jdk1.6.0_10\jre\lib\ext”,这是我放的地方,楼主放在"JAVAHOME\jre\lib\ext",JAVAHOME指JDK的路径 poi3.5开始对excel2007支持,jxl不行,如果只对03格式的话,jxl的api比较简单容易上手 现在要实现用Java解析内容为任意格式的excel的表头 有谁做过类似的没?给点意见 现在用2007的很多了,还是考虑POI吧 给个例子,用的是POI3.0import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.util.ArrayList;import java.util.List;import org.apache.log4j.Logger;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;/*** EXCEL文档解析工具类* 该工具能将EXCEL文档中的表解析为由JAVA基础类构成的数据集合* 整个EXCEL表由多个行组成.每行用一个LIST表示.* EXCEL中的行由一个LIST表示,各列的数据索引从0开始一一对齐存放在这个LIST中;* 多个行构成整个表,由一个LIST存放多个行.* ******************************************** com.trumptech.common.fileParser.excel* 2007-6-15* 16:20:38* author linfan********************************************/public class ExcelSheetParser {private Logger logger= Logger.getLogger(ExcelSheetParser.class);private HSSFWorkbook workbook ;public ExcelSheetParser(File excelFile) throws FileNotFoundException, IOException{ workbook = new HSSFWorkbook(new FileInputStream(excelFile));}/*** 获得表中的数据* @param sheetNumber 表格索引(EXCEL 是多表文档,所以需要输入表索引号)* @return 由LIST构成的行和表* @throws FileNotFoundException* @throws IOException*/public List<List> getDatasInSheet(int sheetNumber) throws FileNotFoundException, IOException{ List<List> result = new ArrayList<List>(); //获得指定的表 HSSFSheet sheet = workbook.getSheetAt(sheetNumber); //获得数据总行数 int rowCount = sheet.getLastRowNum(); logger.info("found excel rows count: " + rowCount); if (rowCount < 1) { return result; } //逐行读取数据 for (int rowIndex = 0; rowIndex <= rowCount; rowIndex++) { //获得行对象 HSSFRow row = sheet.getRow(rowIndex); if (row != null) { List<Object> rowData = new ArrayList<Object>(); //获得本行中单元格的个数 int columnCount = row.getLastCellNum(); //获得本行中各单元格中的数据 for (short columnIndex = 0; columnIndex < columnCount; columnIndex++) { HSSFCell cell = row.getCell(columnIndex); //获得指定单元格中数据 Object cellStr = this.getCellString(cell); rowData.add(cellStr); } result.add(rowData); } } return result;}/*** 获得单元格中的内容* @param cell* @return*/protected Object getCellString(HSSFCell cell){ Object result = null; if (cell != null) { int cellType = cell.getCellType(); switch(cellType){ case HSSFCell.CELL_TYPE_STRING : result = cell.getRichStringCellValue().getString(); break; case HSSFCell.CELL_TYPE_NUMERIC: result=cell.getNumericCellValue(); break; case HSSFCell.CELL_TYPE_FORMULA: result = cell.getNumericCellValue(); break; case HSSFCell.CELL_TYPE_ERROR: result=null; break; case HSSFCell.CELL_TYPE_BOOLEAN: result=cell.getBooleanCellValue(); break; case HSSFCell.CELL_TYPE_BLANK: result=null; break; } } return result;}public static void main(String[] args) throws Exception { File file = new File("D:\\tmp\\test.xls"); ExcelSheetParser parser = new ExcelSheetParser(file); List<List> datas = parser.getDatasInSheet(0); for(int i=0;i<datas.size();i++){//显示数据 List row = datas.get(i); for(short n=0;n<row.size() ;n++){ Object value = row.get(n); String data = String.valueOf(value); System.out.print(data +"\t"); } System.out.println(); }}} java文本排版问题 很简单的问题java 数组 新手咨询---在线等待 weblogic tuxedo 开发结构 求解超实用小程序??? 关于JAVA的API 真的希望大家来看看,我不理解 代码疑问 关于 STATIC 我有点迷,近来指点一下? java poi word to html 刚学java,想知道数组相乘后的结果矩阵的维数怎么自动确定 两个极其相似的方法怎么结构写比较好
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;import org.apache.log4j.Logger;
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;/**
* EXCEL文档解析工具类
* 该工具能将EXCEL文档中的表解析为由JAVA基础类构成的数据集合
* 整个EXCEL表由多个行组成.每行用一个LIST表示.
* EXCEL中的行由一个LIST表示,各列的数据索引从0开始一一对齐存放在这个LIST中;
* 多个行构成整个表,由一个LIST存放多个行.
*
*******************************************
* com.trumptech.common.fileParser.excel
* 2007-6-15
* 16:20:38
* author linfan
*******************************************
*/
public class ExcelSheetParser {private Logger logger= Logger.getLogger(ExcelSheetParser.class);
private HSSFWorkbook workbook ;public ExcelSheetParser(File excelFile) throws FileNotFoundException, IOException{ workbook = new HSSFWorkbook(new FileInputStream(excelFile));
}
/**
* 获得表中的数据
* @param sheetNumber 表格索引(EXCEL 是多表文档,所以需要输入表索引号)
* @return 由LIST构成的行和表
* @throws FileNotFoundException
* @throws IOException
*/
public List<List> getDatasInSheet(int sheetNumber) throws FileNotFoundException, IOException{
List<List> result = new ArrayList<List>();
//获得指定的表
HSSFSheet sheet = workbook.getSheetAt(sheetNumber);
//获得数据总行数
int rowCount = sheet.getLastRowNum();
logger.info("found excel rows count: " + rowCount);
if (rowCount < 1) {
return result;
}
//逐行读取数据
for (int rowIndex = 0; rowIndex <= rowCount; rowIndex++) {
//获得行对象
HSSFRow row = sheet.getRow(rowIndex);
if (row != null) {
List<Object> rowData = new ArrayList<Object>();
//获得本行中单元格的个数
int columnCount = row.getLastCellNum();
//获得本行中各单元格中的数据
for (short columnIndex = 0; columnIndex < columnCount; columnIndex++) {
HSSFCell cell = row.getCell(columnIndex);
//获得指定单元格中数据
Object cellStr = this.getCellString(cell);
rowData.add(cellStr);
}
result.add(rowData);
}
}
return result;
}/**
* 获得单元格中的内容
* @param cell
* @return
*/
protected Object getCellString(HSSFCell cell){
Object result = null;
if (cell != null) { int cellType = cell.getCellType();
switch(cellType){
case HSSFCell.CELL_TYPE_STRING :
result = cell.getRichStringCellValue().getString();
break;
case HSSFCell.CELL_TYPE_NUMERIC:
result=cell.getNumericCellValue();
break;
case HSSFCell.CELL_TYPE_FORMULA:
result = cell.getNumericCellValue();
break;
case HSSFCell.CELL_TYPE_ERROR:
result=null;
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
result=cell.getBooleanCellValue();
break;
case HSSFCell.CELL_TYPE_BLANK:
result=null;
break;
}
}
return result;
}public static void main(String[] args) throws Exception {
File file = new File("D:\\tmp\\test.xls");
ExcelSheetParser parser = new ExcelSheetParser(file);
List<List> datas = parser.getDatasInSheet(0); for(int i=0;i<datas.size();i++){//显示数据
List row = datas.get(i);
for(short n=0;n<row.size() ;n++){
Object value = row.get(n);
String data = String.valueOf(value);
System.out.print(data +"\t");
}
System.out.println();
}
}
}