English 中文(简体)
能够进入C#中的Excel文档
原标题:can t access the excel file in c#

我关于通过使用PIA开立前哨文件的计划存在问题。 我的样本守则如下:

path = @"C:\Test Template.xls";
wb = objExcel.Workbooks.Open(path, Missing.Value, Missing.Value , Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

在实施该守则之后,该方案还发现了“Cannot Access Test Template.xls”的错误信息。 一个人能够解释这一错误的原因,即混乱。

问题回答

我确信,这里的问题是:

path = @"C:\Test Template.xls";

您应使用“@”来表示脚 the。

path = @"C:Test Template.xls";

OR 逃避作为“/”的反弹。

path = "C:\Test Template.xls";

不是两者。

您的同义词是错误的。

@“C:试验舱面”。

使用系统;

使用系统。 IO;

使用系统。 反思;

利用NUnit.Framework;

利用ExcelTools = Ms.Office;

利用Excel = 微软.Office.Interop。 Excel;

namespace Tests {

    public class ExcelSingle
    {
            public void ProcessWorkbook()
            {
                    string file = @"C:UsersChrisDesktopTestSheet.xls";
                    Console.WriteLine(file);

                    Excel.Application excel = null;
                    Excel.Workbook wkb = null;

                    try
                    {
                            excel = new Excel.Application();

                            wkb = ExcelTools.OfficeUtil.OpenBook(excel, file);

                            Excel.Worksheet sheet = wkb.Sheets["Data"] as Excel.Worksheet;

                            Excel.Range range = null;

                            if (sheet != null)
                                    range = sheet.get_Range("A1", Missing.Value);

                            string A1 = String.Empty;

                            if( range != null )
                                    A1 = range.Text.ToString();

                            Console.WriteLine("A1 value: {0}", A1);

                    }
                    catch(Exception ex)
                    {
                            //if you need to handle stuff
                            Console.WriteLine(ex.Message);
                    }
                    finally
                    {
                            if (wkb != null)
                                    ExcelTools.OfficeUtil.ReleaseRCM(wkb);

                            if (excel != null)
                                    ExcelTools.OfficeUtil.ReleaseRCM(excel);
                    }
            }
    }




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签