Lettura e Scrittura Tabelle Query del Foglio di lavoro di Aspose.Cells
Aspose.Cells fornisce la collezione Worksheet.QueryTables che restituisce l’oggetto di tipo QueryTable per indice. Ha le seguenti due proprietà
- QueryTable.AdjustColumnWidth
- QueryTable.PreserveFormatting
Questi sono entrambi valori booleani. È possibile visualizzarli in Microsoft Excel tramite Dati > Connessioni > Proprietà.
Lettura e Scrittura della Tabella di Query del Foglio di Lavoro
Il seguente esempio di codice legge la prima QueryTable del primo foglio di lavoro e quindi stampa entrambe le proprietà della QueryTable. Poi imposta il QueryTable.PreserveFormatting su true.
È possibile scaricare il file Excel di origine utilizzato in questo codice e il file Excel di output generato dal codice dai seguenti link.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create workbook from source excel file | |
Workbook workbook = new Workbook(dataDir + "Sample.xlsx"); | |
// Access first worksheet | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Access first Query Table | |
QueryTable qt = worksheet.QueryTables[0]; | |
// Print Query Table Data | |
Console.WriteLine("Adjust Column Width: " + qt.AdjustColumnWidth); | |
Console.WriteLine("Preserve Formatting: " + qt.PreserveFormatting); | |
// Now set Preserve Formatting to true | |
qt.PreserveFormatting = true; | |
// Save the workbook | |
workbook.Save(dataDir + "Output_out.xlsx"); |
Output della console
Ecco l’output console del codice di esempio sopra
Adjust Column Width: True
Preserve Formatting: False
Recupero dell’intervallo di risultati della tabella di query
Aspose.Cells fornisce l’opzione di leggere l’indirizzo ossia l’intervallo di risultati delle celle per una tabella di query. Il codice seguente dimostra questa funzionalità leggendo l’indirizzo dell’intervallo di risultati per una tabella di query. È possibile scaricare il file di esempio qui.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Create workbook from source excel file | |
Workbook wb = new Workbook("Query TXT.xlsx"); | |
// Display the address(range) of result range of query table | |
Console.WriteLine(wb.Worksheets[0].QueryTables[0].ResultRange.Address); |