collaudato
private void button1_Click(object sender, EventArgs e)
{
object xlApp;
object xlWbCol;
object xlWb;
object xlSheet;
object xlRange;
object xlWsCol;
//~~> create new Excel instance
Type tp;
tp = Type.GetTypeFromProgID("Excel.Application");
xlApp = Activator.CreateInstance(tp);
object[] parameter = new object[1];
parameter[0] = true;
xlApp.GetType().InvokeMember("Visible", BindingFlags.SetProperty, null, xlApp, parameter);
xlApp.GetType().InvokeMember("UserControl", BindingFlags.SetProperty, null, xlApp, parameter);
//~~> Get the xlWb collection
xlWbCol = xlApp.GetType().InvokeMember("Workbooks", BindingFlags.GetProperty, null, xlApp, null);
//~~> Create a new xlWb
xlWb = xlWbCol.GetType().InvokeMember("Add", BindingFlags.InvokeMethod, null, xlWbCol, null);
//~~> Get the worksheet collection
xlWsCol = xlWb.GetType().InvokeMember("WorkSheets", BindingFlags.GetProperty, null, xlApp, null);
//~~> Create a new workxlSheet
xlSheet = xlWb.GetType().InvokeMember("Add", BindingFlags.InvokeMethod, null, xlWsCol, null);
//~~> Assign cell to xlRange object
xlRange = xlSheet.GetType().InvokeMember("Cells", BindingFlags.GetProperty, null, xlSheet, new object[2] { 1, 1 });
//~~> Write a date to cell
xlRange.GetType().InvokeMember("Value", BindingFlags.SetProperty, null, xlRange, new object[] { "1-1-2012" });
//~~> Get the column
object cols = xlRange.GetType().InvokeMember("Columns", BindingFlags.GetProperty, null, xlRange, null);
//~~> Autofit the column
cols.GetType().InvokeMember("AutoFit", BindingFlags.InvokeMethod, null, cols, null);
//~~> Format the entire Column
cols.GetType().InvokeMember("NumberFormat", BindingFlags.SetProperty, null, cols, new object[1] { "DD/MM/YYYY" });
//~~> Release the object
//System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
}
Nota: Aggiungere using System.Reflection;
In cima
FOLLOWUP
Per Adatta per usedrange
questo funziona
private void button1_Click(object sender, EventArgs e)
{
object xlApp;
object xlWbCol;
object xlWb;
object xlSheet;
object xlRangeUsdRng;
object xlRange;
object xlWsCol;
//~~> create new Excel instance
Type tp;
tp = Type.GetTypeFromProgID("Excel.Application");
xlApp = Activator.CreateInstance(tp);
object[] parameter = new object[1];
parameter[0] = true;
xlApp.GetType().InvokeMember("Visible", BindingFlags.SetProperty, null, xlApp, parameter);
xlApp.GetType().InvokeMember("UserControl", BindingFlags.SetProperty, null, xlApp, parameter);
//~~> Get the xlWb collection
xlWbCol = xlApp.GetType().InvokeMember("Workbooks", BindingFlags.GetProperty, null, xlApp, null);
//~~> Create a new xlWb
xlWb = xlWbCol.GetType().InvokeMember("Add", BindingFlags.InvokeMethod, null, xlWbCol, null);
//~~> Get the worksheet collection
xlWsCol = xlWb.GetType().InvokeMember("WorkSheets", BindingFlags.GetProperty, null, xlApp, null);
//~~> Create a new workxlSheet
xlSheet = xlWb.GetType().InvokeMember("Add", BindingFlags.InvokeMethod, null, xlWsCol, null);
//~~> Assign cell F5 to xlRange object for testing purpose
xlRange = xlSheet.GetType().InvokeMember("Cells", BindingFlags.GetProperty, null, xlSheet, new object[2] {5, 6 });
xlRange.GetType().InvokeMember("Value", BindingFlags.SetProperty, null, xlRange, new object[] { "1-1-2012" });
//~~> Assign UsedRange
xlRangeUsdRng = xlSheet.GetType().InvokeMember("UsedRange", BindingFlags.GetProperty, null, xlSheet, null);
//~~> Get the Columns
object cols = xlRangeUsdRng.GetType().InvokeMember("EntireColumn", BindingFlags.GetProperty, null, xlRangeUsdRng, null);
//~~> Autofit
cols.GetType().InvokeMember("AutoFit", BindingFlags.InvokeMethod, null, cols, null);
}
Ciao seyren, benvenuto in SO! Vengo dal mondo VBA e non C#, quindi potrebbe non funzionare, ma comunque provalo. Se 'ws' o' rg'are sono definiti come Oggetti ma in realtà sono un foglio di lavoro o un oggetto di intervallo, è possibile semplicemente accedere ai loro metodi/proprietà allo stesso modo con l'associazione anticipata. Quindi, prova, 'ws.Cells (...)'! –
Il tuo codice di autofit funziona anche per me ... –
Ciao Siddharth - grazie per la risposta, mal provarlo ora, il mio problema è che il codice non ha funzionato, non fa quello che doveva fare, ma guardando il tuo codice penso di aver frainteso l'utilizzo del latebinding penso che sia stata la mia prima volta ad usarlo ma grazie :) –