需要导包 jxl
package com.dj.dao.utils;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStreamWriter;import java.io.Writer;import jxl.Cell;import jxl.Sheet;import jxl.Workbook;public class ExcelToSql { public static void main(String[] args) { jxl.Workbook readwb = null; try { System.out.println("开始读取"); // 构建Workbook对象, 只读Workbook对象 // 直接从本地文件创建Workbook InputStream instream = new FileInputStream( "D:\\sanxia\\doc\\trunk\\新oa\\系统设计\\数据库设计\\乌商项目管理平台表结构汇总.xls"); readwb = Workbook.getWorkbook(instream); String path = "D:\\sanxia\\doc\\trunk\\新oa\\系统设计\\数据库设计\\sql\\"; for (int sheetNum = 1; sheetNum < readwb.getNumberOfSheets(); sheetNum++) { // Sheet的下标是从0开始 // 获取第一张Sheet表 Sheet readsheet = readwb.getSheet(sheetNum); // 获取Sheet表中所包含的总列数 int rsColumns = readsheet.getColumns(); // 获取Sheet表中所包含的总行数 int rsRows = readsheet.getRows(); // 获取指定单元格的对象引用 String sql = ""; String tableName = ""; String PK = ""; for (int i = 0; i < rsRows; i++) { Cell[] str = readsheet.getRow(i); if (i == 0) { tableName = str[0].getContents().toString(); int startIndex = tableName.indexOf("("); if (-1 == startIndex) { startIndex = tableName.indexOf("("); } int endIndex = tableName.indexOf(")"); if (-1 == endIndex) { endIndex = tableName.indexOf(")"); } // System.out.println(tableName+":"+startIndex+"|"+endIndex); tableName = tableName.substring(startIndex + 1, endIndex); if (tableName == null || "".equals(tableName.trim())) { System.out.println("表名未知,请检查"); break; } tableName = tableName.toUpperCase(); System.out.println("tableName:" + tableName); sql = sql + "drop table " + tableName + ";\n"; sql = sql + "CREATE TABLE " + tableName; sql = sql + "\n(\n"; } if (i == 1) { continue; } if (i >= 2) { if (i < 6) { if (str[5].getContents().toString().contains("PK")) { PK = PK + "," + str[1].getContents().toString(); } } String colName = String.format("%-30s", str[1] .getContents().toString()); String colSize = String.format("%-20s", str[3] .getContents().toString()); if ("M".equals(str[4].getContents().toString())) { sql = sql + "\t" + colName + " " + colSize + " NOT NULL ,\n"; } else { sql = sql + "\t" + colName + " " + colSize + " ,\n"; } } } if (PK.length() < 1) { System.out.println("表主键无主键,请自行添加,注意最后一列去掉逗号"); } else { sql = sql + "\t\t PRIMARY KEY (" + PK.substring(1) + ") \n"; } sql = sql + ");\n"; // sql = sql + // "CREATE INDEX "+tableName+".INDEX1 ON "+tableName+"(KEY1,KEY2); --请自行替换KEY1、KEY2\n"; for (int i = 0; i < rsRows; i++) { Cell[] str = readsheet.getRow(i); if (i == 0) { String tableNamePre = str[0].getContents().toString(); int startIndex = tableNamePre.indexOf("("); if (-1 == startIndex) { startIndex = tableNamePre.indexOf("("); } tableNamePre = tableNamePre.substring(0, startIndex); ; if (tableNamePre == null || "".equals(tableNamePre.trim())) { System.out.println("表名称未知,请检查"); break; } String comment = tableNamePre; sql = sql + "COMMENT ON TABLE " + tableName + " IS '" + comment + "'\n"; } if (i == 1) { continue; } if (i >= 2) { String remark = str[5].getContents().toString(); remark = remark.replace("'", ""); remark = remark.replace("\"", ""); remark = remark.replace(",", ""); remark = remark.replace(",", ""); remark = remark.replace("。", ""); remark = remark.replace(".", ""); String comment = ""; if ("".equals(remark.trim())) { comment = str[2].getContents().toString(); } else { comment = str[2].getContents().toString() + ": " + remark; } sql = sql + "COMMENT ON COLUMN " + tableName + "." + str[1].getContents().toString() + " IS '" + comment + "';\n"; } } sql = sql.toUpperCase(); File file = new File(path + tableName + ".sql"); Writer outTxt = new OutputStreamWriter(new FileOutputStream( file, false), "unicode"); outTxt.write(sql); outTxt.close(); } /* * //利用已经创建的Excel工作薄,创建新的可写入的Excel工作薄 jxl.write.WritableWorkbook wwb * = Workbook.createWorkbook(new File( "F:/红楼人物1.xls"), readwb); * //读取第一张工作表 jxl.write.WritableSheet ws = wwb.getSheet(0); * //获得第一个单元格对象 jxl.write.WritableCell wc = ws.getWritableCell(0, * 0); //判断单元格的类型, 做出相应的转化 if (wc.getType() == CellType.LABEL) { * Label l = (Label) wc; l.setString("新姓名"); } //写入Excel对象 * wwb.write(); wwb.close(); */ } catch (Exception e) { e.printStackTrace(); } finally { readwb.close(); } }}