sprawdzony
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);
}
Uwaga: Dodaj using System.Reflection;
Na najwyższym
nawiązanie
Dla AutoFit dla usedrange
to działa
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);
}
Witam serdecznie, witamy w SO! Pochodzę ze świata VBA, a nie C#, więc to może nie działać, ale mimo to spróbować. Jeśli "ws" lub "rg" są zdefiniowane jako Obiekty, ale w rzeczywistości są arkuszem lub obiektem zakresu, możesz po prostu uzyskać dostęp do ich metod/właściwości w taki sam sposób, jak przy wczesnym wiązaniu. Tak więc spróbuj, 'ws.Cells (...)'! –
Twój kod autofitu też działa dla mnie ... –
Witam Siddharth - dzięki za odpowiedź, źle spróbuj teraz, mój problem polega na tym, że kod po prostu nie działał, nie robi tego, co powinien, ale patrzy Twój kod myślę, że źle rozumiem, używając latebinding, myślę, że to dlatego, że był to mój pierwszy raz, kiedy go używałem, ale dzięki :) –