当前位置:首页 » 编程语言 » java解析excel

java解析excel

发布时间: 2022-07-10 15:28:18

‘壹’ java 怎么对选中的excel文件进行解析 求详细实例代码

import Java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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;
public class ReadExcel {
public static void readExcel(File file){
try {
InputStream inputStream = new FileInputStream(file);
String fileName = file.getName();
Workbook wb = null;
// poi-3.9.jar 只可以读取2007以下的版本,后缀为:xsl
wb = new HSSFWorkbook(inputStream);//解析xls格式

Sheet sheet = wb.getSheetAt(0);//第一个工作表 ,第二个则为1,以此类推...

int firstRowIndex = sheet.getFirstRowNum();
int lastRowIndex = sheet.getLastRowNum();
for(int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex ++){
Row row = sheet.getRow(rIndex);
if(row != null){
int firstCellIndex = row.getFirstCellNum();
// int lastCellIndex = row.getLastCellNum();
//此处参数cIndex决定可以取到excel的列数。
for(int cIndex = firstCellIndex; cIndex < 3; cIndex ++){
Cell cell = row.getCell(cIndex);
String value = "";
if(cell != null){
value = cell.toString();
System.out.print(value+"\t");
}
}
System.out.println();
}
}
} catch (FileNotFoundException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
public static void main(String[] args) {
File file = new File("D:/test.xls");
readExcel(file);
}
}

‘贰’ Java对Excel解析(求助)

这篇blog主要是讲述java中poi读取excel,而excel的版本包括:2003-2007和2010两个版本, 即excel的后缀名为:xls和xlsx。

读取excel和MySQL相关: java的poi技术读取Excel数据到MySQL

代码如下

/**
*
*/
packagecom.b510.common;

/**
*@authorHongten
*@created2014-5-21
*/
publicclassCommon{

publicstaticfinalStringOFFICE_EXCEL_2003_POSTFIX="xls";
publicstaticfinalStringOFFICE_EXCEL_2010_POSTFIX="xlsx";

publicstaticfinalStringEMPTY="";
publicstaticfinalStringPOINT=".";
publicstaticfinalStringLIB_PATH="lib";
_INFO_XLS_PATH=LIB_PATH+"/student_info"+POINT+OFFICE_EXCEL_2003_POSTFIX;
_INFO_XLSX_PATH=LIB_PATH+"/student_info"+POINT+OFFICE_EXCEL_2010_POSTFIX;
publicstaticfinalStringNOT_EXCEL_FILE=":NottheExcelfile!";
="Processing...";

}

/**
*
*/
packagecom.b510.excel;

importjava.io.FileInputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.util.ArrayList;
importjava.util.List;

importorg.apache.poi.hssf.usermodel.HSSFCell;
importorg.apache.poi.hssf.usermodel.HSSFRow;
importorg.apache.poi.hssf.usermodel.HSSFSheet;
importorg.apache.poi.hssf.usermodel.HSSFWorkbook;
importorg.apache.poi.xssf.usermodel.XSSFCell;
importorg.apache.poi.xssf.usermodel.XSSFRow;
importorg.apache.poi.xssf.usermodel.XSSFSheet;
importorg.apache.poi.xssf.usermodel.XSSFWorkbook;

importcom.b510.common.Common;
importcom.b510.excel.util.Util;
importcom.b510.excel.vo.Student;

/**
*@authorHongten
*@created2014-5-20
*/
publicclassReadExcel{

/**
*readtheExcelfile
*@
*@return
*@throwsIOException
*/
publicList<Student>readExcel(Stringpath)throwsIOException{
if(path==null||Common.EMPTY.equals(path)){
returnnull;
}else{
Stringpostfix=Util.getPostfix(path);
if(!Common.EMPTY.equals(postfix)){
if(Common.OFFICE_EXCEL_2003_POSTFIX.equals(postfix)){
returnreadXls(path);
}elseif(Common.OFFICE_EXCEL_2010_POSTFIX.equals(postfix)){
returnreadXlsx(path);
}
}else{
System.out.println(path+Common.NOT_EXCEL_FILE);
}
}
returnnull;
}

/**
*ReadtheExcel2010
*@
*@return
*@throwsIOException
*/
publicList<Student>readXlsx(Stringpath)throwsIOException{
System.out.println(Common.PROCESSING+path);
InputStreamis=newFileInputStream(path);
XSSFWorkbookxssfWorkbook=newXSSFWorkbook(is);
Studentstudent=null;
List<Student>list=newArrayList<Student>();
//ReadtheSheet
for(intnumSheet=0;numSheet<xssfWorkbook.getNumberOfSheets();numSheet++){
XSSFSheetxssfSheet=xssfWorkbook.getSheetAt(numSheet);
if(xssfSheet==null){
continue;
}
//ReadtheRow
for(introwNum=1;rowNum<=xssfSheet.getLastRowNum();rowNum++){
XSSFRowxssfRow=xssfSheet.getRow(rowNum);
if(xssfRow!=null){
student=newStudent();
XSSFCellno=xssfRow.getCell(0);
XSSFCellname=xssfRow.getCell(1);
XSSFCellage=xssfRow.getCell(2);
XSSFCellscore=xssfRow.getCell(3);
student.setNo(getValue(no));
student.setName(getValue(name));
student.setAge(getValue(age));
student.setScore(Float.valueOf(getValue(score)));
list.add(student);
}
}
}
returnlist;
}

/**
*ReadtheExcel2003-2007
*@parampaththepathoftheExcel
*@return
*@throwsIOException
*/
publicList<Student>readXls(Stringpath)throwsIOException{
System.out.println(Common.PROCESSING+path);
InputStreamis=newFileInputStream(path);
HSSFWorkbookhssfWorkbook=newHSSFWorkbook(is);
Studentstudent=null;
List<Student>list=newArrayList<Student>();
//ReadtheSheet
for(intnumSheet=0;numSheet<hssfWorkbook.getNumberOfSheets();numSheet++){
HSSFSheethssfSheet=hssfWorkbook.getSheetAt(numSheet);
if(hssfSheet==null){
continue;
}
//ReadtheRow
for(introwNum=1;rowNum<=hssfSheet.getLastRowNum();rowNum++){
HSSFRowhssfRow=hssfSheet.getRow(rowNum);
if(hssfRow!=null){
student=newStudent();
HSSFCellno=hssfRow.getCell(0);
HSSFCellname=hssfRow.getCell(1);
HSSFCellage=hssfRow.getCell(2);
HSSFCellscore=hssfRow.getCell(3);
student.setNo(getValue(no));
student.setName(getValue(name));
student.setAge(getValue(age));
student.setScore(Float.valueOf(getValue(score)));
list.add(student);
}
}
}
returnlist;
}

@SuppressWarnings("static-access")
privateStringgetValue(XSSFCellxssfRow){
if(xssfRow.getCellType()==xssfRow.CELL_TYPE_BOOLEAN){
returnString.valueOf(xssfRow.getBooleanCellValue());
}elseif(xssfRow.getCellType()==xssfRow.CELL_TYPE_NUMERIC){
returnString.valueOf(xssfRow.getNumericCellValue());
}else{
returnString.valueOf(xssfRow.getStringCellValue());
}
}

@SuppressWarnings("static-access")
privateStringgetValue(HSSFCellhssfCell){
if(hssfCell.getCellType()==hssfCell.CELL_TYPE_BOOLEAN){
returnString.valueOf(hssfCell.getBooleanCellValue());
}elseif(hssfCell.getCellType()==hssfCell.CELL_TYPE_NUMERIC){
returnString.valueOf(hssfCell.getNumericCellValue());
}else{
returnString.valueOf(hssfCell.getStringCellValue());
}
}
}

/**
*
*/
packagecom.b510.excel.client;

importjava.io.IOException;
importjava.util.List;

importcom.b510.common.Common;
importcom.b510.excel.ReadExcel;
importcom.b510.excel.vo.Student;

/**
*@authorHongten
*@created2014-5-21
*/
publicclassClient{

publicstaticvoidmain(String[]args)throwsIOException{
Stringexcel2003_2007=Common.STUDENT_INFO_XLS_PATH;
Stringexcel2010=Common.STUDENT_INFO_XLSX_PATH;
//readthe2003-2007excel
List<Student>list=newReadExcel().readExcel(excel2003_2007);
if(list!=null){
for(Studentstudent:list){
System.out.println("No.:"+student.getNo()+",name:"+student.getName()+",age:"+student.getAge()+",score:"+student.getScore());
}
}
System.out.println("======================================");
//readthe2010excel
List<Student>list1=newReadExcel().readExcel(excel2010);
if(list1!=null){
for(Studentstudent:list1){
System.out.println("No.:"+student.getNo()+",name:"+student.getName()+",age:"+student.getAge()+",score:"+student.getScore());
}
}
}
}

/**
*
*/
packagecom.b510.excel.util;

importcom.b510.common.Common;

/**
*@authorHongten
*@created2014-5-21
*/
publicclassUtil{

/**
*getpostfixofthepath
*@parampath
*@return
*/
publicstaticStringgetPostfix(Stringpath){
if(path==null||Common.EMPTY.equals(path.trim())){
returnCommon.EMPTY;
}
if(path.contains(Common.POINT)){
returnpath.substring(path.lastIndexOf(Common.POINT)+1,path.length());
}
returnCommon.EMPTY;
}
}

/**
*
*/
packagecom.b510.excel.vo;

/**
*Student
*
*@authorHongten
*@created2014-5-18
*/
publicclassStudent{
/**
*id
*/
privateIntegerid;
/**
*学号
*/
privateStringno;
/**
*姓名
*/
privateStringname;
/**
*学院
*/
privateStringage;
/**
*成绩
*/
privatefloatscore;

publicIntegergetId(){
returnid;
}

publicvoidsetId(Integerid){
this.id=id;
}

publicStringgetNo(){
returnno;
}

publicvoidsetNo(Stringno){
this.no=no;
}

publicStringgetName(){
returnname;
}

publicvoidsetName(Stringname){
this.name=name;
}

publicStringgetAge(){
returnage;
}

publicvoidsetAge(Stringage){
this.age=age;
}

publicfloatgetScore(){
returnscore;
}

publicvoidsetScore(floatscore){
this.score=score;
}

}

‘叁’ JAVA怎么解析excel表中的单元格是下拉框的所有值

添加依赖spire.xls.jar,用下面的代码

  1. import com.spire.xls.Workbook;

  2. import com.spire.xls.Worksheet;


  3. public class ReadCellContent {

  4. public static void main(String[] args) {

  5. //创建Workbook对象

  6. Workbook wb = new Workbook();

  7. //加载Excel文档

  8. wb.loadFromFile("G:\360MoveData\Users\Administrator\Desktop\test.xlsx");

  9. //获取第一个表格

  10. Worksheet worksheet = wb.getWorksheets().get(0);

  11. //获取指定单元格内下拉列表的值

  12. String[] values = worksheet.getCellRange(7,3).getDataValidation().getValues();

  13. for (int i = 0; i < values.length; i++) {

  14. System.out.println(values[i]);

  15. }

  16. }

  17. }

测试结果:

‘肆’ java使用poi解析或处理excel的时候,如何防止数字变成科学计数法的

思路为:为了防止数字变成科学计数法方式表示,在源文件以及java代码中都用文本的方式去生成和解析excel,具体如下:

1.生成Excel时,设置单元格格式为STRING,即:

//关键代码
HSSFCellcell=newHSSFCell();
cell.setCellType(HSSFCell.CELL_TYPE_STRING);

2.同理,解析的时候,首先要保证源excel文件中该单元格格式是文本类型的,然后在java代码里用STRING类型去解析:

//关键代码
Stringvalue=cell.getStringCellValue();

‘伍’ java如何读取整个excel文件的内容

工具:
参考代码及注释如下:
import Java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import org.apache.poi.hssf.usermodel.HSSFWorkbook; 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; public class ReadExcel { public static void readExcel(File file){ try { InputStream inputStream = new FileInputStream(file); String fileName = file.getName(); Workbook wb = null; // poi-3.9.jar 只可以读取2007以下的版本,后缀为:xsl wb = new HSSFWorkbook(inputStream);//解析xls格式 Sheet sheet = wb.getSheetAt(0);//第一个工作表 ,第二个则为1,以此类推... int firstRowIndex = sheet.getFirstRowNum(); int lastRowIndex = sheet.getLastRowNum(); for(int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex ++){ Row row = sheet.getRow(rIndex); if(row != null){ int firstCellIndex = row.getFirstCellNum(); // int lastCellIndex = row.getLastCellNum(); //此处参数cIndex决定可以取到excel的列数。 for(int cIndex = firstCellIndex; cIndex < 3; cIndex ++){ Cell cell = row.getCell(cIndex); String value = ""; if(cell != null){ value = cell.toString(); System.out.print(value+"\t"); } } System.out.println(); } } } catch (FileNotFoundException e) { // TODO 自动生成 catch 块 e.printStackTrace(); } catch (IOException e) { // TODO 自动生成 catch 块 e.printStackTrace(); } } public static void main(String[] args) { File file = new File("D:/test.xls"); readExcel(file); }}

‘陆’ java如何读取整个excel文件的内容

在java程序添加spire.xls.jar依赖

importcom.spire.xls.*;

publicclassReadExcel{
publicstaticvoidmain(String[]args){

//创建Workbook对象
Workbookwb=newWorkbook();
//加载一个Excel文档
wb.loadFromFile("C:\Users\Administrator\Desktop\test.xlsx");
//获取第一个工作表
Worksheetsheet=wb.getWorksheets().get(0);
//遍历工作表的每一行
for(inti=1;i<sheet.getLastRow()+1;i++){
//遍历工作的每一列
for(intj=1;j<sheet.getLastColumn()+1;j++){
//输出指定单元格的数据
System.out.print(sheet.get(i,j).getText());
System.out.print(" ");
}
System.out.print(" ");
}
}
}

‘柒’ java使用poi解析或处理excel的时候,如何防止数字变成科学计数法

思路为:为了防止数字变成科学计数法方式表示,在源文件以及java代码中都用文本的方式去生成和解析excel,具体如下: 1.生成Excel时,设置单元格格式为STRING,即: //关键代码HSSFCell cell = new HSSFCell();cell.setCellType(HSSFCell.CELL_TYPE_STRING);2.同理,解析的时候,首先要保证源excel文件中该单元格格式是文本类型的,然后在java代码里用STRING类型去解析: //关键代码String value = cell.getStringCellValue()。

‘捌’ java poi根据列头解析excel

这个需要你自己写方法. 遍历你的表头行每个单元格的数据
对比你传的参数 匹配时 返回 该单元格的列号. 然后再用不同的row去get得到的列号

‘玖’ 用java解析EXCEL公式的问题

获取公式值可用 HSSFFormulaEvaluator e= New HSSFFormularEvaluator(workbook);
e.evaluate(cell).getNumberiValue.
如果不是你要的结果。还有其它很多方法。
比如这样一个比较笨的方法。
double value = cell.getNumericCellValue();
value = value * 24 *3600;
int h = (int) (value /3600)
int m= (int )((value - h* 3600) /60);
int s = (int)(value-h*3600 -m* 60) ;
String result = h + ":"+m + "s" ;
得到结果。

热点内容
滑板鞋脚本视频 发布:2025-02-02 09:48:54 浏览:432
群晖怎么玩安卓模拟器 发布:2025-02-02 09:45:23 浏览:557
三星安卓12彩蛋怎么玩 发布:2025-02-02 09:44:39 浏览:743
电脑显示连接服务器错误 发布:2025-02-02 09:24:10 浏览:537
瑞芯微开发板编译 发布:2025-02-02 09:22:54 浏览:146
linux虚拟机用gcc编译时显示错误 发布:2025-02-02 09:14:01 浏览:234
java驼峰 发布:2025-02-02 09:13:26 浏览:651
魔兽脚本怎么用 发布:2025-02-02 09:10:28 浏览:538
linuxadobe 发布:2025-02-02 09:09:43 浏览:212
sql2000数据库连接 发布:2025-02-02 09:09:43 浏览:726