當前位置:首頁 » 編程語言 » javaexcel解析

javaexcel解析

發布時間: 2023-02-27 00:38:41

A. java poi根據列頭解析excel

這個需要你自己寫方法. 遍歷你的表頭行每個單元格的數據
對比你傳的參數 匹配時 返回 該單元格的列號. 然後再用不同的row去get得到的列號

B. 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;
}

}

C. java 怎樣解析 excel生成的xml文件

java解析excel生成的xml文件的方法是使用dom4j實現的。
dom4j是一個簡單的開源庫,用於處理XML、 XPath和XSLT,它基於Java平台,使用Java的集合框架,全面集成了DOM,SAX和JAXP。
1、excel生成的xml樣例文件:
<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
<Created>2006-09-16T00:00:00Z</Created>
<LastSaved>2016-07-25T03:26:50Z</LastSaved>
<Version>14.00</Version>
</DocumentProperties>
<OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">
<AllowPNG/>
<RemovePersonalInformation/>
</OfficeDocumentSettings>
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
<WindowHeight>7956</WindowHeight>
<WindowWidth>14808</WindowWidth>
<WindowTopX>240</WindowTopX>
<WindowTopY>168</WindowTopY>
<ActiveSheet>2</ActiveSheet>
<ProtectStructure>False</ProtectStructure>
<ProtectWindows>False</ProtectWindows>
</ExcelWorkbook>
<Styles>
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Bottom"/>
<Borders/>
<Font ss:FontName="宋體" x:CharSet="134" ss:Size="11" ss:Color="#000000"/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
<Style ss:ID="s16" ss:Name="好">
<Font ss:FontName="宋體" x:CharSet="134" ss:Size="11" ss:Color="#006100"/>
<Interior ss:Color="#C6EFCE" ss:Pattern="Solid"/>
</Style>
<Style ss:ID="s17">
<Alignment ss:Horizontal="Left" ss:Vertical="Center" ss:Indent="1"
ss:WrapText="1"/>
<Font ss:FontName="宋體" x:CharSet="134" ss:Size="8" ss:Color="#686868"/>
<NumberFormat ss:Format="@"/>
</Style>
<Style ss:ID="s18" ss:Parent="s16">
<Alignment ss:Vertical="Bottom"/>
</Style>
<Style ss:ID="s19">
<NumberFormat ss:Format="yyyy/m/d\ h:mm:ss"/>
</Style>
</Styles>
<Worksheet ss:Name="Sheet1">
<Table ss:ExpandedColumnCount="6" ss:ExpandedRowCount="3" x:FullColumns="1"
x:FullRows="1" ss:DefaultRowHeight="14.4">
<Row>
<Cell><Data ss:Type="String">工號</Data></Cell>
<Cell><Data ss:Type="String">姓名 </Data></Cell>
<Cell ss:Index="5"><Data ss:Type="String">工號</Data></Cell>
<Cell><Data ss:Type="String">姓名</Data></Cell>
</Row>
<Row>
<Cell><Data ss:Type="Number">111</Data></Cell>
<Cell><Data ss:Type="String">張三</Data></Cell>
<Cell ss:Index="5"><Data ss:Type="Number">111</Data></Cell>
<Cell ss:Formula="=VLOOKUP(R2C5:R3C5,RC[-5]:R[1]C[-4],2)"><Data
ss:Type="String">張三</Data></Cell>
</Row>
<Row>
<Cell><Data ss:Type="Number">112</Data></Cell>
<Cell><Data ss:Type="String">李四</Data></Cell>
<Cell ss:Index="5"><Data ss:Type="Number">112</Data></Cell>
<Cell ss:Formula="=VLOOKUP(R2C5:R3C5,RC[-5]:R[1]C[-4],2)"><Data
ss:Type="String">李四</Data></Cell>
</Row>
</Table>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<PageSetup>
<Header x:Margin="0.3"/>
<Footer x:Margin="0.3"/>
<PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/>
</PageSetup>
<Panes>
<Pane>
<Number>3</Number>
<ActiveRow>7</ActiveRow>
<ActiveCol>5</ActiveCol>
</Pane>
</Panes>
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
</Workbook>
2、java解析代碼:
import java.io.File;
import java.util.Iterator;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class Demo {
public static void main(String[] args) throws Exception {
SAXReader reader = new SAXReader();
Document document = reader.read(new File("person.xml"));
Element root = document.getRootElement();

Iterator it = root.elementIterator();
while (it.hasNext()) {
Element element = (Element) it.next();

//未知屬性名稱情況下
/*Iterator attrIt = element.attributeIterator();
while (attrIt.hasNext()) {
Attribute a = (Attribute) attrIt.next();
System.out.println(a.getValue());
}*/

//已知屬性名稱情況下
System.out.println("id: " + element.attributeValue("id"));

//未知元素名情況下
/*Iterator eleIt = element.elementIterator();
while (eleIt.hasNext()) {
Element e = (Element) eleIt.next();
System.out.println(e.getName() + ": " + e.getText());
}
System.out.println();*/

//已知元素名情況下
System.out.println("title: " + element.elementText("title"));
System.out.println("author: " + element.elementText("author"));
System.out.println();
}
}
}

D. 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. }

測試結果:

E. 用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" ;
得到結果。

熱點內容
網站源碼查殺 發布:2024-05-16 16:02:53 瀏覽:834
伺服器不用導軌怎麼辦 發布:2024-05-16 15:49:09 瀏覽:180
如何查看pppoe密碼 發布:2024-05-16 15:38:02 瀏覽:173
雲伺服器成本價大概多少 發布:2024-05-16 15:33:42 瀏覽:446
正式服海島冰茶怎麼配置 發布:2024-05-16 15:25:39 瀏覽:290
安卓怎麼注銷探探 發布:2024-05-16 15:24:38 瀏覽:960
ata72x是多大的壓縮機 發布:2024-05-16 15:07:59 瀏覽:95
安卓如何實現carplay的功能 發布:2024-05-16 15:02:54 瀏覽:713
網路如何接通外國游戲伺服器 發布:2024-05-16 14:53:34 瀏覽:628
下載不是緩存 發布:2024-05-16 14:37:06 瀏覽:504