java解析excel
『壹』 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,用下面的代碼
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
public class ReadCellContent {
public static void main(String[] args) {
//創建Workbook對象
Workbook wb = new Workbook();
//載入Excel文檔
wb.loadFromFile("G:\360MoveData\Users\Administrator\Desktop\test.xlsx");
//獲取第一個表格
Worksheet worksheet = wb.getWorksheets().get(0);
//獲取指定單元格內下拉列表的值
String[] values = worksheet.getCellRange(7,3).getDataValidation().getValues();
for (int i = 0; i < values.length; i++) {
System.out.println(values[i]);
}
}
}
測試結果:
『肆』 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" ;
得到結果。