日期:2014-05-16  浏览次数:20734 次

mysql 字段修改
 public static void main(String[] args) {
        String filePath = "C:\\Users\\li-ningning\\Desktop\\fnTable.xls.xls";
//        filePath = "C:\\Users\\li-ningning\\Desktop\\master_data_fncrm.xls";
        File file = new File(filePath);
        try {
            FileInputStream in = new FileInputStream(file);
            HSSFWorkbook wb = new HSSFWorkbook(in);
            // /获取第一张Sheet表
            HSSFSheet st = wb.getSheetAt(0);
            for (int i = 0; i <= st.getLastRowNum(); i++) {
                String sql = "alter table ";
                HSSFRow row = st.getRow(i);
                if (row != null) {
                    String tableName = toString((HSSFCell) row
                            .getCell((short) 0));
                    String column = toString((HSSFCell) row.getCell((short) 1));
                    String length = toString((HSSFCell) row.getCell((short) 3));
                    int value = Integer.parseInt(length);
                    sql += tableName;
                    sql += " modify ";
                    sql += column;
                    if (value <= 50) {
                        sql += "  varchar(50)";
                    } else if (value <= 100) {
                        sql += "  varchar(100)";
                    } else if (value <= 150) {
                        sql += "  varchar(150)";
                    } else if (value <= 200) {
                        sql += "  varchar(200)";
                    } else if (value <= 250) {
                        sql += "  varchar(250)";
                    } else if (value <= 300) {
                        sql += "  varchar(300)";
                    } else if (value < 500) {
                        sql += "  varchar(500)";
                    }
                    sql += ";";
                    String executeSql = sql;
                    System.out.println(executeSql);
                    sql = "";
                }
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String toString(HSSFCell cell) {
        String value = "";
        if (cell == null) {
            return value;
        }
        cell.setEncoding(HSSFCell.ENCODING_UTF_16);
        switch (cell.getCellType()) {
        case HSSFCell.CELL_TYPE_STRING:
            value = cell.getStringCellValue().toString();
            break;
        case HSSFCell.CELL_TYPE_NUMERIC:
            if (HSSFDateUtil.isCellDateFormatted(cell)) {
                Date date = cell.getDateCellValue();
                if (date != null) {
                    value = new SimpleDateFormat("yyyy-MM-dd").format(date);
                } else {
                    value = "";
                }
            } else {
                value = new DecimalFormat("0").format(cell
                        .getNumericCellValue());
            }
            break;
        case HSSFCell.CELL_TYPE_FORMULA:
            // 导入时如果为公式生成的数据则无值
            if (!cell.getStringCellValue().equals("")) {
                value = cell.getStringCellValue();
            } else {
                value = cell.getNumericCellValue() + "";
            }
            break;
        case HSSFCell.CELL_TYPE_BLANK:
            break;
        case HSSFCell.CELL_TYPE_ERROR:
            value = "";
            break;
        case HSSFCell.CELL_TYPE_BOOLEAN:
            value = (cell.getBooleanCellValue() ? "YES" : "NO");
            break;
        default:
            value = "";
        }
        return value.trim();
    }

?