C#访问Excel工作表

C#访问Excel工作表,c#,excel,ms-office,connection-string,C#,Excel,Ms Office,Connection String,这是访问MS Office Excel 2007文件的正确方法吗 String connString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + file_path + ";Extended Properties=Excel 8.0;"; 如果是,如何访问某个工作表并插入行?也欢迎链接。这里有一篇关于codeproject的文章,应该可以让您开始这里有一篇关于codeproject的文章,应该可以让

这是访问MS Office Excel 2007文件的正确方法吗

        String connString = "Provider=Microsoft.Jet.OLEDB.4.0;" + 
    "Data Source=" + file_path + ";Extended Properties=Excel 8.0;";

如果是,如何访问某个工作表并插入行?也欢迎链接。

这里有一篇关于codeproject的文章,应该可以让您开始

这里有一篇关于codeproject的文章,应该可以让您开始

连接字符串

  connectionString = @"provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename + @";Extended Properties=""Excel 12.0;HDR=YES;IMEX=1""";
读取数据

 excelConnection = new System.Data.OleDb.OleDbConnection(connectionString);
        excelConnection.Open();
        dbSchema = excelConnection.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
        firstSheetName = dbSchema.Rows[0]["TABLE_NAME"].ToString();
        strSQL = "SELECT * FROM [" + firstSheetName + "]";
        da = new OleDbDataAdapter(strSQL, excelConnection);
        da.Fill(dt);
尽管如此,写入数据还是使用了自动化。它可能会有所帮助。

连接字符串

  connectionString = @"provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename + @";Extended Properties=""Excel 12.0;HDR=YES;IMEX=1""";
读取数据

 excelConnection = new System.Data.OleDb.OleDbConnection(connectionString);
        excelConnection.Open();
        dbSchema = excelConnection.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
        firstSheetName = dbSchema.Rows[0]["TABLE_NAME"].ToString();
        strSQL = "SELECT * FROM [" + firstSheetName + "]";
        da = new OleDbDataAdapter(strSQL, excelConnection);
        da.Fill(dt);
尽管如此,写入数据还是使用了自动化。它可能会有所帮助。

您可以使用(Microsoft.Office.Interop.Excel):

下面是一些代码的片段:

object missing = (object) Type.Missing;

Application app = new Application();

Workbooks books = app.Workbooks;

Workbook book = books.Open("somefile.xls", missing, missing, missing, missing, missing, missing,
            missing, missing, missing, missing, missing, missing, missing, missing);

Worksheet sheet = (Worksheet)book.Worksheets[1];
它有一些奇怪之处(比如那些“缺失”的参数),但它工作起来相当顺利。如果采用这种方法,请小心EXCEL.exe进程不会孤立。

您可以使用(Microsoft.Office.Interop.EXCEL):

下面是一些代码的片段:

object missing = (object) Type.Missing;

Application app = new Application();

Workbooks books = app.Workbooks;

Workbook book = books.Open("somefile.xls", missing, missing, missing, missing, missing, missing,
            missing, missing, missing, missing, missing, missing, missing, missing);

Worksheet sheet = (Worksheet)book.Worksheets[1];
它有一些奇怪之处(比如那些“缺失”的参数),但它工作起来相当顺利。如果采用这种方法,请小心EXCEL.exe进程不会孤立