| | |
| | | import org.apache.poi.ooxml.POIXMLDocument; |
| | | import org.apache.poi.ooxml.extractor.POIXMLTextExtractor; |
| | | import org.apache.poi.openxml4j.opc.OPCPackage; |
| | | import org.apache.poi.poifs.filesystem.POIFSFileSystem; |
| | | import org.apache.poi.ss.usermodel.Sheet; |
| | | import org.apache.poi.ss.usermodel.Workbook; |
| | | import org.apache.poi.ss.usermodel.WorkbookFactory; |
| | |
| | | //获取最后一行的num,即总行数。此处从0开始 |
| | | int maxRow = sheet.getLastRowNum(); |
| | | for (int row = 0; row <= maxRow; row++) { |
| | | int maxRol = sheet.getRow(row).getLastCellNum(); |
| | | StringBuilder aLine = new StringBuilder(); |
| | | for (int rol = 0; rol < maxRol; rol++) { |
| | | aLine.append(sheet.getRow(row).getCell(rol)).append(splitIdentifier); |
| | | if(Objects.nonNull(sheet.getRow(row))){ |
| | | int maxRol = sheet.getRow(row).getLastCellNum(); |
| | | StringBuilder aLine = new StringBuilder(); |
| | | for (int rol = 0; rol < maxRol; rol++) { |
| | | aLine.append(sheet.getRow(row).getCell(rol)).append(splitIdentifier); |
| | | } |
| | | result.append(aLine).append("\n"); |
| | | } |
| | | result.append(aLine).append("\n"); |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | |
| | | if(StringUtils.isNotBlank(getFileDto.getMdbSampleCode())){ |
| | | s+=" and " + getFileDto.getMdbSampleCode() + " = '" + getFileDto.getSampleCode() + "'"; |
| | | } |
| | | if(StringUtils.isNotBlank(getFileDto.getBushingColorField())){ |
| | | if(ObjectUtils.allNotNull(getFileDto.getBushingColorField(), getFileDto.getBushingColor()) && !getFileDto.getBushingColorField().equals("null")){ |
| | | s+=" and " + getFileDto.getBushingColorField() + " = '" + getFileDto.getBushingColor() + "'"; |
| | | } |
| | | |
| | |
| | | String table = getFileDto.getDbTable(); |
| | | // 检查数据库名和表名是否为空 |
| | | if (dbName == null || dbName.isEmpty() || table == null || table.isEmpty()) { |
| | | return R.failed("数据库名或表名不能为空"); |
| | | return Result.failed("数据库名或表名不能为空"); |
| | | } |
| | | // 数据库连接信息 |
| | | String url = "jdbc:mysql://localhost:3306/"+dbName+"?useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true&characterEncoding=utf8"; |
| | |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | // 假设 R 类有 failed 方法,若没有需补充实现 |
| | | return R.failed("数据库查询出错: " + e.getMessage()); |
| | | return Result.failed("数据库查询出错: " + e.getMessage()); |
| | | } finally { |
| | | try { |
| | | if (resultSet != null) { |
| | |
| | | List<Object> list = new ArrayList<>(); |
| | | //转义中文 |
| | | String sampleCode = changeCharset(getFileDto.getSampleCode(), "GBK"); |
| | | String sql = "select * from " + getFileDto.getDbFileName() + |
| | | " where " + getFileDto.getMdbEntrustCode() + " = '" + getFileDto.getEntrustCode() + |
| | | "' and " + getFileDto.getMdbSampleCode() + " = '" + sampleCode + "'"; |
| | | String sql = "select * from " + getFileDto.getDbFileName() + " where 1=1"; |
| | | if(StringUtils.isNotBlank(getFileDto.getMdbEntrustCode())){ |
| | | sql+=" and " + getFileDto.getMdbEntrustCode() + " = '" + getFileDto.getEntrustCode() +"'"; |
| | | } |
| | | if(StringUtils.isNotBlank(getFileDto.getMdbSampleCode())){ |
| | | sql+=" and " + getFileDto.getMdbSampleCode() + " = '" + sampleCode + "'"; |
| | | } |
| | | if (ObjectUtils.allNotNull(getFileDto.getFiberOpticRibbonCode(), getFileDto.getFiberOpticRibbon()) && !getFileDto.getFiberOpticRibbonCode().equals("null")) { |
| | | sql = sql + " and " + getFileDto.getFiberOpticRibbonCode() + " = '" + getFileDto.getFiberOpticRibbon() + "'"; |
| | | } |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 读取sqlserver数据 |
| | | * @param getFileDto 请求参数对象 |
| | | * @return |
| | | */ |
| | | public static Object readSqlServerDataBase(GetFileDto getFileDto) { |
| | | Map<String, Object> tableMap = new HashMap<>(16); |
| | | // 从 GetFileDto 获取数据库名,对应【文件名称】字段 |
| | | String dbName = getFileDto.getDbFileName(); |
| | | String user = getFileDto.getDbUserName(); |
| | | String password = getFileDto.getDbPassword(); |
| | | // 从 GetFileDto 获取数据表名,对应【数据库表名】字段 |
| | | String table = getFileDto.getDbTable(); |
| | | // 检查数据库名和表名是否为空 |
| | | if (dbName == null || dbName.isEmpty() || table == null || table.isEmpty()) { |
| | | return Result.failed("数据库名或表名不能为空"); |
| | | } |
| | | // 数据库连接信息 |
| | | String url = "jdbc:sqlserver://localhost:1433;DatabaseName="+dbName+";encrypt=true;trustServerCertificate=true"; |
| | | Connection connection = null; |
| | | PreparedStatement preparedStatement = null; |
| | | ResultSet resultSet = null; |
| | | List<Map<String, Object>> dataList = new ArrayList<>(); |
| | | try { |
| | | // 建立连接 |
| | | connection = DriverManager.getConnection(url, user, password); |
| | | // 构建基础 SQL |
| | | |
| | | String sql = "SELECT * FROM "+table+" WHERE 1=1"; |
| | | if(StringUtils.isNotBlank(getFileDto.getMdbEntrustCode()) ){ |
| | | sql+=" AND (" + getFileDto.getMdbEntrustCode() + " = TRIM('" + getFileDto.getEntrustCode()+ "')"; |
| | | if(StringUtils.isNotBlank(getFileDto.getLotBatchNo())){ |
| | | sql+=" OR "+ getFileDto.getMdbEntrustCode() + " = TRIM('" + getFileDto.getLotBatchNo()+ "')"; |
| | | } |
| | | sql+=")"; |
| | | } |
| | | if(StringUtils.isNotBlank(getFileDto.getMdbSampleCode())){ |
| | | sql+=" AND " + getFileDto.getMdbSampleCode() + " = '" + getFileDto.getSampleCode() + "'"; |
| | | } |
| | | if(StringUtils.isNotBlank(getFileDto.getCableTag())){ |
| | | sql+=" AND Color = '" + getFileDto.getCableTag() + "'"; |
| | | } |
| | | // 创建 PreparedStatement 对象执行 SQL |
| | | preparedStatement = connection.prepareStatement(sql); |
| | | resultSet = preparedStatement.executeQuery(); |
| | | ResultSetMetaData metaData = resultSet.getMetaData(); |
| | | int columnCount = metaData.getColumnCount(); |
| | | // 遍历结果集获取数据 |
| | | while (resultSet.next()) { |
| | | Map<String, Object> rowData = new HashMap<>(); |
| | | for (int i = 1; i <= columnCount; i++) { |
| | | String columnName = metaData.getColumnName(i); |
| | | rowData.put(columnName, resultSet.getObject(i)); |
| | | } |
| | | dataList.add(rowData); |
| | | } |
| | | tableMap.put("data", dataList); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | // 假设 R 类有 failed 方法,若没有需补充实现 |
| | | return Result.failed("数据库查询出错: " + e.getMessage()); |
| | | } finally { |
| | | try { |
| | | if (resultSet != null) { |
| | | resultSet.close(); |
| | | } |
| | | if (preparedStatement != null) { |
| | | preparedStatement.close(); |
| | | } |
| | | if (connection != null) { |
| | | connection.close(); |
| | | } |
| | | } catch (SQLException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | return tableMap; |
| | | } |
| | | } |