导入txt文件使用excel interop在C#(QueryTables.Add) [英] import txt files using excel interop in C# (QueryTables.Add)

查看:215
本文介绍了导入txt文件使用excel interop在C#(QueryTables.Add)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Querytables.Add将文本文件插入到excel单元格中;没有错误,但工作表是空的。除了使用Value2属性的单细胞操作外。



我已经使用宏来记录使用的对象。



你可以帮我吗(我使用vs2008,C#,excel 2003和2007;都显示为空单元格)。



下面是我的代码;感谢您的帮助

  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];


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


解决方案

p>

它是RefreshStyle属性。应该设置为xlInsertEntierRows。



temp.RefreshStyle = XlCellInsertionMode.xlInsertEntireRows;



http://support.microsoft.com/kb/306023


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

这篇关于导入txt文件使用excel interop在C#(QueryTables.Add)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆