Java读取excel中的数据

xiaoxiao2021-02-28  78

import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import jxl.Cell; import jxl.Sheet; import jxl.Workbook; import jxl.read.biff.BiffException; public class QueryAnalysis { /* 从标注的文件中读到哈希中 * */ public static HashMap<String,String> readFile(String filename){ HashMap<String,String>tagedData=new HashMap<String,String>(); Workbook wb=null; Cell cell=null; StringBuffer sb=new StringBuffer(); try { File f=new File(filename); InputStream in=new FileInputStream(f); //创建输入流 wb=Workbook.getWorkbook(in); //获取Excel文件对象 Sheet s=wb.getSheet(0); //获取文件的指定工作表,默认为第一个 String key=null; for(int i=1;i<s.getRows();i++){ //表头目录不需要,从第一行开始 for(int j=0;j<s.getColumns();j++){ if(j==1){ cell=s.getCell(j, i); key=cell.getContents(); } if(j!=0&&j!=3){ //只要2、4、5、6列,1列为key cell=s.getCell(j, i); //获取第i行,j列的值 sb.append(cell.getContents()); } } tagedData.put(key, sb.toString()); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (BiffException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return tagedData; } public static void main(String[] args) { // TODO Auto-generated method stub HashMap<String,String>tagedData=readFile("D://input.xls"); System.out.println(tagedData.size()); }
转载请注明原文地址: https://www.6miu.com/read-79579.html

最新回复(0)