`
小女墙
  • 浏览: 111218 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

java读取excel

    博客分类:
  • JAVA
阅读更多
                   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;
               }
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics