java解析excel文件的方法
程序员文章站
2024-03-31 19:25:10
建立工程前需要导入poi包。poi相关jar包下载地址:
1.解析.xlsx后缀名的的excel文件:
package com.shuai.hello;...
建立工程前需要导入poi包。poi相关jar包下载地址:
1.解析.xlsx后缀名的的excel文件:
package com.shuai.hello; import java.io.fileinputstream; import java.io.ioexception; import java.io.inputstream; import org.apache.poi.hssf.usermodel.hssfcell; import org.apache.poi.xssf.usermodel.xssfcell; import org.apache.poi.xssf.usermodel.xssfrow; import org.apache.poi.xssf.usermodel.xssfsheet; import org.apache.poi.xssf.usermodel.xssfworkbook; public class readexcel { public static void main(string[] args) throws ioexception { //file file = new file("c:/users.xlsx"); inputstream stream = new fileinputstream("c:/users.xlsx"); xssfworkbook xssfworkbook = new xssfworkbook(stream); xssfsheet xssfsheet = xssfworkbook.getsheetat(0); int rowstart = xssfsheet.getfirstrownum(); int rowend = xssfsheet.getlastrownum(); for(int i=rowstart;i<=rowend;i++) { xssfrow row = xssfsheet.getrow(i); if(null == row) continue; int cellstart = row.getfirstcellnum(); int cellend = row.getlastcellnum(); for(int k=cellstart;k<=cellend;k++) { xssfcell cell = row.getcell(k); if(null==cell) continue; switch (cell.getcelltype()) { case hssfcell.cell_type_numeric: // 数字 system.out.print(cell.getnumericcellvalue() + "\t"); break; case hssfcell.cell_type_string: // 字符串 system.out.print(cell.getstringcellvalue() + "\t"); break; case hssfcell.cell_type_boolean: // boolean system.out.println(cell.getbooleancellvalue() + "\t"); break; case hssfcell.cell_type_formula: // 公式 system.out.print(cell.getcellformula() + "\t"); break; case hssfcell.cell_type_blank: // 空值 system.out.println(" "); break; case hssfcell.cell_type_error: // 故障 system.out.println(" "); break; default: system.out.print("未知类型 "); break; } } system.out.print("\n"); } } } /*string filetype = filepath.substring(filepath.lastindexof(".") + 1, filepath.length()); inputstream stream = new fileinputstream(filepath); workbook wb = null; if (filetype.equals("xls")) { wb = new hssfworkbook(stream); } else if (filetype.equals("xlsx")) { wb = new xssfworkbook(stream); } else { system.out.println("您输入的excel格式不正确"); }*/
2.解析后缀为.xls的excel文件:
package com.shuai.hello; import java.io.file; import java.io.fileinputstream; import java.io.ioexception; 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; import org.apache.poi.poifs.filesystem.poifsfilesystem; public class readxls { public static void main(string[] args) throws ioexception, ioexception { file file = new file("c:/users/dengta/desktop/ok1.xls"); poifsfilesystem poifsfilesystem = new poifsfilesystem(new fileinputstream(file)); hssfworkbook hssfworkbook = new hssfworkbook(poifsfilesystem); hssfsheet hssfsheet = hssfworkbook.getsheetat(0); int rowstart = hssfsheet.getfirstrownum(); int rowend = hssfsheet.getlastrownum(); for(int i=rowstart;i<=rowend;i++) { hssfrow row = hssfsheet.getrow(i); if(null == row) continue; int cellstart = row.getfirstcellnum(); int cellend = row.getlastcellnum(); for(int k=cellstart;k<=cellend;k++) { hssfcell cell = row.getcell(k); if(null==cell) continue; //system.out.print("" + k + " "); //system.out.print("type:"+cell.getcelltype()); switch (cell.getcelltype()) { case hssfcell.cell_type_numeric: // 数字 system.out.print(cell.getnumericcellvalue() + " "); break; case hssfcell.cell_type_string: // 字符串 system.out.print(cell.getstringcellvalue() + " "); break; case hssfcell.cell_type_boolean: // boolean system.out.println(cell.getbooleancellvalue() + " "); break; case hssfcell.cell_type_formula: // 公式 system.out.print(cell.getcellformula() + " "); break; case hssfcell.cell_type_blank: // 空值 system.out.println(" "); break; case hssfcell.cell_type_error: // 故障 system.out.println(" "); break; default: system.out.print("未知类型 "); break; } } system.out.print("\n"); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。