Aspose.Cells を使用して既存の SQL データ接続を変更します
Contents
[
Hide
]
Aspose.Cells は、既存の SQL データ接続の変更をサポートしています。この記事では、Aspose.Cells を使用して SQL データ接続のさまざまなプロパティを変更する方法について説明します。
次の方法で、Microsoft Excel 内のデータ接続を追加または表示できます。データ > 接続メニューコマンド。
同様に、Aspose.Cells は Workbook.getDataConnections() コレクションを使用してデータ接続にアクセスし、変更する手段を提供します。
Aspose.Cells を使用して既存の SQL データ接続を変更します
次のサンプルは、Aspose.Cells を使用してブックの SQL データ接続を変更する方法を示しています。このコードで使用されているソース Excel ファイルと、コードによって生成された出力 Excel ファイルは、次のリンクからダウンロードできます。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(ModifyExistingSQLDataConnection.class); | |
// Create a workbook object from source file | |
Workbook workbook = new Workbook(dataDir + "DataConnection.xlsx"); | |
// Access first Data Connection | |
ExternalConnection conn = workbook.getDataConnections().get(0); | |
// Change the Data Connection Name and Odc file | |
conn.setName("MyConnectionName"); | |
conn.setOdcFile(dataDir + "MyDefaulConnection.odc"); | |
// Change the Command Type, Command and Connection String | |
DBConnection dbConn = (DBConnection) conn; | |
dbConn.setCommandType(OLEDBCommandType.SQL_STATEMENT); | |
dbConn.setCommand("Select * from AdminTable"); | |
dbConn.setConnectionInfo( | |
"Server=myServerAddress;Database=myDataBase;User ID=myUsername;Password=myPassword;Trusted_Connection=False"); | |
// Save the workbook | |
workbook.save(dataDir + "outxput.xlsx"); |