|
@@ -0,0 +1,221 @@
|
|
|
|
+package cn.com.qmth.export;
|
|
|
|
+
|
|
|
|
+import java.io.File;
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.sql.Connection;
|
|
|
|
+import java.sql.Driver;
|
|
|
|
+import java.sql.DriverManager;
|
|
|
|
+import java.sql.PreparedStatement;
|
|
|
|
+import java.sql.ResultSet;
|
|
|
|
+import java.sql.SQLException;
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.Arrays;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Properties;
|
|
|
|
+
|
|
|
|
+import org.apache.log4j.LogManager;
|
|
|
|
+import org.apache.log4j.Logger;
|
|
|
|
+import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFRow;
|
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFSheet;
|
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
|
+
|
|
|
|
+import oracle.jdbc.driver.OracleDriver;
|
|
|
|
+
|
|
|
|
+public class ExportPaperByCourseCode {
|
|
|
|
+ private static Logger logger = LogManager.getLogger(Export.class);
|
|
|
|
+ private static String excelDir;
|
|
|
|
+ private static String quesDir;
|
|
|
|
+
|
|
|
|
+ public static void main(String[] args) {
|
|
|
|
+ logger.debug("导出开始");
|
|
|
|
+ try {
|
|
|
|
+ dispose();
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ logger.error(e.getCause(),e);
|
|
|
|
+ }
|
|
|
|
+ logger.debug("导出结束");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static void dispose() throws InvalidFormatException, IOException, SQLException {
|
|
|
|
+ File directory = new File("");
|
|
|
|
+ excelDir = directory.getAbsolutePath() + "\\excelDir\\";
|
|
|
|
+ quesDir = excelDir + "quesDir\\";
|
|
|
|
+ File excelFolder = new File(excelDir);
|
|
|
|
+ if (excelFolder.exists()) {
|
|
|
|
+ FileUtil.clearDirectory(excelDir);
|
|
|
|
+ } else {
|
|
|
|
+ excelFolder.mkdir();
|
|
|
|
+ }
|
|
|
|
+ File quesFolder = new File(quesDir);
|
|
|
|
+ quesFolder.mkdir();
|
|
|
|
+ List<String> codes = readCourseCode();
|
|
|
|
+ if (codes == null || codes.size() == 0) {
|
|
|
|
+ logger.debug("无数据导出");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ Connection connect = null;
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ Driver driver = new OracleDriver();
|
|
|
|
+ DriverManager.deregisterDriver(driver);
|
|
|
|
+
|
|
|
|
+ Properties pro = new Properties();
|
|
|
|
+ pro.put("user", "qspad");
|
|
|
|
+ pro.put("password", "qspad1");
|
|
|
|
+ connect = driver.connect("jdbc:oracle:thin:@202.114.196.115:1521:qspad", pro);
|
|
|
|
+ for (String code : codes) {
|
|
|
|
+ exportPaper(connect, code);
|
|
|
|
+ }
|
|
|
|
+ } finally {
|
|
|
|
+ if (connect != null) {
|
|
|
|
+ connect.close();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static List<String> readCourseCode() throws InvalidFormatException, IOException {
|
|
|
|
+ File directory = new File("");
|
|
|
|
+ List<String> list = new ArrayList<String>();
|
|
|
|
+ XSSFWorkbook wb = null;
|
|
|
|
+ try {
|
|
|
|
+ wb = new XSSFWorkbook(directory.getAbsolutePath() + "\\CourseCode.xlsx");
|
|
|
|
+ XSSFSheet sheet = wb.getSheetAt(0);
|
|
|
|
+ int rows = sheet.getLastRowNum();
|
|
|
|
+ for (int i = 1; i <= rows; i++) {
|
|
|
|
+ XSSFRow row = sheet.getRow(i);
|
|
|
|
+ list.add(row.getCell(1).getStringCellValue());
|
|
|
|
+ }
|
|
|
|
+ } finally {
|
|
|
|
+ if (wb != null) {
|
|
|
|
+ wb.close();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return list;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static void exportPaper(Connection connect, String code) throws SQLException, IOException {
|
|
|
|
+ PreparedStatement preState = null;
|
|
|
|
+ ResultSet resultSet = null;
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ String sql = "SELECT * from TEST_PAPER_INFO p WHERE p.COURSE_ID in "
|
|
|
|
+ + "(SELECT c.ID FROM PE_TCH_COURSE c WHERE c.code = '" + code
|
|
|
|
+ + "') and (p.INVALID_REASON is null or p.INVALID_REASON='') and p.PAPER_STATUS = 0 "
|
|
|
|
+ + "and p.NAME not like '%考试'";
|
|
|
|
+ preState = connect.prepareStatement(sql);
|
|
|
|
+
|
|
|
|
+ resultSet = preState.executeQuery();
|
|
|
|
+ List<TestPaper> tps = new ArrayList<TestPaper>();
|
|
|
|
+ while (resultSet.next()) {
|
|
|
|
+ TestPaper tp = new TestPaper();
|
|
|
|
+ tp.setId(resultSet.getString("id"));
|
|
|
|
+ tp.setName(resultSet.getString("name"));
|
|
|
|
+ tp.setTestPaperContentId(resultSet.getString("test_paper_content_id"));
|
|
|
|
+ tp.setScore(resultSet.getDouble("score"));
|
|
|
|
+ tps.add(tp);
|
|
|
|
+ }
|
|
|
|
+ List<SheetData> sheets = new ArrayList<SheetData>();
|
|
|
|
+ List<String> paperheader = new ArrayList<String>();
|
|
|
|
+ paperheader.addAll(Arrays.asList(new String[] { "试卷ID", "试卷名称", "试卷内容ID", "试卷满分" }));
|
|
|
|
+ fillPaperData(sheets, tps, paperheader);
|
|
|
|
+ int flag = 1;
|
|
|
|
+ for (TestPaper tp : tps) {
|
|
|
|
+ exportQuestion(connect, tp.getTestPaperContentId(), sheets, flag);
|
|
|
|
+ flag++;
|
|
|
|
+ }
|
|
|
|
+ ExportUtils.exportExcel(excelDir, code, sheets);
|
|
|
|
+ } finally {
|
|
|
|
+ if (resultSet != null) {
|
|
|
|
+ resultSet.close();
|
|
|
|
+ }
|
|
|
|
+ if (preState != null) {
|
|
|
|
+ preState.close();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static void fillPaperData(List<SheetData> sheets, List<TestPaper> list, List<String> header) {
|
|
|
|
+ SheetData sheet = new SheetData();
|
|
|
|
+ sheet.setHeader(header);
|
|
|
|
+ sheet.setName("课程试卷信息");
|
|
|
|
+ List<Object[]> data = new ArrayList<Object[]>();
|
|
|
|
+ for (TestPaper b : list) {
|
|
|
|
+ Object[] ob = new Object[header.size()];
|
|
|
|
+ ob[0] = b.getId();
|
|
|
|
+ ob[1] = b.getName();
|
|
|
|
+ ob[2] = b.getTestPaperContentId();
|
|
|
|
+ ob[3] = b.getScore();
|
|
|
|
+ data.add(ob);
|
|
|
|
+ }
|
|
|
|
+ sheet.setData(data);
|
|
|
|
+ sheets.add(sheet);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static void exportQuestion(Connection connect, String testPaperContentId, List<SheetData> sheets, int flag)
|
|
|
|
+ throws SQLException, IOException {
|
|
|
|
+ PreparedStatement preState = null;
|
|
|
|
+ ResultSet resultSet = null;
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ String sql = "SELECT q.ID id,d.dNumber,d.NAME dName,d.number1 number1,d.score score,q.PARENT_ID parent_id,q.QUESTION_TYPE_ENUM type,q.JSON "
|
|
|
|
+ + "FROM QUESTION_CONTENT q ,(SELECT b.QUESTION_CONTENT_ID question_id, b.SCORE score, b.SEQUENCE number1, c.NAME name, c.dNumber dNumber "
|
|
|
|
+ + "FROM TEST_PAPER_QUESTION b ,(SELECT a.ID id, a.NAME name,a.SEQUENCE dNumber,a.TEST_PAPER_CONTENT_ID "
|
|
|
|
+ + "FROM TEST_PAPER_QUESTION_GROUP a WHERE a.TEST_PAPER_CONTENT_ID = '" + testPaperContentId
|
|
|
|
+ + "' ORDER BY a.SEQUENCE) c "
|
|
|
|
+ + "WHERE b.TEST_PAPER_QUESTION_GROUP_ID = c.id ORDER BY c.dNumber,b.SEQUENCE) d WHERE q.ID = d.question_id ORDER BY d.dNumber,d.number1";
|
|
|
|
+ preState = connect.prepareStatement(sql);
|
|
|
|
+
|
|
|
|
+ resultSet = preState.executeQuery();
|
|
|
|
+ List<TestQuestion> tqs = new ArrayList<TestQuestion>();
|
|
|
|
+ while (resultSet.next()) {
|
|
|
|
+ TestQuestion tq = new TestQuestion();
|
|
|
|
+ tq.setId(resultSet.getString("id"));
|
|
|
|
+ tq.setdNumber(resultSet.getInt("dnumber"));
|
|
|
|
+ tq.setdName(resultSet.getString("dname"));
|
|
|
|
+ tq.setNumber(resultSet.getInt("number1"));
|
|
|
|
+ tq.setScore(resultSet.getDouble("score"));
|
|
|
|
+ tq.setParentId(resultSet.getString("parent_id"));
|
|
|
|
+ tq.setType(resultSet.getInt("type"));
|
|
|
|
+ tq.setJson(resultSet.getString("json"));
|
|
|
|
+ tqs.add(tq);
|
|
|
|
+ }
|
|
|
|
+ List<String> header = new ArrayList<String>();
|
|
|
|
+ header.addAll(Arrays.asList(new String[] { "试题ID", "大题号", "大题名称", "小题号", "题分", "题父ID", "题类型" }));
|
|
|
|
+ fillQuestionData(sheets, tqs, header, flag);
|
|
|
|
+ } finally {
|
|
|
|
+ if (resultSet != null) {
|
|
|
|
+ resultSet.close();
|
|
|
|
+ }
|
|
|
|
+ if (preState != null) {
|
|
|
|
+ preState.close();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static void fillQuestionData(List<SheetData> sheets, List<TestQuestion> list, List<String> header, int flag)
|
|
|
|
+ throws IOException {
|
|
|
|
+ SheetData sheet = new SheetData();
|
|
|
|
+ sheet.setHeader(header);
|
|
|
|
+ sheet.setName("试卷" + flag);
|
|
|
|
+ List<Object[]> data = new ArrayList<Object[]>();
|
|
|
|
+ for (TestQuestion b : list) {
|
|
|
|
+ Object[] ob = new Object[header.size()];
|
|
|
|
+ ob[0] = b.getId();
|
|
|
|
+ ob[1] = b.getdNumber();
|
|
|
|
+ ob[2] = b.getdName();
|
|
|
|
+ ob[3] = b.getNumber();
|
|
|
|
+ ob[4] = b.getScore();
|
|
|
|
+ ob[5] = b.getParentId();
|
|
|
|
+ ob[6] = b.getType();
|
|
|
|
+ File txtFile = new File(quesDir + b.getId() + ".txt");
|
|
|
|
+ if (!txtFile.exists()) {
|
|
|
|
+ FileUtil.writeTxt(quesDir, b.getId(), b.getJson());
|
|
|
|
+ }
|
|
|
|
+ data.add(ob);
|
|
|
|
+ }
|
|
|
|
+ sheet.setData(data);
|
|
|
|
+ sheets.add(sheet);
|
|
|
|
+ }
|
|
|
|
+}
|