SQL接続データの取得

Contents
[ ]

Microsoft Excelでは、データベースに接続するには:

  1. データメニューをクリックし、その他のソース、その後 SQL Server を選択します。
  2. 次に、データ、その後 Connections を選択します。
  3. Connectionsウィザードを使用してデータベースに接続し、データベースクエリを作成します。

Microsoft ExcelでのSQL接続オプションの表示

todo:image_alt_text

Aspose.Cellsは、Workbook.getDataConnections()メソッドを使用して外部接続の取得を行います。これは、ワークブック内のExternalConnectionオブジェクトのコレクションを返します。

ExternalConnectionオブジェクトにSQL接続データが含まれている場合、DBConnectionオブジェクトに型変換し、そのプロパティを使用してデータベースのコマンド、コマンドタイプ、接続の説明、接続情報、資格情報などを取得することができます。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(RetrieveSQLConnectionData.class);
// Create a workbook object from source file
Workbook workbook = new Workbook(dataDir + "connection.xlsx");
// Access the external collections
ExternalConnectionCollection connections = workbook.getDataConnections();
int connectionCount = connections.getCount();
ExternalConnection connection = null;
for (int i = 0; i < connectionCount; i++) {
connection = connections.get(i);
// Check if the Connection is DBConnection, then retrieve its various properties
if (connection instanceof DBConnection) {
DBConnection dbConn = (DBConnection) connection;
// Retrieve DB Connection Command
System.out.println("Command: " + dbConn.getCommand());
// Retrieve DB Connection Command Type
System.out.println("Command Type: " + dbConn.getCommandType());
// Retrieve DB Connection Description
System.out.println("Description: " + dbConn.getConnectionDescription());
// Retrieve DB Connection ID
System.out.println("Id: " + dbConn.getConnectionId());
// Retrieve DB Connection Info
System.out.println("Info: " + dbConn.getConnectionInfo());
// Retrieve DB Connection Credentials
System.out.println("Credentials: " + dbConn.getCredentials());
// Retrieve DB Connection Name
System.out.println("Name: " + dbConn.getName());
// Retrieve DB Connection ODC File
System.out.println("OdcFile: " + dbConn.getOdcFile());
// Retrieve DB Connection Source File
System.out.println("Source file: " + dbConn.getSourceFile());
// Retrieve DB Connection Type
System.out.println("Type: " + dbConn.getType());
// Retrieve DB Connection Parameters Collection
ConnectionParameterCollection parameterCollection = dbConn.getParameters();
int paramCount = parameterCollection.getCount();
// Iterate the Parameter Collection
for (int j = 0; j < paramCount; j++) {
ConnectionParameter param = parameterCollection.get(j);
// Retrieve Parameter Cell Reference
System.out.println("Cell reference: " + param.getCellReference());
// Retrieve Parameter Name
System.out.println("Parameter name: " + param.getName());
// Retrieve Parameter Prompt
System.out.println("Prompt: " + param.getPrompt());
// Retrieve Parameter SQL Type
System.out.println("SQL Type: " + param.getSqlType());
// Retrieve Parameter Type
System.out.println("Param Type: " + param.getType());
// Retrieve Parameter Value
System.out.println("Param Value: " + param.getValue());
} // End for
} // End if
} // End for