这是访问MS Office Excel 2007文件的正确方法吗?C#访问Excel工作表
String connString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + file_path + ";Extended Properties=Excel 8.0;";
如果是这样,我该如何访问某个工作表并插入行?链接也欢迎。
这是访问MS Office Excel 2007文件的正确方法吗?C#访问Excel工作表
String connString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + file_path + ";Extended Properties=Excel 8.0;";
如果是这样,我该如何访问某个工作表并插入行?链接也欢迎。
有CodeProject上的一篇文章 - http://www.codeproject.com/KB/office/excel_using_oledb.aspx - 应该让你开始
连接字符串
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);
写入数据看Excel Generation这款采用自动化虽然。它可能有帮助。
您可以使用Excel Interop(的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];
它有一些weirdnesses(像那些 “失踪” 的参数),但它工作非常顺利。如果采取这种方法,请注意EXCEL.exe进程不会成为孤儿。