gestione schede file Excel:
sintassi
- scheda esistente
- crea oggetto scheda Worksheet:
Excel.Worksheet xlSchedaExcel; variabile tipo Worksheets
- assegnazione scheda all'oggetto scheda
- xlSchedaExcel = (Excel.Worksheet)xlFileExcel.Sheets["nome scheda"]
- xlSchedaExcel = (Excel.Worksheet)xlFileExcel.Worksheets.get_Item(1)
- xlSchedaExcel = (Excel.Worksheet)xlFileExcel.ActiveSheet scheda attiva
- xlSchedaExcel = xlFileExcelSPEC.Worksheets[2] scheda in seconda posizione
- aggiungere una nuova scheda
- crea oggetto scheda Sheet:
Excel.Sheets xlSchedaExcel; crea oggetto tipo Sheets
xlSchedaExcel = xlFileExcel.Worksheets; assegna all'oggetto una scheda di lavoro Worksheets
- crea oggetto nuova scheda tipo Worksheet:
- aggiunge in prima posizione
Excel.Worksheet xlNuovaSchedaExcel = (Excel.Worksheet)xlSchedaExcel.Add();
- aggiunge scheda/e secondo dei parametri
Excel.Worksheet xlNuovaSchedaExcel = (Excel.Worksheet)xlSchedaExcel.Add(xlSchedaExcel[1] posizione 1, xlSchedaExcel[dopo di], quantità (1 default), tipo);
esempio: aggiunge scheda
- aggiuge una scheda prima della prima (come il semplice Add())
Excel.Worksheet xlNuovaSchedaExcel = (Excel.Worksheet)xlSchedaExcel.Add(xlSchedaExcel[1], Type.Missing, Type.Missing, Type.Missing);
attento: non funziona se vuoi mettere il seconda posizione [2] la scheda
Excel.Worksheet xlNuovaSchedaExcel = (Excel.Worksheet)xlSchedaExcel.Add(xlSchedaExcel[2], Type.Missing, Type.Missing, Type.Missing);
devi usare:
Excel.Worksheet xlNuovaSchedaExcel = (Excel.Worksheet)xlSchedaExcel.Add(Type.Missing, xlSchedaExcel[1],Type.Missing, Type.Missing);
cioé mettere la scheda dopo la 1, non inidcando la posizione direttamente
- aggiunge 3 schede dopo la quinta scheda
Excel.Worksheet xlNuovaSchedaExcel = (Excel.Worksheet)xlSchedaExcel.Add(Type.Missing, xlSchedaExcel[5], 3, Type.Missing);
- aggiunge la scheda alla fine (usa .Count)
Excel.Worksheet xlNuovaSchedaExcel = (Excel.Worksheet)xlSchedaExcel.Add(Type.Missing, xlSchedaExcel[xlFileExcel.Worksheets.Count], Type.Missing, Type.Missing);
- scorrere tutte le schede del foglio
con l'istruzione foreach scorriamo tutte le schede del file Excel
foreach (Excel.Worksheet xlSchedaExcel in xlFileExcel.Worksheets)
{
...
}
funzioni Worksheet (sito Microsoft)
proprietà Worksheet / Worksheets (sito Microsoft)
- celle foglio
Worksheet.Cells[] cella foglio
Worksheet.get_Range(). intevallo celle foglio (metodo)
- tabelle foglio
Worksheet.ListObjects[] lista tabelle
Worksheet.PivotTables() lista tabelle pivot (metodo)
- Worksheet.Hyperlinks[] lista link
- .Name nome scheda
xlSchedaExcel.Name = "nome"
variabile_stringa = xlSchedaExcel.Name
attento il limite massimo di caratteri per il nome di una scheda in Excel é 31
- .Tab.Color colore scheda
xlSchedaExcel.Tab.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.colore);
esempio: colora scheda di rosso
xlSchedaExcel.Tab.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
- .Count costa schede
xlFileExcel.Worksheets.Count restutisce la quantità delle schede del file
- .Visible stato visibile
xlSchedaExcel.Visible.ToString() restutuisce lo stato della scheda
serve anche per settare lo stato
- Excel.XlSheetVisibility.xlSheetVisible normale
- Excel.XlSheetVisibility.xlSheetHidden nascosto
- Excel.XlSheetVisibility.xlSheetVeryHidden veramente nascosto
esempio: verifica se la scheda non é visibile, in caso rendila visibile
Excel.Worksheet xlSchedaExcel = (Excel.Worksheet)xlFileExcel.Sheets["nome scheda"];
if (xlSchedaExcel.Visible != Excel.XlSheetVisibility.xlSheetVisible)
{
xlSchedaExcel.Visible = Excel.XlSheetVisibility.xlSheetVisible;
}
- Gridlines visualizza o nasconde la Gridlines
non esiste una propietà di Worksheet occorre usare ActiveWindow
xlApp.ActiveWindow.DisplayGridlines = true / false
- Zoom settaggio dello zoom
non esiste una propietà di Worksheet occorre usare ActiveWindow
xlApp.ActiveWindow.Zoom = x
esempio: zoom a 120%
xlApp.ActiveWindow.Zoom = 120