77 lines
3.6 KiB
Java
77 lines
3.6 KiB
Java
package com.amarsoft.aims.tools;
|
|
import java.io.File;
|
|
import java.io.FileWriter;
|
|
import java.io.IOException;
|
|
import java.io.PrintWriter;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.regex.Matcher;
|
|
import java.util.regex.Pattern;
|
|
|
|
import jxl.Sheet;
|
|
import jxl.Workbook;
|
|
import jxl.read.biff.BiffException;
|
|
|
|
public class Excel2SqlConvertor {
|
|
/**
|
|
*将xls文件按照指定的sql语句模板转换成sql脚本。
|
|
*@param xlsFile xls文件路径
|
|
*@param sqlFile 要输出的sql脚本文件路径
|
|
*@param template sql语句模板
|
|
*@param sheetIndex xls中标签页的序号(从0开始)
|
|
*@param start 转换开始的行数 (从0开始)
|
|
*@param end 转换结束的行数 (从0开始)
|
|
*@throws IOException
|
|
*@throws BiffException
|
|
*/
|
|
public static void convert(String xlsFile, String sqlFile, String template,int sheetIndex,int start, int end) throws IOException, BiffException {
|
|
// 获取工作薄
|
|
Workbook workbook = Workbook.getWorkbook(new File(xlsFile));
|
|
Sheet sheet = workbook.getSheet(sheetIndex); // 获取sql中的所有字段点位符
|
|
Matcher matcher = Pattern.compile("(:\\d)").matcher(template);
|
|
List<Integer> columns = new ArrayList();
|
|
while (matcher.find()) {
|
|
columns.add(Integer.valueOf(matcher.group().replace(":", "")));
|
|
}
|
|
// 输出sql脚本文件
|
|
PrintWriter writer = new PrintWriter(new FileWriter(sqlFile));
|
|
System.out.println("Writing sql statements to file: " + sqlFile);
|
|
System.out.println("-------------------------------------------------------------------------------");
|
|
int rowCount = 0;
|
|
for (int i = 0, j = sheet.getRows(); i < j; i++) {
|
|
if (i < start - 1 || i >= end) {
|
|
continue;
|
|
}else {
|
|
// 组装sql语句
|
|
String line = new String(template);
|
|
for (Integer column : columns) {
|
|
//System.out.println("xfliu"+line);
|
|
line = line.replace(":" + column, sheet.getCell(column, i).getContents());
|
|
}
|
|
System.out.println(line);
|
|
writer.println(line);
|
|
rowCount++;
|
|
}
|
|
}
|
|
writer.flush();
|
|
writer.close();
|
|
System.out.println("-------------------------------------------------------------------------------");
|
|
System.out.format("Converting completed. %d row(s) in total.%n", rowCount);
|
|
//Runtime.getRuntime().exec("notepad.exe " + sqlFile); }
|
|
|
|
}
|
|
public static void main(String[] args) {
|
|
String xlsFile = "d:\\文档梳理20140813.xls";
|
|
String sqlFile = "d:\\文档梳理20140813XT.SQL";
|
|
// 其中:0 :2 :6 为值所在excel的列号
|
|
//String template = "INSERT INTO NEW_BUSINESS_TYPE(TYPENO, SORTNO, TYPENAME,ISINUSE,OFFSHEETFLAG,ApplyDetailNo,ApproveDetailNo,ContractDetailNo,DisplayTemplet,INPUTUSER,INPUTORG,INPUTTIME) VALUES(':0', ':0', ':1',':2',':3',':4',':5',':6',':7'"+",'system','1','"+StringFunction.getTodayNow()+"');";
|
|
//String template = "insert into CODE_LIBRARY (CODENO, ITEMNO, ITEMNAME, BANKNO, SORTNO, ISINUSE, ITEMDESCRIBE, ITEMATTRIBUTE, RELATIVECODE, ATTRIBUTE1, ATTRIBUTE2, ATTRIBUTE3, ATTRIBUTE4, ATTRIBUTE5, ATTRIBUTE6, ATTRIBUTE7, ATTRIBUTE8, INPUTUSER, INPUTORG, INPUTTIME, UPDATEUSER, UPDATETIME, REMARK, HELPTEXT) values ('BelSecIndustry',':0', ':1', null, ':0', '1', null, null, null, null, null, null, null, null, null, null, null, 'SYS_Designer', null, '2014/07/17', 'SYS_Designer', '2014/07/17', null, null);";
|
|
String template = "insert into doc_library (DOCNO,sortno,doctitle,DocStage,REMARK) values (':0', ':0', ':1', ':2', ':3');";
|
|
try {
|
|
Excel2SqlConvertor.convert(xlsFile, sqlFile, template, 1, 0, 170);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|