I am trying to insert text files into excel cell using Querytables.Add; no error, but the worksheet is empty. except for the single cell manipulation using Value2 property.
I already using macro to record the object used.
Can you help me on this(I am using vs2008, C# , excel 2003 and 2007; both shown empty cell).
Below is my code; thanks for your help
Application application = new ApplicationClass();
try
{
object misValue = Missing.Value;
wbDoc = application.Workbooks.Open(flnmDoc, misValue, misValue, misValue, misValue, misValue, misValue,
misValue, misValue, misValue, misValue, misValue, misValue, misValue,
misValue);
wsRefDocBudgetOwner = (Worksheet)wbDoc.Worksheets[2];
Range lRange = wsRefDocBudgetOwner.get_Range("B2", "B25");
var temp2 = wsRefDocBudgetOwner.QueryTables;
var temp = temp2.Add(@"TEXT;d:\temp\config ssas.txt", lRange, Type.Missing);
//temp.RefreshStyle = XlCellInsertionMode.xlInsertDeleteCells;
//temp.RefreshOnFileOpen = true;
wsRefDocBudgetOwner.get_Range("B1", "B1").Value2 = "Lgfdgast adsffdafadfads";
wbDoc.Save();
//wbDoc.SaveAs(flnmDoc2, misValue, misValue, misValue, misValue, misValue, XlSaveAsAccessMode.xlExclusive,
// misValue, misValue, misValue, misValue, misValue);
wbDoc.Close(Missing.Value, Missing.Value, Missing.Value);
}
finally
{
application.Quit();
}
I found it;
it is the RefreshStyle property. It should be set to xlInsertEntierRows.
temp.RefreshStyle = XlCellInsertionMode.xlInsertEntireRows;
as shown in http://support.microsoft.com/kb/306023
来源:https://stackoverflow.com/questions/2793975/import-txt-files-using-excel-interop-in-c-sharp-querytables-add