日期:2014-05-20  浏览次数:20748 次

导出excel读取excel问题
我用pl/sql将数据库中的数据先导出到了cvs文件中,
然后用excel打开另存为一个.xls的excel文件。
问题就出现了,导出数据中的身份证号等excel就会转成指数形式,这样就造成了身份证号后几位的值丢失。

cvs文件用记事本打开的时候里面的数据还是正确的,另存为excel就出错了。

有没有什么办法取到完整的数据?
Java code
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.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
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;


Java code
if (file.getName().endsWith(".xls")) {
            FileInputStream fis = null;
            try {
                fis = new  FileInputStream(file);
                Workbook wb = WorkbookFactory.create(fis);
                int numberOfSheets = wb.getNumberOfSheets();
                for (int i = 0; i < numberOfSheets; i++) {
                    Sheet sheet = wb.getSheetAt(i);
                    if(sheet == null){
                        continue;
                    }
                    Row firstRow = sheet.getRow(0);
                    int lastRowNum = sheet.getLastRowNum();
                    for (int j = 0; j <= lastRowNum; j++) {
                        if(j == 0){
                            continue;
                        }
                        Map<String, String> rowData = new HashMap<String, String>();
                        Row row = sheet.getRow(j);
                        short lastCellNum = row.getLastCellNum();
                        for (int k = 0; k < lastCellNum; k++) {
                            Cell cell = row.getCell(k);
                            if(cell == null){
                                continue;
                            }
                            String value= null;
                            int cellType = cell.getCellType();
                            switch(cellType){
                            case Cell.CELL_TYPE_BLANK:
                                break;
                            case Cell.CELL_TYPE_BOOLEAN:
                                value = String.valueOf(cell.getBooleanCellValue());
                                break;
                            case Cell.CELL_TYPE_ERROR:
                                value = String.valueOf(cell.getErrorCellValue());
                                break;
                            case Cell.CELL_TYPE_NUMERIC:
                                if(DateUtil.isCellDateFormatted(cell)){
                                    value = format.format(cell.getDateCellValue());
                                }else{
                                    value = String.valueOf(cell.getNumericCellValue());
                                }
                                break;
                            case Cell.CELL_TYPE_STRING:
                                value = cell.getStringCellValue();
                                break;
                                default:break;
                            }
                            Cell cellOfFirstRow = firstRow.getCell(k);
                            if(cellOfFirstRow != null){
                                rowData.put(cellOfFirstRow.getStringCellValue(), value);
                            }
                        }
                        rowData.put("rowNum", String.valueOf(j));
                        rowData.put("reortTaskfileid", String.valueOf(reortTaskfileid));
                        data.add(rowData);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return data;