import txt files using excel interop in C# (QueryTables.Add)

与世无争的帅哥 提交于 2019-12-08 07:04:32

问题


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();
        }

回答1:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!