java读取excel

                   Workbook book = null;  
		   File inputFile = new File("d:/test/2aaa.最新 11372条数据.xls"); 
		   int inputFileSheetIndex = 0;
		   WorkbookSettings setting = new WorkbookSettings();   
		   java.util.Locale locale = new java.util.Locale("zh","CN"); //本地
		   setting.setLocale(locale);  
		   setting.setEncoding("UTF-8");   //设置字符集编码,不知道有用没用,我感觉没用。。。
		  
		   try{  
		    book = Workbook.getWorkbook(inputFile, setting);  
		   }catch(Exception e){  
		    e.printStackTrace();
		   }  

		   Sheet sheet = book.getSheet(inputFileSheetIndex);
		   int rowIndex = sheet.getRows();
		   int columnIndex = sheet.getColumns();
		   String[][] contents = new String[rowIndex][columnIndex];
		   for (int rows = 0; rows < rowIndex; rows++) {//行  
			   String[] column_contents = new String[columnIndex];   
			   for (int cols = 0; cols < columnIndex; cols++) {//列
				   Cell cell = sheet.getCell(cols, rows); 
		     
				     if (cell.getType() == CellType.LABEL) {
							LabelCell labelCell = (LabelCell) cell;
							String cellContent = labelCell.getString();
                                                        //去除数据后面的空格或者乱码
                                                        cellContent = (String) cellContent.subSequence(0, cellContent.length()-1);
							column_contents[cols] = cellContent;
					}else
					if (cell.getType() == CellType.NUMBER) {
							NumberCell numberCell = (NumberCell) cell;
							String cellContent = numberCell.getContents();
							column_contents[cols] = cellContent;
					}else
					if (cell.getType() == CellType.DATE) {
							DateCell dateCell = (DateCell) cell;
							Date dateDemo = dateCell.getDate();
							String cellContent = dateDemo.toString();
							column_contents[cols] = cellContent;
					}else
					if(cell.getContents()==null){
							column_contents[cols] = "null";
					}
			   } 
			   contents[rows] = column_contents;
               }
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值