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;
}
java读取excel
最新推荐文章于 2024-08-20 22:18:52 发布